为什么textureView在Android API级别23的相机应用程序上显示黑屏?

时间:2016-08-01 15:01:49

标签: android android-camera android-textureview

我正在使用TextureView类开发相机应用程序。

但是在创建MainActivity时会显示黑屏。

我已经在Github上检查了一些源代码,但它们也有同样的问题。

//MainActivity
private CaptureFragment     mCaptureFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
    //...
    mCaptureFragment = CaptureFragment.newInstance();
        getFragmentManager().beginTransaction()
                .replace(R.id.recordVideoContent, mCaptureFragment)
                .commit();
    //...
}

//CaptureFragment class
public class CaptureFragment Fragment {
    private AutoFitTextureView mCameraLayout;
}
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    mCameraLayout = (AutoFitTextureView)               
                                 view.findViewById(getTextureResource());
}
@Override
public void onResume() {
    super.onResume();
    if (mCameraLayout.isAvailable()) {
        openCamera(mCameraLayout.getWidth(), mCameraLayout.getHeight());
    } else {
        mCameraLayout.setSurfaceTextureListener(mSurfaceTextureListener);
    }
}

private TextureView.SurfaceTextureListener mSurfaceTextureListener = new 
                                  TextureView.SurfaceTextureListener() {
    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, 
                        int height) {
        openCamera(width, height);
    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int 
                                                      width, int height) {
        configureTransform(width, height);
    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
        return true;
    }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surface) {
        Log.d("SurfaceTextureUpdated", "");
    }
};

private void openCamera(int width, int height, int mode) {
    if (!CameraUtil.hasPermissionsGranted(getActivity(), 
                        Camera2PermissionDialog.VIDEO_PERMISSIONS)) {
        requestVideoPermissions();
        return;
    }
    CameraManager cameraManager = (CameraManager) 
              activity.getSystemService(Context.CAMERA_SERVICE);
    try {

    } catch() {
        //...
        cameraManager.openCamera(cameraId, mStateCallback, null);
        //...
    }
}

private CameraDevice.StateCallback mStateCallback = new CameraDevice.StateCallback() {
    @Override
    public void onOpened(@NonNull CameraDevice camera) {
        mCameraDevice = camera;

        startPreview();
        mCameraOpenCloseLock.release();
        if (null != mCameraLayout) {
            configureTransform(mCameraLayout.getWidth(), mCameraLayout.getHeight());
        }
    }

    @Override
    public void onDisconnected(@NonNull CameraDevice camera) {
        mCameraOpenCloseLock.release();
        camera.close();
        mCameraDevice = null;
    }

    @Override
    public void onError(@NonNull CameraDevice camera, int error) {
        mCameraOpenCloseLock.release();
        camera.close();
        mCameraDevice = null;
        Activity activity = getActivity();
        if (null != activity) {
            activity.finish();
        }
    }
};

0 个答案:

没有答案