准备
- JDK1.8
- OpenCV4.0.1
- IDEA (任意版本),参考博客:https://blog.youkuaiyun.com/MaxBigBug/article/details/142784125
OpenCV安装
官网下载地址:https://opencv.org/releases/
百度云下载地址:https://pan.baidu.com/s/121nnzQMlshkSc96b2mDSeQ?pwd=hdip 提取码: hdip
自行选择安装路径,点击Extract即可
IDEA配置
以上步骤完成后 创建一个 main 方法 如果能打印出版本号 即可表示配置成功
实现控制摄像头连续拍照功能
package com.example.OpenCV;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.highgui.HighGui;
import org.opencv.videoio.VideoCapture;
import org.opencv.imgcodecs.Imgcodecs;
import java.util.Scanner;
/**
* @description:
* @author: xxjxl
* @date: 2024/10/30 15:24
*/
public class FaceRecognitionCapture {
static {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
}
public static void main(String[] args) {
VideoCapture camera = new VideoCapture(0); // 0 是默认摄像头
Mat frame = new Mat();
Scanner scanner = new Scanner(System.in);
int photoCount = 0; // 照片计数器
if (!camera.isOpened()) {
System.out.println("无法打开摄像头");
return;
}
System.out.println("按下 Enter 键拍照,按 'q' 键退出");
while (true) {
camera.read(frame);
if (!frame.empty()) {
HighGui.imshow("Camera", frame);
}
// 检查用户输入
if (scanner.hasNextLine()) {
String input = scanner.nextLine();
if (input.equals("")) {
// 拍照并保存
String filePath = "D:\\OpenCV\\captured_image_" + photoCount + ".jpg"; // 保存路径
Imgcodecs.imwrite(filePath, frame);
System.out.println("照片已保存到: " + filePath);
photoCount++; // 增加照片计数
} else if (input.equals("q")) {
break; // 按 'q' 键退出
}
}
// 每隔30毫秒刷新图像
if (HighGui.waitKey(30) >= 0) {
break; // 按任意键退出
}
}
camera.release();
HighGui.destroyAllWindows();
scanner.close();
}
}
小结
期待各位大佬分享更多OpenCV的用法,欢迎留言讨论。
码海无涯,回头是岸