Javacv调用摄像头采集图像

直接贴代码


import org.bytedeco.javacpp.opencv_core;
import org.bytedeco.javacv.*;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.FrameGrabber.Exception;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.awt.image.WritableRaster;
import java.io.File;
import java.io.IOException;

import static org.bytedeco.javacpp.opencv_core.cvReleaseImage;

/**
 * 作者:Haibo.Liu
 * 描述:
 * 日期: 2017/5/5
 * QQ:836915746
 */
public class Pic implements Runnable {

    public static void main(String[] args) throws FrameGrabber.Exception {
        Pic p = new Pic();
        p.startCamera();
    }

    private static Timer timer;
    private static int width, height;
    static BufferedImage bImage;
    public TimerAction timerAction;


    class TimerAction implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent arg0) {
            System.out.println("=====================================================================");
            try {
                ImageIO.write(bImage, "jpg", new File("d:/a.jpg"));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void startCamera() throws FrameGrabber.Exception {
        OpenCVFrameGrabber grabber = new OpenCVFrameGrabber(0);
        grabber.start(); // 开始获取摄像头数据
        OpenCVFrameConverter.ToIplImage converter = new OpenCVFrameConverter.ToIplImage();//转换器
        Frame frame = grabber.grab();
        opencv_core.IplImage grabbedImage = converter.convert(frame);//抓取一帧视频并将其转换为图像,至于用这个图像用来做什么?加水印,人脸识别等等自行添加
        width = grabbedImage.width();
        height = grabbedImage.height();
        bImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

        //显示器监控当前图像
        Graphics2D bGraphics = bImage.createGraphics();
        CanvasFrame canvas = new CanvasFrame("Camera", 1);
        canvas.setCanvasSize(width, height);

        timer = new Timer(1400, timerAction);// 设置每x秒保存一次照片。
        timer.start();

        while ((frame = grabber.grab()) != null) {
            //注释掉下行代码,及cavas.isVisibal,在Linux上可能不会出现警告
            canvas.showImage(frame);
            bGraphics.drawImage(iplToBufImgData(grabbedImage), null, 0, 0);
        }
        cvReleaseImage(grabbedImage);
        grabber.stop();
        canvas.dispose();
    }


    public static BufferedImage iplToBufImgData(opencv_core.IplImage mat) {
        if (mat.height() > 0 && mat.width() > 0) {
            BufferedImage image = new BufferedImage(mat.width(), mat.height(), BufferedImage.TYPE_3BYTE_BGR);
            WritableRaster raster = image.getRaster();
            DataBufferByte dataBuffer = (DataBufferByte) raster.getDataBuffer();
            byte[] data = dataBuffer.getData();
            mat.getByteBuffer().get(data);
            return image;
        }
        return null;
    }


    @Override
    public void run() {
        try {
            startCamera();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}








评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值