CameraUtils.java 4.81 KB
package com.huaheng.common.utils;

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

import org.bytedeco.javacpp.opencv_core.IplImage;
import org.bytedeco.javacv.*;

public class CameraUtils {
    private static CameraThread cameraThread = null;

    /**
     * outFile 存储地址 列如 D:\\test\\2.jpg
     * width height 窗口大小 自己设置一下
     */
    public static void Camera(String outFile, int width, int height) {
        OpenCVFrameGrabber grabber = new OpenCVFrameGrabber(0);
        grabber.setImageWidth(width);
        grabber.setImageHeight(height);
        //新建一个窗口
        CanvasFrame canvas = new CanvasFrame("摄像头");
        canvas.setAlwaysOnTop(true);
        canvas.setLayout(null);
//        canvas.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Font font = new Font("微软雅黑", Font.BOLD, 30);
        JButton jButton = new JButton("确认上传");
        JButton jButton1 = new JButton("继续拍照");
        jButton1.setEnabled(false);
        JButton jButton2 = new JButton("关闭");
        jButton.setFont(font);
        jButton1.setFont(font);
        jButton2.setFont(font);
        jButton.setBounds(20, 550, 200, 100);
        jButton1.setBounds(250, 550, 200, 100);
        jButton2.setBounds(470, 550, 200, 100);
        canvas.add(jButton);
        canvas.add(jButton1);
        canvas.add(jButton2);
        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (cameraThread != null) {
                    cameraThread.terminate();
                }
                jButton1.setEnabled(true);
            }
        });
        jButton1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jButton1.setEnabled(false);
                cameraThread = new CameraThread(height, height, canvas, grabber, outFile);
                cameraThread.start();
            }
        });

        jButton2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                cameraThread.terminate();
                canvas.dispose();
            }
        });
        cameraThread = new CameraThread(width, height, canvas, grabber, outFile);
        cameraThread.start();

    }

    /**
     * 输出图片
     * @param f 视频一帧图片
     * @param targetFileName
     */
    public static void doExecuteFrame(Frame f, String targetFileName) {
        if (null == f || null == f.image) {
            return;
        }
        Java2DFrameConverter converter = new Java2DFrameConverter();
        targetFileName = targetFileName.replace("mp4", "jpg");
        System.out.println("targetFileName" + targetFileName);
        String imageMat = "jpg";
        String FileName = targetFileName;
        BufferedImage bi = converter.getBufferedImage(f);
        File output = new File(FileName);
        try {
            ImageIO.write(bi, imageMat, output);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

class CameraThread extends Thread{
    /** 此变量必须加上volatile */
    private volatile boolean stop = true;
    private int width;
    private int height;
    private CanvasFrame canvas;
    private OpenCVFrameGrabber grabber;
    private String outFile;
    public CameraThread(int width, int height, CanvasFrame canvas, OpenCVFrameGrabber grabber, String outFile) {
        super();
        this.width = width;
        this.height = height;
        this.canvas = canvas;
        this.grabber = grabber;
        this.outFile = outFile;
    }

    @Override
    public void run() {
        int i=0;
        try {
            grabber.start(); //开始获取摄像头数据
            while(stop){
                if(i<2) {
                    canvas.setSize(width, height);
                    i++;
                }
                canvas.showImage(grabber.grab());
                //50毫秒刷新一次图像
                Thread.sleep(50);
            }
            OpenCVFrameConverter.ToIplImage converter =new OpenCVFrameConverter.ToIplImage();
            IplImage src = null;
            Frame capturedFrame = null;
            capturedFrame= grabber.grab();
            src =converter.convert(capturedFrame);
            capturedFrame =converter.convert(src);
            CameraUtils.doExecuteFrame(grabber.grab(),outFile);
            grabber.stop();//停止抓取
        } catch (org.bytedeco.javacv.FrameGrabber.Exception | InterruptedException e) {
            e.printStackTrace();
        }
    }
    public void terminate() {
        stop = false;
    }

}