在本章中,咱們將學習如何使用OpenCV使用系統相機捕獲幀。org.opencv.videoio包的VideoCapture類包含使用相機捕獲視頻的類和方法。讓咱們一步一步學習如何捕捉幀 -java
在使用OpenCV庫編寫Java代碼時,您須要作的第一步是使用loadLibrary()加載OpenCV的本機庫。加載OpenCV本機庫,以下所示。git
// Loading the core library System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
使用前面本教程中提到的任何函數來實例化Mat類。app
// Instantiating the VideoCapture class (camera:: 0) VideoCapture capture = new VideoCapture(0);
您能夠使用VideoCapture類的read()方法從相機中讀取幀。此方法接受Mat類的對象來存儲讀取的幀。ide
// Reading the next video frame from the camera Mat matrix = new Mat(); capture.read(matrix);
代碼:
1 package com.gitee.dgw.Camera; 2 3 import javafx.application.Application; 4 import javafx.embed.swing.SwingFXUtils; 5 import javafx.scene.Group; 6 import javafx.scene.Scene; 7 import javafx.scene.image.ImageView; 8 import javafx.scene.image.WritableImage; 9 import javafx.stage.Stage; 10 import org.opencv.core.Mat; 11 import org.opencv.imgcodecs.Imgcodecs; 12 import org.opencv.videoio.VideoCapture; 13 14 import java.awt.image.BufferedImage; 15 import java.awt.image.DataBufferByte; 16 import java.awt.image.WritableRaster; 17 18 public class CameraSnapshotJavaFX extends Application { 19 20 static { 21 platformUtils.loadLibraries(); 22 } 23 Mat matrix = null; 24 25 @Override 26 public void start(Stage stage) { 27 // Capturing the snapshot from the camera 28 CameraSnapshotJavaFX obj = new CameraSnapshotJavaFX(); 29 WritableImage writableImage = obj.capureSnapShot(); 30 31 // Saving the image 32 obj.saveImage(); 33 34 // Setting the image view 35 ImageView imageView = new ImageView(writableImage); 36 37 // setting the fit height and width of the image view 38 imageView.setFitHeight(400); 39 imageView.setFitWidth(600); 40 41 // Setting the preserve ratio of the image view 42 imageView.setPreserveRatio(true); 43 44 // Creating a Group object 45 Group root = new Group(imageView); 46 47 // Creating a scene object 48 Scene scene = new Scene(root, 600, 400); 49 50 // Setting title to the Stage 51 stage.setTitle("Capturing an image"); 52 53 // Adding scene to the stage 54 stage.setScene(scene); 55 56 // Displaying the contents of the stage 57 stage.show(); 58 } 59 public WritableImage capureSnapShot() { 60 WritableImage WritableImage = null; 61 62 63 // Instantiating the VideoCapture class (camera:: 0) 64 VideoCapture capture = new VideoCapture(0); 65 66 // Reading the next video frame from the camera 67 Mat matrix = new Mat(); 68 capture.read(matrix); 69 70 // If camera is opened 71 if( capture.isOpened()) { 72 // If there is next video frame 73 if (capture.read(matrix)) { 74 // Creating BuffredImage from the matrix 75 BufferedImage image = new BufferedImage(matrix.width(), 76 matrix.height(), BufferedImage.TYPE_3BYTE_BGR); 77 78 WritableRaster raster = image.getRaster(); 79 DataBufferByte dataBuffer = (DataBufferByte) raster.getDataBuffer(); 80 byte[] data = dataBuffer.getData(); 81 matrix.get(0, 0, data); 82 this.matrix = matrix; 83 84 // Creating the Writable Image 85 WritableImage = SwingFXUtils.toFXImage(image, null); 86 } 87 } 88 return WritableImage; 89 } 90 public void saveImage() { 91 // Saving the Image 92 String file = "z://sanpshot.jpg"; 93 94 // Instantiating the imgcodecs class 95 Imgcodecs imageCodecs = new Imgcodecs(); 96 97 // Saving it again 98 imageCodecs.imwrite(file, matrix); 99 } 100 public static void main(String args[]) { 101 launch(args); 102 } 103 }