我正在使用opencv和视频捕获,我想调用一个基于按键获取该帧的方法。我不确定我做错了什么,但当我按下空格键运行程序时,图像没有被捕获。这是我的代码:
public class VideoCap implements KeyListener{
GetContours takeFrame = new GetContours();
static{
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
}
VideoCapture cap;
Mat2Image mat2Img = new Mat2Image();
Mat copy = new Mat();
VideoCap(){
cap = new VideoCapture();
cap.open(0);
}
int frames = 0;
/*int savedCount = 0; //keep track of saved frames
String[] photos = {"first.png","second.png","third.png","forth.png",
"fifth.png","sixth.png","seventh.png","eigth.png","ninth.png"};
*/
boolean captured = false;
BufferedImage getOneFrame() {
frames++;
cap.read(mat2Img.mat);
mat2Img.mat = takeFrame.captureFrame(mat2Img.mat);
if(captured == true){
mat2Img.mat = takeFrame.captureFrame(mat2Img.mat);
System.out.println("WRITTEN!!!");
Imgcodecs.imwrite("first.png", mat2Img.mat);
System.exit(0);
}
return mat2Img.getImage(mat2Img.mat);
}
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_SPACE){
captured = true;
}
}