opencv3_java 启动摄像头 VideoCapture Videoio

来源:互联网 发布:游戏优化学什么 编辑:程序博客网 时间:2024/06/05 03:31

启动摄像头 VideoCapture Videoio


package opencv_java_demo;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.WindowConstants;import java.awt.*;import java.awt.image.BufferedImage;import org.opencv.core.Core;import org.opencv.core.Mat;import org.opencv.videoio.VideoCapture;import org.opencv.videoio.Videoio;public class CaptureBasic extends JPanel {private static final long serialVersionUID=1L;private BufferedImage mImg;private BufferedImage mat2BI(Mat mat){int dataSize =mat.cols()*mat.rows()*(int)mat.elemSize();byte[] data=new byte[dataSize];mat.get(0, 0,data);int type=mat.channels()==1?BufferedImage.TYPE_BYTE_GRAY:BufferedImage.TYPE_3BYTE_BGR;if(type==BufferedImage.TYPE_3BYTE_BGR){for(int i=0;i<dataSize;i+=3){byte blue=data[i+0];data[i+0]=data[i+2];data[i+2]=blue;}}BufferedImage image=new BufferedImage(mat.cols(),mat.rows(),type);image.getRaster().setDataElements(0, 0, mat.cols(), mat.rows(), data);return image;}public void paintComponent(Graphics g){if(mImg!=null){g.drawImage(mImg, 0, 0, mImg.getWidth(),mImg.getHeight(),this);}}public static void main(String[] args) {try{System.loadLibrary(Core.NATIVE_LIBRARY_NAME);Mat capImg=new Mat();VideoCapture capture=new VideoCapture(0);int height = (int)capture.get(Videoio.CAP_PROP_FRAME_HEIGHT);int width = (int)capture.get(Videoio.CAP_PROP_FRAME_WIDTH);if(height==0||width==0){throw new Exception("camera not found!");}JFrame frame=new JFrame("camera");frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);CaptureBasic panel=new CaptureBasic();frame.setContentPane(panel);frame.setVisible(true);frame.setSize(width+frame.getInsets().left+frame.getInsets().right,height+frame.getInsets().top+frame.getInsets().bottom);while(frame.isShowing()){capture.read(capImg);panel.mImg=panel.mat2BI(capImg);panel.repaint();}capture.release();frame.dispose();}catch(Exception e){System.out.println("例外:" + e);}finally{System.out.println("--done--");}}}




阅读全文
0 0
原创粉丝点击