-
Notifications
You must be signed in to change notification settings - Fork 58
/
opencv_capture_picture.java
72 lines (58 loc) · 1.89 KB
/
opencv_capture_picture.java
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
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Size;
import org.opencv.videoio.VideoCapture;
import org.opencv.imgproc.Imgproc;
import org.opencv.highgui.HighGui;
public class WebcamCapture {
public static void main(String[] args) {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
VideoCapture cap = new VideoCapture(0);
if (!cap.isOpened()) {
System.out.println("Failed to open the camera!");
return;
}
make_1080p(cap);
// make_720p(cap);
// make_480p(cap);
// change_res(cap, 640, 480);
while (true) {
Mat frame = new Mat();
cap.read(frame);
frame = rescaleFrame(frame, 30);
HighGui.imshow("frame", frame);
Mat frame2 = rescaleFrame(frame, 500);
HighGui.imshow("frame2", frame2);
if (HighGui.waitKey(20) == 'q') {
break;
}
}
cap.release();
HighGui.destroyAllWindows();
}
public static void make_1080p(VideoCapture cap) {
cap.set(3, 1920);
cap.set(4, 1080);
}
public static void make_720p(VideoCapture cap) {
cap.set(3, 1020);
cap.set(4, 720);
}
public static void make_480p(VideoCapture cap) {
cap.set(3, 640);
cap.set(4, 480);
}
public static void change_res(VideoCapture cap, int width, int height) {
cap.set(3, width);
cap.set(4, height);
}
public static Mat rescaleFrame(Mat frame, double percent) {
int width = (int) (frame.width() * percent / 100);
int height = (int) (frame.height() * percent / 100);
Size dim = new Size(width, height);
Mat resizedFrame = new Mat();
Imgproc.resize(frame, resizedFrame, dim, 0, 0, Imgproc.INTER_AREA);
return resizedFrame;
}
}