PrtScr%20capture_2.jpg 

 
直接顯示:

import org.bytedeco.javacv.CanvasFrame;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.FrameGrabber;
import org.bytedeco.javacv.VideoInputFrameGrabber;
 
public class WebCamDemo {
 
    public static void main(String[] args) throws Exception {
        CanvasFrame canvas = new CanvasFrame("WebCam Demo");
        canvas.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
 
        VideoInputFrameGrabber webcam = new VideoInputFrameGrabber(0); // 1 for next camera
        
        FrameGrabber grabber = webcam;
        
        grabber.start();
        Frame img;
        while (true) {
            img = grabber.grab();
            if (img != null) {
                canvas.showImage(img);
            }
        }
    }
 
}

 //-----------------------------------------

保存成圖片 1:


import org.bytedeco.javacpp.opencv_core.IplImage;
import static org.bytedeco.javacpp.opencv_imgcodecs.cvSaveImage;
import org.bytedeco.javacv.CanvasFrame;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.FrameGrabber;
import org.bytedeco.javacv.OpenCVFrameConverter;
import org.bytedeco.javacv.VideoInputFrameGrabber;

public class WebCamDemo {

    public static void main(String[] args) throws Exception {
        CanvasFrame canvas = new CanvasFrame("WebCam Demo");
        canvas.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);

        VideoInputFrameGrabber webcam = new VideoInputFrameGrabber(0); // 1 for next camera
        FrameGrabber grabber = webcam;

        grabber.start();

        OpenCVFrameConverter.ToIplImage converter = new OpenCVFrameConverter.ToIplImage();

        Frame frame;
        while (true) {
            frame = grabber.grab();
            if (frame != null) {
                canvas.showImage(frame);
                IplImage img = converter.convert(frame);
                cvSaveImage("C:/Test/X.jpg", img); // 保存到指定路徑
                canvas.waitKey(1000); // 延遲一秒
            }
        }
    }

}


 //-----------------------------------------

保存成圖片 2 (用 ImageIO 寫入):

import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import org.bytedeco.javacpp.opencv_core.IplImage;
import org.bytedeco.javacv.CanvasFrame;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.FrameGrabber;
import org.bytedeco.javacv.Java2DFrameConverter;
import org.bytedeco.javacv.OpenCVFrameConverter;
import org.bytedeco.javacv.VideoInputFrameGrabber;

public class WebCamDemo {

    public static void main(String[] args) throws Exception {
        CanvasFrame canvas = new CanvasFrame("WebCam Demo");
        canvas.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);

        VideoInputFrameGrabber webcam = new VideoInputFrameGrabber(0); // 1 for next camera
        FrameGrabber grabber = webcam;

        grabber.start();

        OpenCVFrameConverter.ToIplImage converter = new OpenCVFrameConverter.ToIplImage();

        Frame frame;
        while (true) {
            frame = grabber.grab();
            if (frame != null) {
                canvas.showImage(frame);
                IplImage img = converter.convert(frame);

                BufferedImage bi = IplImageToBufferedImage(img);

                ImageIO.write(bi, "jpg", new File("C:/Test/Y.jpg"));

                canvas.waitKey(1000);
            }
        }
    }

    public static BufferedImage IplImageToBufferedImage(IplImage src) {
        OpenCVFrameConverter.ToIplImage grabberConverter = new OpenCVFrameConverter.ToIplImage();
        Java2DFrameConverter paintConverter = new Java2DFrameConverter();
        Frame frame = grabberConverter.convert(src);
        return paintConverter.getBufferedImage(frame, 1);
    }

}






    全站熱搜

    黃彥霖 發表在 痞客邦 留言(0) 人氣()