CameraUtils.java
4.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
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;
}
}