在前面的章節中,我們已經討論了如何使用OpenCV Java庫讀取和保存圖像。除此之外,我們還可以使用AWT/Swings和JavaFX等GUI庫在單獨的窗口中顯示加載的圖像。
Converting Mat to Buffered Image
要讀取圖像,我們使用imread()方法。此方法返回以矩陣形式讀取的圖像。但是,要將這個映像與GUI庫(AWT/Swings和JavaFX)一起使用,它應該被轉換爲包的java.AWT.image.BufferedImage類的對象。
下面是將OpenCV的aMat對象轉換爲BufferedImage對象的步驟。
Step 1: encode the Mat to MatOfByte
首先,需要將矩陣轉換爲字節矩陣。您可以使用類Imgcodecs的方法imencode()。下面是這個方法的語法。
imencode(ext, image, matOfByte);
此方法接受以下參數&負;
Ext−指定圖像格式的字符串參數(.jpg、.png等)
圖像的Mat對象
matOfByte−matOfByte類的空對象
使用此方法對圖像進行編碼,如下所示。
//Reading the image Mat image = Imgcodecs.imread(file); //instantiating an empty MatOfByte class MatOfByte matOfByte = new MatOfByte(); //Converting the Mat object to MatOfByte Imgcodecs.imencode(".jpg", image, matOfByte);
Step 2: Convert the MatOfByte object to byte array
使用方法toArray()將MatOfByte對象轉換爲字節數組。
byte[] byteArray = matOfByte.toArray();
Step 3: Preparing the InputStream object
準備InputStream對象,方法是將上一步創建的字節數組傳遞給ByteArrayInputStream類的構造函數。
//Preparing the InputStream object InputStream in = new ByteArrayInputStream(byteArray);
Step 4: Preparing the InputStream object
將上一步創建的輸入流對象傳遞給ImageIO類的read()方法。這將返回一個BufferedImage對象。
//Preparing the BufferedImage BufferedImage bufImage = ImageIO.read(in);
Displaying Image using AWT/Swings
要使用AWT/Swings框架顯示圖像,首先,使用imread()方法讀取圖像,然後按照上述步驟將其轉換爲BufferedImage。
然後,實例化JFrame類,並將創建的緩衝圖像添加到JFrame的ContentPane中,如下所示−
//Instantiate JFrame JFrame frame = new JFrame(); //Set Content to the JFrame frame.getContentPane().add(new JLabel(new ImageIcon(bufImage))); frame.pack(); frame.setVisible(true);
示例
下面的程序代碼演示如何使用OpenCV庫通過swing窗口讀取圖像並顯示圖像。
import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.InputStream; import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.core.MatOfByte; import org.opencv.imgcodecs.Imgcodecs; public class DisplayingImagesUsingSwings { public static void main(String args[]) throws Exception { //Loading the OpenCV core library System.loadLibrary( Core.NATIVE_LIBRARY_NAME ); //Reading the Image from the file and storing it in to a Matrix object String file = "C:/EXAMPLES/OpenCV/sample.jpg"; Mat image = Imgcodecs.imread(file); //Encoding the image MatOfByte matOfByte = new MatOfByte(); Imgcodecs.imencode(".jpg", image, matOfByte); //Storing the encoded Mat in a byte array byte[] byteArray = matOfByte.toArray(); //Preparing the Buffered Image InputStream in = new ByteArrayInputStream(byteArray); BufferedImage bufImage = ImageIO.read(in); //Instantiate JFrame JFrame frame = new JFrame(); //Set Content to the JFrame frame.getContentPane().add(new JLabel(new ImageIcon(bufImage))); frame.pack(); frame.setVisible(true); System.out.println("Image Loaded"); } }
在執行上述程序時,您將得到以下輸出&負;
Image Loaded
除此之外,您還可以看到一個窗口顯示加載的圖像,如下所示;
Displaying Image using JavaFX
要使用JavaFX顯示圖像,首先,使用imread()方法讀取圖像並將其轉換爲BufferedImage。然後,將BufferedImage轉換爲WritableImage,如下所示。
WritableImage writableImage = SwingFXUtils.toFXImage(bufImage, null);
將這個writebleimage對象傳遞給ImageView類的構造函數。
ImageView imageView = new ImageView(writableImage);
示例
下面的程序代碼演示如何使用OpenCV庫通過JavaFX窗口讀取圖像並顯示圖像。
import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import javafx.application.Application; import javafx.embed.swing.SwingFXUtils; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.image.ImageView; import javafx.scene.image.WritableImage; import javafx.stage.Stage; import javax.imageio.ImageIO; import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.core.MatOfByte; import org.opencv.imgcodecs.Imgcodecs; public class DisplayingImagesJavaFX extends Application { @Override public void start(Stage stage) throws IOException { WritableImage writableImage = loadImage(); //Setting the image view ImageView imageView = new ImageView(writableImage); //Setting the position of the image imageView.setX(50); imageView.setY(25); //setting the fit height and width of the image view imageView.setFitHeight(400); imageView.setFitWidth(500); //Setting the preserve ratio of the image view imageView.setPreserveRatio(true); //Creating a Group object Group root = new Group(imageView); //Creating a scene object Scene scene = new Scene(root, 600, 400); //Setting title to the Stage stage.setTitle("Loading an image"); //Adding scene to the stage stage.setScene(scene); //Displaying the contents of the stage stage.show(); } public WritableImage loadImage() throws IOException { //Loading the OpenCV core library System.loadLibrary( Core.NATIVE_LIBRARY_NAME ); //Reading the Image from the file and storing it in to a Matrix object String file ="C:/EXAMPLES/OpenCV/sample.jpg"; Mat image = Imgcodecs.imread(file); //Encoding the image MatOfByte matOfByte = new MatOfByte(); Imgcodecs.imencode(".jpg", image, matOfByte); //Storing the encoded Mat in a byte array byte[] byteArray = matOfByte.toArray(); //Displaying the image InputStream in = new ByteArrayInputStream(byteArray); BufferedImage bufImage = ImageIO.read(in); System.out.println("Image Loaded"); WritableImage writableImage = SwingFXUtils.toFXImage(bufImage, null); return writableImage; } public static void main(String args[]) { launch(args); } }
在執行上述程序時,您將得到以下輸出&負;
Image Loaded
除此之外,您還可以看到一個窗口顯示加載的圖像,如下所示;