1

I've trying to access the android camera by using OpenCV. I had success in creating a project and it run without any problem. I tried to use a code from the internet and check it in my project. But, the app stoped. It's seems to be a RuntimeException problem but i don't know how to solve it. That's my code, that i copied from this website http://people.oregonstate.edu/~robinsti/CS_496/Tutorial/:

mainActivity.java

public class MainActivity extends AppCompatActivity implements View.OnTouchListener, CameraBridgeViewBase.CvCameraViewListener2{

private CameraBridgeViewBase mOpenCvCameraView;
private Mat mRgba;

@Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
    super.onCreate(savedInstanceState, persistentState);
    setContentView(R.layout.activity_main);

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.Surface);
    mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
    //mOpenCvCameraView.setMaxFrameSize(176, 152);
    mOpenCvCameraView.setCvCameraViewListener(this);
}

private BaseLoaderCallback mLoaderCallBack = new BaseLoaderCallback(this) {
    @Override
    public void onManagerConnected(int status) {
        super.onManagerConnected(status);
        switch (status) {
            case LoaderCallbackInterface.SUCCESS: {
                mOpenCvCameraView.enableView();
                mOpenCvCameraView.setOnTouchListener(MainActivity.this);
            } break;
            default: {
                super.onManagerConnected(status);
            } break;
        }
    }
};

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

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

}


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

    if(!OpenCVLoader.initDebug()) {
        OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_1_0,this, mLoaderCallBack);
    }
    else {
        mLoaderCallBack.onManagerConnected(LoaderCallbackInterface.SUCCESS);
    }
}

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

}

@Override
public void onCameraViewStopped() {

}

@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
    mRgba = inputFrame.rgba();
    return mRgba;
}

@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
    return false;
}

}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
xmlns:opencv="http://schemas.android.com/apk/res-auto"
tools:context="com.tech.te.smartrotate.MainActivity">

<org.opencv.android.JavaCameraView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/Surface" />

</RelativeLayout>

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="                 ">
<uses-permission android:name="android.permission.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:supportsRtl="true"
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen">        <!--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>

Logcat:

07-29 15:54:44.066 31188-31188/com.tech.te.app W/art: Failed to find OatDexFile for DexFile /data/data/com.tech.te.app/files/instant-run/dex/slice-slice_2-classes.dex ( canonical path /data/data/com.tech.te.app/files/instant-run/dex/slice-slice_2-classes.dex) with checksum 0x7f90e189 in OatFile /data/data/com.tech.te.app/cache/slice-slice_2-classes.dex
07-29 15:54:45.836 31188-31195/com.tech.te.app W/art: Suspending all threads took: 19.602ms
07-29 15:54:45.946 31188-31188/com.tech.te.app D/DisplayManager: DisplayManager()
07-29 15:54:46.376 31188-31188/com.tech.te.app W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
07-29 15:54:46.596 31188-31188/com.tech.te.app E/VdcInflateDelegate: Exception while inflating 
                                                                             java.lang.RuntimeException: Failed to resolve attribute at index 1
                                                                                 at android.content.res.TypedArray.twGetColorStateList(TypedArray.java:437)
                                                                                 at android.content.res.TypedArray.getColorStateList(TypedArray.java:419)
                                                                                 at android.support.graphics.drawable.VectorDrawableCompat.updateStateFromTypedArray(VectorDrawableCompat.java:518)
                                                                                 at android.support.graphics.drawable.VectorDrawableCompat.inflate(VectorDrawableCompat.java:472)
                                                                                 at android.support.graphics.drawable.VectorDrawableCompat.createFromXmlInner(VectorDrawableCompat.java:436)
                                                                                 at android.support.v7.widget.AppCompatDrawableManager$VdcInflateDelegate.createFromXmlInner(AppCompatDrawableManager.java:708)
                                                                                 at android.support.v7.widget.AppCompatDrawableManager.loadDrawableFromDelegates(AppCompatDrawableManager.java:348)
                                                                                 at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:188)
                                                                                 at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:181)
                                                                                 at android.support.v7.widget.AppCompatDrawableManager.checkVectorDrawableSetup(AppCompatDrawableManager.java:689)
                                                                                 at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:186)
                                                                                 at android.support.v7.widget.TintTypedArray.getDrawableIfKnown(TintTypedArray.java:77)
                                                                                 at android.support.v7.app.AppCompatDelegateImplBase.(AppCompatDelegateImplBase.java:83)
                                                                                 at android.support.v7.app.AppCompatDelegateImplV7.(AppCompatDelegateImplV7.java:146)
                                                                                 at android.support.v7.app.AppCompatDelegateImplV11.(AppCompatDelegateImplV11.java:28)
                                                                                 at android.support.v7.app.AppCompatDelegateImplV14.(AppCompatDelegateImplV14.java:41)
                                                                                 at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:193)
                                                                                 at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:173)
                                                                                 at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:511)
                                                                                 at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:71)
                                                                                 at android.app.Activity.performCreate(Activity.java:6112)
                                                                                 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1117)
                                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2609)
                                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2721)
                                                                                 at android.app.ActivityThread.access$900(ActivityThread.java:168)
                                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                 at android.os.Looper.loop(Looper.java:135)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5753)
                                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                                 at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
