|
| 1 | +import org.opencv.core.Core; |
| 2 | +import org.opencv.core.CvType; |
| 3 | +import org.opencv.core.Mat; |
| 4 | +import org.opencv.core.Size; |
| 5 | +import org.opencv.videoio.VideoCapture; |
| 6 | +import org.opencv.imgproc.Imgproc; |
| 7 | +import org.opencv.highgui.HighGui; |
| 8 | + |
| 9 | +public class WebcamCapture { |
| 10 | + public static void main(String[] args) { |
| 11 | + System.loadLibrary(Core.NATIVE_LIBRARY_NAME); |
| 12 | + |
| 13 | + VideoCapture cap = new VideoCapture(0); |
| 14 | + |
| 15 | + if (!cap.isOpened()) { |
| 16 | + System.out.println("Failed to open the camera!"); |
| 17 | + return; |
| 18 | + } |
| 19 | + |
| 20 | + make_1080p(cap); |
| 21 | + // make_720p(cap); |
| 22 | + // make_480p(cap); |
| 23 | + // change_res(cap, 640, 480); |
| 24 | + |
| 25 | + while (true) { |
| 26 | + Mat frame = new Mat(); |
| 27 | + cap.read(frame); |
| 28 | + |
| 29 | + frame = rescaleFrame(frame, 30); |
| 30 | + HighGui.imshow("frame", frame); |
| 31 | + |
| 32 | + Mat frame2 = rescaleFrame(frame, 500); |
| 33 | + HighGui.imshow("frame2", frame2); |
| 34 | + |
| 35 | + if (HighGui.waitKey(20) == 'q') { |
| 36 | + break; |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + cap.release(); |
| 41 | + HighGui.destroyAllWindows(); |
| 42 | + } |
| 43 | + |
| 44 | + public static void make_1080p(VideoCapture cap) { |
| 45 | + cap.set(3, 1920); |
| 46 | + cap.set(4, 1080); |
| 47 | + } |
| 48 | + |
| 49 | + public static void make_720p(VideoCapture cap) { |
| 50 | + cap.set(3, 1020); |
| 51 | + cap.set(4, 720); |
| 52 | + } |
| 53 | + |
| 54 | + public static void make_480p(VideoCapture cap) { |
| 55 | + cap.set(3, 640); |
| 56 | + cap.set(4, 480); |
| 57 | + } |
| 58 | + |
| 59 | + public static void change_res(VideoCapture cap, int width, int height) { |
| 60 | + cap.set(3, width); |
| 61 | + cap.set(4, height); |
| 62 | + } |
| 63 | + |
| 64 | + public static Mat rescaleFrame(Mat frame, double percent) { |
| 65 | + int width = (int) (frame.width() * percent / 100); |
| 66 | + int height = (int) (frame.height() * percent / 100); |
| 67 | + Size dim = new Size(width, height); |
| 68 | + Mat resizedFrame = new Mat(); |
| 69 | + Imgproc.resize(frame, resizedFrame, dim, 0, 0, Imgproc.INTER_AREA); |
| 70 | + return resizedFrame; |
| 71 | + } |
| 72 | +} |
0 commit comments