title: OpenCV Java [ WARN:2] videoio(MSMF): can't grab frame. Error: -1072875772
body: <p>I have been trying to make a simple application that opens and closes the camera. So far, the opening part works, but every time I try to close the camera, this series of events happen:</p>
  
  <ol>
  <li>The capture stops capturing, but the lights still on and the image does not get cleared.</li>
  <li>The second time i press the stop button, the image gets cleared.</li>
  <li>After that, whenever i press the start button, regardless on whether or not I did step 2, it does nothing and gives me this error:</li>
  </ol>
  
  <blockquote>
    <p>[ WARN:1] videoio(MSMF): OnReadSample() is called with error status:
    -1072875772 [ WARN:1] videoio(MSMF): async ReadSample() call is failed with error status: -1072875772 [ WARN:2] videoio(MSMF): can't grab
    frame. Error: -1072875772</p>
  </blockquote>
  
  <pre><code>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 &amp;&amp; !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();
          }
      }
  </code></pre>
  
  <p>This right here is my controller object, can anyone help me find out whats wrong?</p>