Reading the image in buffer in java

asked 2017-02-16 15:01:59 -0500

updated 2017-02-16 15:13:18 -0500

Hello. I Use OpenCV3.2. How to write the image to the buffer java. Now the image is written to hard drive. Thank You.

public class Video { public static void main (String args[]){ JFrame jf = new JFrame(); jf.setSize(640,480); jf.setLayout(null); JLabel jl = new JLabel(); jf.setLayout(null); jf.add(jl); jl.setBounds(0,0,640,480); Image tempIm; System.loadLibrary(Core.NATIVE_LIBRARY_NAME); VideoCapture camera = new VideoCapture(0); camera.set(Videoio.CV_CAP_PROP_FRAME_WIDTH, 1280); camera.set(Videoio.CV_CAP_PROP_FRAME_HEIGHT, 720);

    Thread t = new Thread(new Runnable() {
        @Override
        public void run() {
            if(!camera.isOpened()){
                System.out.println("Error");
            }
            else {
                int index = 0;
                Mat frame = new Mat();
                while(true){
                    if (camera.read(frame)){

                            Imgcodecs.imwrite("camera" + (index++) + ".jpg", frame);
                            System.out.println("camera" + (index) + ".jpg" + " index=" + index);
                        if(index!=0) {
                            ImageIcon ii = new ImageIcon("camera" + (index - 1) + ".jpg");
                            System.out.println("camera" + (index) + ".jpg" + " index2=" + index);
                            jl.setIcon(ii);
                            System.out.println("OK");
                            jl.repaint();
                            jf.repaint();


                            //break;
                        }
                    }
                }
            }
            camera.release();
        }
    });t.start();
    jf.setVisible(true);



}

}

edit retag flag offensive close merge delete