Ask Your Question
0

Android OpenCV + camera is just showing black screen

asked 2020-04-22 19:24:55 -0600

Mark Mayo gravatar image

updated 2020-04-23 16:00:26 -0600

I've spent days (well, nights) trying to work this out. So many examples online are for different versions of Android Studio, different versions of Android, different versions of OpenCV and I can't get any of them to the final 'working' stage.

This example (based on a youtube tutorial, I got to the point where I needed permissions. That's fine, I added that in and a check for them, and it pops up asking the user for camera permissions. But the screen stays blank. I've put in logcat debug, all the right methods seem to be getting called. Would appreciate any assistance.

EDIT: I've found other 'working' examples, copied them over and run them too, and black screen still. I tried shrinking the cameraview to half view, and indeed, it's the camera view going black. Cannot figure it out.

Code:

Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.mytestopencvapp" >

    <uses-permission android:name="android.permission.CAMERA"/>


    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <activity android:name=".MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

MainActivity.java

package com.example.mytestopencvapp;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceView;

import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.JavaCamera2View;
import org.opencv.android.JavaCameraView;
import org.opencv.android.OpenCVLoader;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.imgproc.Imgproc;

public class MainActivity extends AppCompatActivity implements CameraBridgeViewBase.CvCameraViewListener2 {

    private static String TAG = "MainActivity";

    JavaCameraView javaCameraView;
    Mat mRGBA, mRGBAT;

    private final int PERMISSIONS_READ_CAMERA=1;

    BaseLoaderCallback baseLoaderCallback = new BaseLoaderCallback(MainActivity.this) {
        @Override
        public void onManagerConnected(int status) {
            Log.d(TAG, "callbacksuccess");
            switch (status)
            {
                case BaseLoaderCallback.SUCCESS:
                {
                    Log.d(TAG, "case success");
                    javaCameraView.enableView();
                    break;
                }
                default:
                {
                    Log.d(TAG, "case default");
                    super.onManagerConnected(status);
                    break;
                }

            }

        }
    };

    static
    {
        if (OpenCVLoader.initDebug())
        {
            Log.d(TAG, "OpenCV is intialised");
        }
        else
        {
            Log.d(TAG, "OpenCV is not initialised");
        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate");
        setContentView(R.layout.activity_main);
        javaCameraView = (JavaCameraView)findViewById(R.id.my_camera_view);
        javaCameraView.setVisibility(SurfaceView.VISIBLE);
        javaCameraView.setCvCameraViewListener(MainActivity.this);

// Here, thisActivity is the current activity
        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.CAMERA)
                != PackageManager.PERMISSION_GRANTED) {

            // Permission is not granted
            // Should we show an explanation?
            if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                    Manifest.permission.CAMERA)) {
                // Show an explanation to the user *asynchronously* -- don't block
                // this thread waiting for the user's response! After the user
                // sees the explanation, try again to request the permission.
            } else {
                // No explanation needed, we can request the permission.
                ActivityCompat.requestPermissions ...
(more)
edit retag flag offensive close merge delete

4 answers

Sort by » oldest newest most voted
0

answered 2020-11-20 01:41:09 -0600

dkurt gravatar image

Please try to add one more method

@Override
protected List<? extends CameraBridgeViewBase> getCameraViewList() {
    return Collections.singletonList(cameraBridgeViewBase);
}
edit flag offensive delete link more
0

answered 2020-07-22 09:40:28 -0600

ph4r05 gravatar image

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;
        }
    }
}
edit flag offensive delete link more

Comments

1

Thanks, this is the right answer, camera preview working good in 3.x.x versions, but 4.x.x requires the above mentioned changes. If I had enough points I would upvote this.

AtulVasudev gravatar imageAtulVasudev ( 2020-10-30 14:11:31 -0600 )edit
0

answered 2020-06-11 02:31:16 -0600

I've tried it for a week,It's still a black screen, too Does anyone know what's going on ?

edit flag offensive delete link more

Comments

This code solves my problem:

private void getPermission() {
    if (Build.VERSION.SDK_INT > 22) {
        if (ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
        } else {
            Log.i("MainActivity", "get permission");
        }
    } else {
        Log.i("MainActivity", "no need");
    }
}
softwbc gravatar imagesoftwbc ( 2020-07-09 10:02:39 -0600 )edit
0

answered 2020-04-24 15:33:25 -0600

LiorA gravatar image

What are you trying to do ?

You tried firebase ?

edit flag offensive delete link more

Comments

Trying to just get any opencv example with a camera to load. I've built a separate video modification tool in python, now trying to build the equivalent in Android. Haven't tried firebase, looks interesting, but this is opencv's forums and I'm trying to get one of the common examples with opencv working with Android :/

Mark Mayo gravatar imageMark Mayo ( 2020-04-24 15:43:15 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2020-04-22 19:24:55 -0600

Seen: 6,379 times

Last updated: Nov 20 '20