Black Screen Issue in Android App Using OpenCV

Hello community,

I’m developing a surveillance application using OpenCV on Android, and I’m encountering an issue where the screen stays black when trying to display the camera preview on both a physical device (Konka N15) and an emulator. OpenCV seems to load correctly, but no image is shown on the screen.

Here’s a snippet of the code that sets up and handles the camera view:

class MainActivity : ComponentActivity(), CvCameraViewListener2 {
private var cameraView: CameraBridgeViewBase? = null;

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState);
    window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    if (!OpenCVLoader.initDebug()) {
        Log.e("OpenCV", "Unable to load OpenCV!");
    } else {
        Log.d("OpenCV", "OpenCV loaded successfully!");
    }

    setContent {
        MaterialTheme {
            Surface(
                modifier = Modifier.fillMaxSize(),
                color = MaterialTheme.colorScheme.background
            ) {
                AndroidView(
                    factory = { context ->
                        JavaCamera2View(context, -1).apply {
                            setCvCameraViewListener(this@MainActivity);
                            layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                            visibility = android.view.View.VISIBLE;
                            cameraView = this;
                        }
                    },
                    modifier = Modifier.fillMaxSize()
                );
            }
        };
    }

    checkCameraPermission();
}

override fun onCameraFrame(inputFrame: CvCameraViewFrame): Mat {
    Log.d("CameraFrame", "Received a frame");
    return inputFrame.rgba();
}

// Additional methods...

}


During execution, several logs are recorded that I am not sure if they are related to the problem:


2024-04-22 18:49:14.636 mara_vigilanci com.example.camara_vigilancia W ClassLoaderContext classpath size mismatch. expected=0, found=1
2024-04-22 18:49:14.719 linker com.example.camara_vigilancia W Warning: "/data/app/~~Zvykmk4-FMii-EuRyKO8nA==/com.example.camara_vigilancia-aLuC20vWP-EbTNTvVuXPYg==/base.apk!/lib/arm64-v8a/libc++_shared.so" unused DT entry: unknown processor-specific (type 0x70000001 arg 0x0)

Could these log messages be indicating an incorrect configuration or some issue that might be affecting the camera rendering in the app? Are there specific settings in the OpenCV or Android environment that I should check to resolve this black screen issue?

Any guidance or suggestions will be greatly appreciated.

Below is a more detailed explanation of what’s in my MainActivity.kt, including the imports:

// Imports needed for the application
import org.opencv.android.CameraBridgeViewBase
import org.opencv.android.OpenCVLoader
import org.opencv.core.Mat
import org.opencv.android.BaseLoaderCallback
import org.opencv.android.LoaderCallbackInterface
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.ui.Modifier
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.ui.platform.AndroidView
import android.util.Log
import android.view.WindowManager
import android.view.ViewGroup

// Main class of the Android application that uses OpenCV for camera view.
class MainActivity : ComponentActivity(), CvCameraViewListener2 {
// Variable to keep a reference to the camera view.
private var cameraView: CameraBridgeViewBase? = null;

// Method called when creating the activity. Here the user interface is set up and OpenCV is initialized.
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState);
    // Keep the screen on while the app is active.
    window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    // Try to load OpenCV statically and log whether it is successful or not.
    if (!OpenCVLoader.initDebug()) {
        Log.e("OpenCV", "Unable to load OpenCV!");
    } else {
        Log.d("OpenCV", "OpenCV loaded successfully!");
    }

    // Set the content of the user interface using Jetpack Compose.
    setContent {
        MaterialTheme {
            Surface(
                modifier = Modifier.fillMaxSize(),
                color = MaterialTheme.colorScheme.background
            ) {
                // Create and configure the camera view.
                AndroidView(
                    factory = { context ->
                        // Initialize the camera view with context and no specific camera index.
                        JavaCamera2View(context, -1).apply {
                            setCvCameraViewListener(this@MainActivity);
                            layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                            visibility = android.view.View.VISIBLE;
                            // Save the reference to the camera view.
                            cameraView = this;
                        }
                    },
                    modifier = Modifier.fillMaxSize()
                );
            }
        };
    }

    // Check camera permissions before enabling the view.
    checkCameraPermission();
}

// Method called for each received camera frame.
override fun onCameraFrame(inputFrame: CvCameraViewFrame): Mat {
    Log.d("CameraFrame", "Received a frame");
    // Return the processed frame.
    return inputFrame.rgba();
}

}