07-29 15:54:46.596 31188-31202/com.tech.te.app I/art: Background sticky concurrent mark sweep GC freed 6652(1091KB) AllocSpace objects, 1(20KB) LOS objects, 14% free, 5MB/6MB, paused 36.125ms total 277.107ms
07-29 15:54:46.616 31188-31202/com.tech.te.app I/art: Background sticky concurrent mark sweep GC freed 274(43KB) AllocSpace objects, 0(0B) LOS objects, 14% free, 5MB/6MB, paused 7.366ms total 16.707ms
07-29 15:54:46.706 31188-31188/com.tech.te.app D/AndroidRuntime: Shutting down VM
07-29 15:54:46.716 31188-31188/com.tech.te.app E/AndroidRuntime: FATAL EXCEPTION: main
                                                                         Process: com.tech.te.app, PID: 31188
                                                                         java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tech.te.app/com.tech.te.app.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2656)
                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2721)
                                                                             at android.app.ActivityThread.access$900(ActivityThread.java:168)
                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)
                                                                             at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                             at android.os.Looper.loop(Looper.java:135)
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5753)
                                                                             at java.lang.reflect.Method.invoke(Native Method)
                                                                             at java.lang.reflect.Method.invoke(Method.java:372)
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
                                                                          Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
                                                                             at android.support.v7.app.AppCompatDelegateImplV7.createSubDecor(AppCompatDelegateImplV7.java:343)
                                                                             at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:312)
                                                                             at android.support.v7.app.AppCompatDelegateImplV7.onPostCreate(AppCompatDelegateImplV7.java:167)
                                                                             at android.support.v7.app.AppCompatActivity.onPostCreate(AppCompatActivity.java:98)
                                                                             at android.app.Instrumentation.callActivityOnPostCreate(Instrumentation.java:1198)
                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2638)
                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2721) 
                                                                             at android.app.ActivityThread.access$900(ActivityThread.java:168) 
                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393) 
                                                                             at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                             at android.os.Looper.loop(Looper.java:135) 
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5753) 
                                                                             at java.lang.reflect.Method.invoke(Native Method) 
                                                                             at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405) 
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200) 
07-29 15:54:50.366 31188-31188/com.tech.te.app I/Process: Sending signal. PID: 31188 SIG: 9

Thanks a lot.

Leonid
  • 21
  • 7
  • `Unable to start activity ComponentInfo{com.tech.te.app/com.tech.te.app.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.` – Phantômaxx Jul 29 '16 at 13:05
  • Thanks. Now the app don't crash but i don't see the camera. How should i solve this problem? – Leonid Jul 29 '16 at 13:16

1 Answers1

0

You are loading OpenCV on your OnDestroy, you should not do that.

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

    if(!OpenCVLoader.initDebug()) {
        OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_1_0,this, mLoaderCallBack);
    }
    else {
        mLoaderCallBack.onManagerConnected(LoaderCallbackInterface.SUCCESS);
    }
}

Also, you should not use initDebug and initAsync at the same time.

initDebug is for static initialization, you need to have the libraries inside your apk.

initAsync is to use with OpenCV Manager installed.

for static initialization, there is a good answer Here

onResume, onPause and onDestroy should go like this:

@Override
protected void onResume()
{
    super.onResume();
    _javaCameraView.enableView();
}

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

@Override
public void onDestroy() {
    if (_javaCameraView != null) {
        _javaCameraView.disableView();
    }
    super.onDestroy();
}

Some info about static initialization:

public class MainActivity extends Activity implements CameraBridgeViewBase.CvCameraViewListener2 {

private static boolean openCVStarted = false;
JavaCameraView _javaCameraView;

    static{
        System.loadLibrary("opencv_java3");


        if(!OpenCVLoader.initDebug()) {
            Log.d("ERROR", "Unable to load OpenCV");
            openCVStarted = false;
        } else {
            Log.d("SUCCESS", "OpenCV loaded");
            openCVStarted = true;
        }
    }

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        _javaCameraView = (JavaCameraView)findViewById(R.id.cameraView);
        //_javaCameraView.setMaxFrameSize(size.width, size.height);
        _javaCameraView.setVisibility(SurfaceView.VISIBLE);
        _javaCameraView.setCvCameraViewListener(this);
        _javaCameraView.enableView();
        _javaCameraView.enableFpsMeter();
    }

    @Override
    public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame)
    {
        _imgBgrTemp = inputFrame.rgba();
        // Work with the frame here
        return _imgBgrTemp;
    }
Community
  • 1
  • 1
  • I change the code about the theme and the app works. Then i change the onResume, onPause and onDestroy functions and the app crash. What's the problem? – Leonid Jul 29 '16 at 13:44
  • You must initialize OpenCV before doing any calls to its functions. You want to make static initialization or use OpenCV Manager on Android ? – Carlos Goncalves Jul 29 '16 at 13:47
  • I set up the project to be "static initialization". How the functions should be? – Leonid Jul 29 '16 at 14:17
  • I used this tutorial to set up the OpenCV in android studio: https://www.youtube.com/watch?v=OTw_GIQNbD8 – Leonid Jul 29 '16 at 14:58
  • Use this answer: http://stackoverflow.com/questions/27406303/opencv-in-android-studio/27421494#27421494 It is how I setup mine, don't forget that it is architecture dependent, if you plan to distribute your app, you should configure for all CPU. – Carlos Goncalves Jul 29 '16 at 18:29
  • I will edit the answer to put more information on static initialization, but Don't forget to look to that other answer in the link above – Carlos Goncalves Jul 29 '16 at 18:30
  • I used [this tutorial](https://www.youtube.com/watch?v=JIHfqzTdOcQ) to make a static initialization. The code that you added is good for this type of initialization? – Leonid Jul 30 '16 at 13:12
  • I succeeded to view the camera. Thank you very much people. – Leonid Jul 31 '16 at 11:48