Camera onPreviewFrame not called

前端 未结 4 1609
刺人心
刺人心 2020-12-09 08:36

when using the Camera.PreviewCallback implementation the onPreviewFrame is called without problem after initializing camera and starting preview (Camera.startPrevew()). The

相关标签:
4条回答
  • 2020-12-09 09:12

    I had a similar problem; see

    setOneShotPreviewCallback not hitting onPreviewFrame() in callback

    What I discovered was that after calling Camera#unlock() to prepare the MediaRecorder, it was necessary to call Camera#reconnect() before setting the preview callback. This is because Camera.unlock() detaches the camera from the process to let the MediaRecorder connect to it.

    http://developer.android.com/reference/android/hardware/Camera.html#unlock()

    In my investigations I also discovered that if you set any preview callbacks using other methods than the one shot method, you have to reset all of these after calling Camera#reconnect() as well. So, briefly:

    mCamera.unlock();
    //set up MediaRecorder
    mCamera.reconnect();
    mCamera.setPreviewCallback(mCallback);
    //or whatever callback method you want to use
    //and even if you've set this callback already
    

    I hope that helps!

    0 讨论(0)
  • 2020-12-09 09:25

    You must call setPreviewCallback in the surfaceChanged method, not only in the surfaceCreated.

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
    if (mHolder.getSurface() == null){
      return;
    }
    
    try {
        mCamera.stopPreview();
    } catch (Exception e){
      // ignore: tried to stop a non-existent preview
    }
    
    try {
        mCamera.setPreviewCallback(this);
        mCamera.setPreviewDisplay(mHolder);
        mCamera.startPreview();
    
    } catch (Exception e){
        Log.d(TAG, "Error starting camera preview: " + e.getMessage());
    }
    }
    
    0 讨论(0)
  • 2020-12-09 09:37

    You should call it within new instantiation of previewCallBacks() interface, like below

    public void surfaceCreated(SurfaceHolder holder) {
            // if (mediaRecorder == null) {
            try {
                camera = Camera.open();
                camera.setPreviewCallback(new PreviewCallback() {
                    public void onPreviewFrame(byte[] _data, Camera _camera) {
                    }
                }
            }
    }
    
    0 讨论(0)
  • 2020-12-09 09:38

    You need to call startPreview() again after a video or photo was taken.

    0 讨论(0)
提交回复
热议问题