Ask Your Question

Revision history [back]

I had the same problem, v4.1.0 worked but higher versions show a black screen. The culprit was that the CameraBridgeViewBase has a new method setCameraPermissionGranted() that needs to be called when permissions have been granted in order to allow the camera preview. Without calling this method, only the black screen is visible.

private CameraBridgeViewBase cameraBridgeViewBase;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Permissions for Android 6+
    ActivityCompat.requestPermissions(MainActivity.this,
            new String[]{Manifest.permission.CAMERA},
            1);

    // rest of the code...
    cameraBridgeViewBase = (CameraBridgeViewBase) findViewById(R.id.main_surface);
    cameraBridgeViewBase.setVisibility(SurfaceView.VISIBLE);
    cameraBridgeViewBase.setCameraPermissionGranted();
    cameraBridgeViewBase.setCvCameraViewListener(this);
}

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], 
                                       int[] grantResults) {
    switch (requestCode) {
        case 1: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                cameraBridgeViewBase.setCameraPermissionGranted();  // <------ THIS!!!
            } else {
                // permission denied
            }
            return;
        }
    }
}