OpenCV Java [ WARN:2] videoio(MSMF):无法抓取帧。错误: -1072875772



我一直在尝试制作一个简单的应用程序来打开和关闭相机。到目前为止,开头部分是有效的,但每次我试图关闭相机时,都会发生以下一系列事件:

  1. 捕获停止,但灯仍然亮着,图像没有被清除
  2. 第二次按下停止按钮时,图像会被清除
  3. 之后,每当我按下开始按钮时,无论我是否执行了第2步,它都不会执行任何操作,并会给我以下错误:

[WARN:1]videoio(MSMF(:调用OnReadSample((时出现错误状态:-1072875772[警告:1]videoio(MSMF(:异步ReadSample((调用失败,错误状态为:-1072877572[警告:2]videoio(MSMF(:无法抓取框架错误:-1072877572

package application;
import java.io.ByteArrayInputStream;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.opencv.videoio.VideoCapture;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
public class FXHelloCVController {
@FXML
private AnchorPane MainPane;
@FXML
private Button StartButton;
@FXML
private ImageView currentFrame;
@FXML
private Button StopButton;
private ScheduledExecutorService timer;
private VideoCapture capture = new VideoCapture(); 
private boolean cameraActive = false;
private static int cameraID = 0;
@FXML
void startCamera(ActionEvent event) {
if(!this.cameraActive) {
//if the camera is not active, open the camera
cameraActive = true;
this.capture.open(cameraID);
if(this.capture.isOpened()) {
//if the stream is available, run at 30fps (33 ms)
Runnable frameGrabber = new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
Image imageToShow = grabFrame();
Platform.runLater(new Runnable() {
@Override public void run() { 
currentFrame.setImage(imageToShow); }
});
}

};
this.timer = Executors.newSingleThreadScheduledExecutor();
this.timer.scheduleAtFixedRate(frameGrabber, 0, 33, 
TimeUnit.MILLISECONDS);
}
}
}
private Image grabFrame() {
// TODO Auto-generated method stub
Mat frame = new Mat();
MatOfByte buffer = new MatOfByte();
// check if the capture is open
if (this.capture.isOpened()){
try{
// read the current frame
this.capture.read(frame);
// if the frame is not empty, process it
if (!frame.empty()){
Imgcodecs.imencode(".png", frame, buffer);
}
}catch(Exception e){
// log the error
System.err.println("Exception during the image elaboration: " + 
e);
}
}
return new Image(new ByteArrayInputStream(buffer.toArray()));
}
@FXML
private void stopCamera(){
cameraActive = false;
if (this.timer!=null && !this.timer.isShutdown()){
try{
// stop the timer
this.timer.awaitTermination(33, TimeUnit.MILLISECONDS);
this.timer.shutdown();
this.currentFrame.setImage(null);
}catch (InterruptedException e){
e.printStackTrace();
}
}

这是我的控制器对象,有人能帮我找出问题吗?

由于硬件故障,我得到了完全相同的错误。我在Windows上,更新后它停止工作。也可能是驱动程序问题。我强烈怀疑它起源于平台级别。我换了网络摄像头,一切都好了。

我认为您的stopCamera方法需要capture.release((.

可能不相关,但我认为计时器的东西是错误的。即使相机设置为30 FPS,其流媒体时钟和Java定时之间的偏差最终也会导致故障。在线程中使用无限循环,并知道capture.read((将挂起,直到有帧可用。无限循环将在任何FPS下正常工作,前提是您有足够的处理能力来跟上。

在我的情况下,我甚至无法启动相机。这是因为"卡巴斯基端点安全",在退出病毒防护后,它开始工作。

最新更新