利用Java+OpenCV實現拍照功能

由於項目需要拍照,看瞭好多的資料不是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 {undefined

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

    private BufferedImage mat2BI(Mat mat){undefined
        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){undefined
            for(int i=0;i<dataSize;i+=3){undefined
                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){undefined
        if(mImg!=null){undefined
            g.drawImage(mImg, 0, 0, mImg.getWidth(),mImg.getHeight(),this);
        }
    }

    public static void main(String[] args) {undefined
        try{undefined
            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){undefined
                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() {undefined
                @Override
                public void mouseClicked(MouseEvent arg0) {undefined
                     System.out.println("click");
                     saveImg();
                }
                @Override
                public void mouseMoved(MouseEvent arg0) {undefined
                    System.out.println("move");

                }
                @Override
                public void mouseReleased(MouseEvent arg0) {undefined
                    System.out.println("mouseReleased");
                }
                @Override
                public void mousePressed(MouseEvent arg0) {undefined
                    System.out.println("mousePressed");
                }
                @Override
                public void mouseExited(MouseEvent arg0) {undefined
                    System.out.println("mouseExited");
                    //System.out.println(arg0.toString());
                }
                @Override
                public void mouseDragged(MouseEvent arg0) {undefined
                    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){undefined
                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){undefined
            System.out.println("例外:" + e);
        }finally{undefined
            System.out.println("--done--");
        }

    }
    
    //點擊面板
    public static void saveImg(){undefined
        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("保存圖片");
    }
}

點擊在對應目錄下生成的照片

到此這篇關於利用Java+OpenCV實現拍照功能的文章就介紹到這瞭,更多相關Java OpenCV拍照內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: