简体   繁体   English

android.hardware.Camera每第二次打开失败

[英]android.hardware.Camera fails to open every second time

Problem background 问题背景

I am developing a VR Project on Unreal Engine 4, and the project requires the usage of Android's native camera. 我正在虚幻引擎4上开发一个VR项目,该项目需要使用Android的本机摄像头。 Since there are no built-in functions in UE4 to iteract with Android's native methods, I customized this plugin under my need. 由于UE4中没有内置功能可以使用Android的本地方法进行迭代,因此我根据需要自定义了此插件

The original plugin uses the JNI interface to iteract with C++ code. 原始插件使用JNI接口来迭代C ++代码。 It calls camera.open() and camera.startPreview() on UE4's EventBeginPlay , and calls camera.stopPreview() and camera.Release() on UE4's EventEndPlay . 它调用camera.open()camera.startPreview()上UE4的EventBeginPlay ,并调用camera.stopPreview()camera.Release()上UE4的EventEndPlay Since it is a known issue that EventEndPlay never fires up on Android platform, I decided to manipulate the camera in onResume() and onPause() methods. 由于这是EventEndPlay永远不会在Android平台上触发的已知问题EventEndPlay我决定使用onResume()onPause()方法来操纵摄像机。 Here is the code: 这是代码:

<gameActivityClassAdditions>
<insert>
  /* Unrelevant code goes here */
    ...
    ...
  /* End of unrelevant code */

  public void AndroidThunkJava_startCamera()
  {                        
    surfaceTexture = new SurfaceTexture(10);
    surfaceTexture.setDefaultBufferSize(preferredWidth, preferredHeight);

    if (camera == null){
      try {
        camera = Camera.open();
      } catch (RuntimeException exc) {
        return;
      }
    }               

    try {
      camera.setPreviewTexture(surfaceTexture);
    } catch (IOException t) {
      return;
    }

    Parameters cameraParam = camera.getParameters();

    cameraParam.setPreviewFormat(ImageFormat.NV21);
    cameraParam.setPreviewSize(preferredWidth, preferredHeight);
    cameraParam.setPreviewFpsRange(preferredFPS, preferredFPS);
    cameraParam.setFocusMode(Camera.Parameters.FOCUS_MODE_MACRO);
    if (cameraParam.isVideoStabilizationSupported()) {
      cameraParam.setVideoStabilization(false);
    }
    if (cameraParam.isAutoWhiteBalanceLockSupported()) {
      cameraParam.setAutoWhiteBalanceLock(false);
    }

    camera.setParameters(cameraParam);

    camera.setPreviewCallback(new PreviewCallback() {
      @Override
      public void onPreviewFrame(byte[] data, Camera camera) {
        int Height = camera.getParameters().getPreviewSize().height;
        int Width = camera.getParameters().getPreviewSize().width;

        // calling C++ function via JNI interface
        processFrameData(Width, Height, data);
      }
    });

    camera.startPreview();
  }

  public void AndroidThunkJava_stopCamera()
  {
    if (camera != null)
    {
      camera.stopPreview();
      camera.release();
      camera = null;
    }
  }
</insert>
</gameActivityClassAdditions>

<gameActivityOnPauseAdditions>
<insert>
  AndroidThunkJava_stopCamera();
</insert>
</gameActivityOnPauseAdditions>

<gameActivityOnResumeAdditions>
<insert>  
  AndroidThunkJava_startCamera();
</insert>
</gameActivityOnResumeAdditions>

The problem 问题

The camera works fine every second time. 相机每秒钟工作正常。 That means: 这意味着:

I open the app, camera is working. 我打开应用程序,相机正在工作。 I pushed the home button (which triggers onPause() method), then switch back to the app (triggers onResume() method). 我按下了主页按钮(触发onPause()方法),然后切换回应用程序(触发onResume()方法)。 Pushed the home button again, and then switched back - camera works. 再按一次主页按钮,然后再切换回-相机可以工作。 And so on, the camera works every second time. 依此类推,摄像头每秒钟工作一次。

Anybody have any idea about this issue? 有人对此有任何想法吗? Is that connected to the fact that android.hardware.Camera is deprecated? 是否与android.hardware.Camera已过时的事实有关? I'm using API version 19, so it is not possible to use newer android.hardware.camera2 . 我正在使用API​​版本19,因此无法使用较新的android.hardware.camera2

Here is my onStop and onResume methods. 这是我的onStop和onResume方法。 I'm not using onPause. 我没有使用onPause。 And it works perfectly: 它完美地工作:

  @Override
    protected void onResume() {
        super.onResume();
        if (mCamera == null) {
            restartPreview();
        }
    }
    @Override
    public void onStop() {

        // stop the preview
        if (mCamera != null) {
            stopCameraPreview();
            mCamera.release();
            mCamera = null;
        }

        super.onStop();
    }

 private void restartPreview() {
        if (mCamera != null) {
            stopCameraPreview();
            mCamera.release();
            mCamera = null;
        }



            getCamera(mCameraID);
            startCameraPreview();
        }

private void startCameraPreview() {


        try {
            mCamera.setPreviewDisplay(mSurfaceHolder);
            mCamera.startPreview();

            setSafeToTakePhoto(true);
            setCameraFocusReady(true);
        } catch (IOException e) {
            Log.d("st", "Can't start camera preview due to IOException " + e);
            e.printStackTrace();
        }
    }

 private void stopCameraPreview() {
        setSafeToTakePhoto(false);
        setCameraFocusReady(false);

        // Nulls out callbacks, stops face detection
        mCamera.stopPreview();
        mPreviewView.setCamera(null);
    }

maybe some implementations not equals yours but i think it is help you. 也许某些实现不等于您的实现,但我认为这对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM