如何用Java+OpenCV实现拍照功能

如何用Java+OpenCV实现拍照功能

本篇内容主要讲解“如何用Java+OpenCV实现拍照功能”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“如何用Java+OpenCV实现拍照功能”吧!

依赖jar包:只有一个OpenCv的jar包,可以直接从官网上下载

下面上代码

importjava.awt.Graphics;importjava.awt.event.MouseAdapter;importjava.awt.event.MouseEvent;importjava.awt.image.BufferedImage;importjavax.swing.JFrame;importjavax.swing.JPanel;importjavax.swing.WindowConstants;importorg.opencv.core.Core;importorg.opencv.core.Mat;importorg.opencv.imgcodecs.Imgcodecs;importorg.opencv.imgproc.Imgproc;importorg.opencv.videoio.VideoCapture;importorg.opencv.videoio.Videoio;publicclassCaptureBasicextendsJPanel{undefinedprivatestaticBufferedImagemImg;privatestaticVideoCapturecapture;privatestaticMatcapImg;privateBufferedImagemat2BI(Matmat){undefinedintdataSize=mat.cols()*mat.rows()*(int)mat.elemSize();byte[]data=newbyte[dataSize];mat.get(0,0,data);inttype=mat.channels()==1?BufferedImage.TYPE_BYTE_GRAY:BufferedImage.TYPE_3BYTE_BGR;if(type==BufferedImage.TYPE_3BYTE_BGR){undefinedfor(inti=0;i<dataSize;i+=3){undefinedbyteblue=data[i+0];data[i+0]=data[i+2];data[i+2]=blue;}}BufferedImageimage=newBufferedImage(mat.cols(),mat.rows(),type);image.getRaster().setDataElements(0,0,mat.cols(),mat.rows(),data);returnimage;}publicvoidpaintComponent(Graphicsg){undefinedif(mImg!=null){undefinedg.drawImage(mImg,0,0,mImg.getWidth(),mImg.getHeight(),this);}}publicstaticvoidmain(String[]args){undefinedtry{undefinedSystem.loadLibrary(Core.NATIVE_LIBRARY_NAME);capImg=newMat();capture=newVideoCapture(0);//capture.open(0);intheight=(int)capture.get(Videoio.CAP_PROP_FRAME_HEIGHT);intwidth=(int)capture.get(Videoio.CAP_PROP_FRAME_WIDTH);if(height==0||width==0){undefinedthrownewException("cameranotfound!");}JFrameframe=newJFrame("camera");frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);CaptureBasicpanel=newCaptureBasic();panel.addMouseListener(newMouseAdapter(){undefined@OverridepublicvoidmouseClicked(MouseEventarg0){undefinedSystem.out.println("click");saveImg();}@OverridepublicvoidmouseMoved(MouseEventarg0){undefinedSystem.out.println("move");}@OverridepublicvoidmouseReleased(MouseEventarg0){undefinedSystem.out.println("mouseReleased");}@OverridepublicvoidmousePressed(MouseEventarg0){undefinedSystem.out.println("mousePressed");}@OverridepublicvoidmouseExited(MouseEventarg0){undefinedSystem.out.println("mouseExited");//System.out.println(arg0.toString());}@OverridepublicvoidmouseDragged(MouseEventarg0){undefinedSystem.out.println("mouseDragged");//System.out.println(arg0.toString());}});frame.setContentPane(panel);frame.setVisible(true);frame.setSize(width+frame.getInsets().left+frame.getInsets().right,height+frame.getInsets().top+frame.getInsets().bottom);intn=0;Mattemp=newMat();while(frame.isShowing()&&n<5){undefinedSystem.out.println("第"+n+"张");capture.read(capImg);Imgproc.cvtColor(capImg,temp,Imgproc.COLOR_BGRA2BGR);//Imgcodecs.imwrite("C:/HWKJ/opencv/"+n+".png",temp);//自动保存//panel.mImg=panel.mat2BI(detectFace(capImg));panel.mImg=panel.mat2BI(capImg);panel.repaint();//n++;//break;}capture.release();//frame.dispose();}catch(Exceptione){undefinedSystem.out.println("例外:"+e);}finally{undefinedSystem.out.println("--done--");}}//点击面板publicstaticvoidsaveImg(){undefinedMattemp=newMat();intheight=(int)capture.get(Videoio.CAP_PROP_FRAME_HEIGHT);intwidth=(int)capture.get(Videoio.CAP_PROP_FRAME_WIDTH);capture.read(capImg);Imgproc.cvtColor(capImg,temp,Imgproc.COLOR_BGRA2BGR);Imgcodecs.imwrite("C:/HWKJ/opencv/aaa.png",temp);System.out.println("保存图片");}}

点击在对应目录下生成的照片

到此,相信大家对“如何用Java+OpenCV实现拍照功能”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

发布于 2022-01-06 23:29:57
收藏
分享
海报
0 条评论
35
上一篇:Java多线程方法有哪些 下一篇:Python的数据类型知识点有哪些
目录

    0 条评论

    本站已关闭游客评论,请登录或者注册后再评论吧~

    忘记密码?

    图形验证码