I imported opencv in android studio and crashed after calling inputFrame.rgba()

asked 2018-03-23 04:16:13 -0600

Here is my code:

public class MainActivity extends AppCompatActivity implements CameraBridgeViewBase.CvCameraViewListener2{
    private CameraBridgeViewBase mOpenCvCameraView;
    public static final String TAG = "MainActivity";
    static {
        System.loadLibrary("OpenCV");
    }
    private Mat mRgba;
    private Mat mFlipRgba;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        setContentView(R.layout.activity_main);
        mOpenCvCameraView = findViewById(R.id.HelloOpenCvView);
        mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
        mOpenCvCameraView.setCvCameraViewListener(this);
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, 1);
        }
       // mOpenCvCameraView.enableView();
    }
    BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
        @Override
        public void onManagerConnected(int status){
            switch (status) {
                case LoaderCallbackInterface.SUCCESS:
                    Log.i(TAG,"OpenCV loaded successfully");
                    mOpenCvCameraView.enableView();
                    break;
                default:
                    break;
            }
        }
    };
    @Override
    protected void onResume() {
        super.onResume();
        //OpenCVLoader.initDebug();
        if (!OpenCVLoader.initDebug()) {
            Log.d(TAG,"OpenCV library not found!");
        } else {
            Log.d(TAG, "OpenCV library found inside package. Using it!");
        mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
    }
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    if (requestCode == 1 && grantResults[0] == PackageManager.PERMISSION_DENIED) {
        return;
    }
}

@Override
protected void onPause() {
    super.onPause();
    if (mOpenCvCameraView != null)
        mOpenCvCameraView.disableView();
}

public void onDestroy() {
    super.onDestroy();
    if (mOpenCvCameraView != null)
        mOpenCvCameraView.disableView();
}

@Override
public void onCameraViewStarted(int width, int height) {
    Log.d(TAG, "onCameraViewStarted");
    mRgba = new Mat();
    mFlipRgba = new Mat();
}

@Override
public void onCameraViewStopped() {
    Log.d(TAG, "onCameraViewStopped");
    mRgba.release();
}

@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
    Log.d(TAG,"onCameraFrame");
    mRgba = inputFrame.rgba();  //argb rgb
    //Imgproc.COLOR_RGBA2RGB
   // Imgproc.cvtColor(mRgba,mFlipRgba,Imgproc.COLOR_RGBA2RGB,0);
    return inputFrame.gray();
}}

and AndroidManifest.xml: <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> Here is exception:

Failed to dispatch window animation state change.
                                                                 android.os.DeadObjectException
                                                                  at android.os.BinderProxy.transactNative(Native Method)
                                                                  at android.os.BinderProxy.transact(Binder.java:503)
                                                                  at android.view.IWindow$Stub$Proxy.onAnimationStopped(IWindow.java:534)
                                                                  at com.android.server.wm.WindowAnimator.updateWindowsLocked(WindowAnimator.java:286)
                                                                  at com.android.server.wm.WindowAnimator.animateLocked(WindowAnimator.java:678)
                                                                  at com.android.server.wm.WindowAnimator.access$000(WindowAnimator.java:53)
                                                                  at com.android.server.wm.WindowAnimator$1.doFrame(WindowAnimator.java:123)
                                                                  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:856)
                                                                  at android.view.Choreographer.doCallbacks(Choreographer.java:670)
                                                                  at android.view.Choreographer.doFrame(Choreographer.java:603)
                                                                  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
                                                                  at android.os.Handler.handleCallback(Handler.java:739)
                                                                  at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                  at android.os.Looper.loop(Looper.java:148)
                                                                  at android.os.HandlerThread.run(HandlerThread.java:61)
                                                                  at com.android.server.ServiceThread.run(ServiceThread.java:46)
**thank you very much for your help**
edit retag flag offensive close merge delete