Java + OpenCV 实现拍照

3 篇文章 0 订阅
1 篇文章 0 订阅

由于项目需要拍照,看了好多的资料不是C语言的就是python,开始用的JavaCv但是有好多问题,所以改成了OpenCv

只能硬着上了,查了好的网上的资料,终于找到了

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

下面上代码

import java.awt.Graphics;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.opencv.videoio.VideoCapture;
import org.opencv.videoio.Videoio;

public class CaptureBasic extends JPanel {

    private static BufferedImage mImg;
    private static VideoCapture capture;
    private static  Mat capImg;

    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);

            capImg=new Mat();
            capture =new VideoCapture(0);
           // capture.open(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();
            panel.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent arg0) {
                     System.out.println("click");
                     saveImg();
                }
                @Override
                public void mouseMoved(MouseEvent arg0) {
                    System.out.println("move");

                }
                @Override
                public void mouseReleased(MouseEvent arg0) {
                    System.out.println("mouseReleased");
                }
                @Override
                public void mousePressed(MouseEvent arg0) {
                    System.out.println("mousePressed");
                }
                @Override
                public void mouseExited(MouseEvent arg0) {
                    System.out.println("mouseExited");
                    //System.out.println(arg0.toString());
                }
                @Override
                public void mouseDragged(MouseEvent arg0) {
                    System.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);
            int n=0;
            Mat temp=new Mat();
            while(frame.isShowing()&& n<5){
                System.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(Exception e){
            System.out.println("例外:" + e);
        }finally{
            System.out.println("--done--");
        }

    }
    
    //点击面板
    public static void saveImg(){
        Mat temp=new Mat();
        int height = (int)capture.get(Videoio.CAP_PROP_FRAME_HEIGHT);
        int width = (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("保存图片");
    }
}

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

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值