Basic camera initialization gets black screen

Hi,

This is a basic app in order to initialize camera and apply Canny edges. Everything is ok but I only get a black screen.
Any help would be appreciated.

MainActivity

package com.example.opencvcamera;

import android.os.Bundle;
import com.google.android.material.snackbar.Snackbar;
import androidx.appcompat.app.AppCompatActivity;
import android.util.Log;
import android.view.SurfaceView;
import androidx.navigation.ui.AppBarConfiguration;
import com.example.opencvcamera.databinding.ActivityMainBinding;
import android.view.Menu;
import android.view.MenuItem;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.OpenCVLoader;
import org.opencv.core.Mat;
import org.opencv.imgproc.Imgproc;

public class MainActivity extends AppCompatActivity implements CameraBridgeViewBase.CvCameraViewListener2{

private AppBarConfiguration appBarConfiguration;
private ActivityMainBinding binding;

private CameraBridgeViewBase cameraView = null;
private static boolean initOpenCV = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if (!OpenCVLoader.initDebug()) {
        Log.e(this.getClass().getSimpleName(), "  OpenCVLoader.initDebug(), not working.");
    } else {
        Log.d(this.getClass().getSimpleName(), "  OpenCVLoader.initDebug(), working.");
        initOpenCV = true;
    }
    cameraView = (CameraBridgeViewBase) findViewById(R.id.cameraview);
    cameraView.setVisibility(SurfaceView.VISIBLE);
    cameraView.setCvCameraViewListener(this);

}


@Override
protected void onResume() {
    super.onResume();
    if (initOpenCV) { cameraView.enableView(); }
}

@Override
public void onPause() {
    super.onPause();

    // Release the camera.
    if (cameraView != null) {
        cameraView.disableView();
        cameraView = null;
    }
}

@Override
public void onCameraViewStarted(int width, int height) { }

@Override
public void onCameraViewStopped() { }



@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {

    Mat src = inputFrame.gray(); // convertir a escala de grises
    Mat cannyEdges = new Mat();  // objeto para almacenar el resultado

    // aplicar el algoritmo canny para detectar los bordes
    Imgproc.Canny(src, cannyEdges, 10, 100);

    // devolver el objeto Mat procesado
    return cannyEdges;
}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

Layout

<?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android=“http://schemas.android.com/apk/res/android
xmlns:app=“http://schemas.android.com/apk/res-auto
xmlns:opencv=“http://schemas.android.com/apk/res-auto
xmlns:tools=“http://schemas.android.com/tools
android:layout_width=“match_parent”
android:layout_height=“match_parent”
tools:context=“.MainActivity”>

<org.opencv.android.JavaCameraView
    android:id="@+id/cameraview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:visibility="gone"
    opencv:camera_id="any"
    opencv:show_fps="true"
    tools:ignore="MissingConstraints" />

</androidx.constraintlayout.widget.ConstraintLayout>

Hi,
In my case, I solved adding the onPause() , onResume() and getCameraViewList() methods.
uridium, you can try removing cameraView = null; from onPause() and adding the following:

> @Override
>     protected List<? extends CameraBridgeViewBase> getCameraViewList() {
>         return Collections.singletonList(mOpenCvCameraView);
>     }

Regards,
infinito1971