Quantcast
Channel: Active questions tagged crash - Stack Overflow
Viewing all articles
Browse latest Browse all 7190

OpenCV application - Android Camera crashes after 10 seconds

$
0
0

Background

In this application, I was able to launch my camera preview that was developed in Android Studio with an aim to incorporate OpenCV into it. However, I face an error; the camera preview only lasts around 10 seconds until it crashes and the app shuts down. Would there be any way to resolve this? Here is my code;

MainActivity.java

package com.example.cv;import androidx.annotation.NonNull;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.opengl.Matrix;import android.os.Bundle;import android.util.Log;import android.view.SurfaceView;import android.widget.Toast;import org.opencv.android.BaseLoaderCallback;import org.opencv.android.CameraBridgeViewBase;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 static final int MY_CAMERA_REQUEST_CODE = 100;    BaseLoaderCallback baseLoaderCallback = new BaseLoaderCallback(MainActivity.this) {        @Override        public void onManagerConnected(int status)        {            if (status == BaseLoaderCallback.SUCCESS) {                javaCameraView.enableView();            } else {                super.onManagerConnected(status);            }        }    };    static    {        if (OpenCVLoader.initDebug())        {            Log.d(TAG, "OpenCV is Configured or Connected successfully.");        }        else        {            Log.d(TAG, "OpenCV not Working or Loaded.");        }    }    @Override    protected void onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        javaCameraView = (JavaCameraView) findViewById(R.id.my_camera_view);        if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)                == PackageManager.PERMISSION_GRANTED)  {            Log.d(TAG, "Permissions granted");            javaCameraView.setCameraPermissionGranted();            javaCameraView.setCameraIndex(CameraBridgeViewBase.CAMERA_ID_BACK);            javaCameraView.setVisibility(CameraBridgeViewBase.VISIBLE);            javaCameraView.setCvCameraViewListener(this);        } else {            Log.d(TAG, "Permission prompt");            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, MY_CAMERA_REQUEST_CODE);        }    }    @Override    public void onCameraViewStarted(int width, int height)    {        mRGBA = new Mat(height, width, CvType.CV_8UC4);    }    @Override    public void onCameraViewStopped()    {        mRGBA.release();    }    @Override    public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame)    {        mRGBA = inputFrame.rgba();        mRGBAT = mRGBA.t();        Core.flip(mRGBA.t(), mRGBAT, 1);        Imgproc.resize(mRGBAT, mRGBAT, mRGBA.size());        return mRGBAT;    }    @Override    public void onPointerCaptureChanged(boolean hasCapture) {    }    @Override    protected void onDestroy() {        super.onDestroy();        if (javaCameraView != null)        {            javaCameraView.disableView();        }    }    @Override    protected void onPause() {        super.onPause();        if (javaCameraView != null)        {            javaCameraView.disableView();        }    }    @Override    protected void onResume() {        super.onResume();        if (OpenCVLoader.initDebug())        {            Log.d(TAG, "OpenCV is Configured or Connected successfully.");            baseLoaderCallback.onManagerConnected(BaseLoaderCallback.SUCCESS);        }        else        {            Log.d(TAG, "OpenCV not Working or Loaded.");            OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION, this, baseLoaderCallback);        }    }    @Override    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {        super.onRequestPermissionsResult(requestCode, permissions, grantResults);        if (requestCode == MY_CAMERA_REQUEST_CODE) {            // camera can be turned on            Toast.makeText(this, "camera permission granted", Toast.LENGTH_LONG).show();            javaCameraView.setCameraPermissionGranted();            javaCameraView.setCameraIndex(CameraBridgeViewBase.CAMERA_ID_FRONT);            javaCameraView.setVisibility(CameraBridgeViewBase.VISIBLE);            javaCameraView.setCvCameraViewListener(this);        } else {            //camera will stay off            Toast.makeText(this, "camera permission denied", Toast.LENGTH_LONG).show();        }    }}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.cv"><uses-permission android:name="android.permission.CAMERA"/><uses-feature android:name="android.hardware.camera"/><uses-feature android:name="android.hardware.camera.autofocus"/><uses-feature android:name="android.hardware.camera.front"/><uses-feature android:name="android.hardware.camera.front.autofocus"/><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>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="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/my_camera_view"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        /></RelativeLayout>

Viewing all articles
Browse latest Browse all 7190

Trending Articles