OpenCV's native camera in Android prevents vid

2019-06-12 14:53发布

问题:

I'd like to play a video file in android (using VideoView and MediaController) while enabling the camera to capture images. No camera display or preview is required. However, if I invoke "openCamera" (instantiate a new VideoCapture), the application fails and informs that: video cannot display. If I remove the call to instantiate the camera, the video plays smoothly.

public class Myvideoplayer extends Activity {
  public FrameLayout imageFrame;
  public RelativeLayout imageFrameLayout;
  private ProgressDialog progressDialog;@
  Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    imageFrame = new FrameLayout(this);
    imageFrameLayout = new RelativeLayout(this);
    imageFrameLayout.setGravity(Gravity.CENTER);
    VideoView videoPlayer = new VideoView(this);
    MediaController mediaController = new MediaController(this);
    mediaController.setAnchorView(videoPlayer);
    videoPlayer.setMediaController(mediaController);
    RelativeLayout.LayoutParams rel_btnBack = 
      new RelativeLayout.LayoutParams(
        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    File root = Environment.getExternalStorageDirectory();
    String myURI = root + "/DCIM/Camera/20121122_170657.mp4";
    Uri video = Uri.parse(myURI);
    videoPlayer.setVideoURI(video);
    videoPlayer.requestFocus();
    videoPlayer.start();
    imageFrameLayout.addView(videoPlayer);
    imageFrame.addView(imageFrameLayout);
    setContentView(imageFrame);
    //set up camera
    progressDialog = ProgressDialog.show(this,
                             "Please wait....", "Openning camera");
    new Thread(new Runnable() {
      public void run() {
        try {
          Thread.sleep(2 * 1000);
          progressDialog.dismiss();
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
    }).start();
    new Calculating().execute(1000, 1000, 1000);
  }
  public void InfoMessageBox(String str, String str2) {
    AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this);
    dlgAlert.setTitle(str);
    dlgAlert.setMessage(str2);
    dlgAlert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int whichButton) {}
    });
    dlgAlert.create().show();
  }
  private class Calculating extends AsyncTask < Integer, Integer, Integer > {
    protected Integer doInBackground(Integer...intInput) {
      return 0;
    }
    protected void onProgressUpdate(Integer...progress) {}
    protected void onPostExecute(Integer result) {
      super.onPostExecute(result);
      cameraWrapper mCameraController = new cameraWrapper();
      if (!mCameraController.openCamera()) {
        InfoMessageBox("", "OOps, camera not ready!");
        return;
      }
    }
  }
}

And here is the camera opening

 public boolean openCamera() {
                synchronized (this) {
            releaseCamera();
            mCamera = new VideoCapture(Highgui.CV_CAP_ANDROID+1);
            if (!mCamera.isOpened()) {
                mCamera.release();
                mCamera = null;
                return false;
            }
        }
        return true;
    }

 public void releaseCamera() {
                    synchronized (this) {
            if (mCamera != null) {
                    mCamera.release();
                    mCamera = null;
            }
        }
    }