Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Opencv for android ignoring inputFrame

I'm trying to create a simple helloworld in android, but not sure why it's not displayed what I return from onCameraFrame.

I'm using opencv 3.4.1 on android 8.0.0. This dosen't draw anything:

@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame)
{
    mRgba = inputFrame.rgba();
    Imgproc.circle (
            mRgba,                 //Matrix obj of the image
            new Point(mWidth * 0.5, mHeight * 0.5),    //Center of the circle
            10,                    //Radius
            new Scalar(0, 0, 255),  //Scalar object for color
            10                     //Thickness of the circle
    );
     Log.i(TAG, "Called.");
    return mRgba;

But Called appears in the prompt. And neither this is working:

@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame)
{
    return inputFrame.gray();
}

The image is still colored. I've searched a lot and tryied different solutions, and none of them worked :(

Opencv for android ignoring inputFrame

I'm trying to create a simple helloworld in android, but not sure why it's not displayed what I return from onCameraFrame.

I'm using opencv 3.4.1 on android 8.0.0. This dosen't draw anything:

@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame)
{
    mRgba = inputFrame.rgba();
    Imgproc.circle (
            mRgba,                 //Matrix obj of the image
            new Point(mWidth * 0.5, mHeight * 0.5),    //Center of the circle
            10,                    //Radius
            new Scalar(0, 0, 255),  //Scalar object for color
            10                     //Thickness of the circle
    );
     Log.i(TAG, "Called.");
    return mRgba;

But Called appears in the prompt. And neither this is working:

@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame)
{
    return inputFrame.gray();
}

The image is still colored. I've searched a lot and tryied different solutions, and none of them worked :(

Here is the complete class code:

public class MainActivity extends AppCompatActivity implements CameraBridgeViewBase.CvCameraViewListener2
{

    private static final String TAG = MainActivity.class.getCanonicalName();
    private CameraBridgeViewBase mOpenCvCameraView;
    private static final Scalar FACE_RECT_COLOR = new Scalar(0, 255, 0, 255);
    private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this)
    {
        @Override
        public void onManagerConnected(int status)
        {
            switch (status)
            {
                case LoaderCallbackInterface.SUCCESS:
                {
                    Log.i(TAG, "OpenCV loaded successfully");
                    try
                    {
                        initializeOpenCVDependencies();
                    } catch (IOException e)
                    {
                        e.printStackTrace();
                    }
                }
                break;
                default:
                {
                    super.onManagerConnected(status);
                }
                break;
            }
        }
    };
    private Mat mRgba;
    private Mat mGray;
    private int mWidth;
    private int mHeight;

    private void initializeOpenCVDependencies() throws IOException
    {

        mOpenCvCameraView.enableView();

    }


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        if (ContextCompat.checkSelfPermission(this,
                                              Manifest.permission.CAMERA)
                == PackageManager.PERMISSION_DENIED)
        {
            Log.i(TAG, "Requesting camera permissions because are: " +
                    ContextCompat.checkSelfPermission(this,
                                                      Manifest.permission.CAMERA));
            requestPermissions(new String[]{Manifest.permission.CAMERA}, 13);
            return;
        }

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {

            }
        });

        mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.tutorial1_activity_java_surface_view);
        mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
        mOpenCvCameraView.setCvCameraViewListener(this);

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults)
    {

        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED)
        {

            Snackbar.make(findViewById(R.id.container), "Permission Granted",
                          Snackbar.LENGTH_LONG).show();

        }
        else
        {

            Snackbar.make(findViewById(R.id.container), "Permission denied",
                          Snackbar.LENGTH_LONG).show();

        }
    }

    @Override
    public void onResume()
    {

        super.onResume();
        if (!OpenCVLoader.initDebug())
        {
            Log.d(TAG, "Internal OpenCV library not found. Using OpenCV Manager for initialization");
            OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION, this, mLoaderCallback);
        }
        else
        {
            Log.d(TAG, "OpenCV library found inside package. Using it!");
            mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
        }
    }

    @Override
    public void onPause()
    {

        super.onPause();
        if (mOpenCvCameraView != null)
        {
            mOpenCvCameraView.disableView();
        }
    }

    @Override
    public void onCameraViewStarted(int width, int height)
    {
        mGray = new Mat(height, width, CvType.CV_8UC4);
        mRgba = new Mat();
        mWidth = width;
        mHeight = height;
    }

    @Override
    public void onCameraViewStopped()
    {
        mGray.release();
        mRgba.release();
    }

    @Override
    public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame)
    {
        return inputFrame.gray();
    }
}