Ask Your Question
0

Static initialization of OpenCV on Android

asked 2012-09-24 08:03:22 -0600

GFG gravatar image

updated 2012-10-02 05:10:14 -0600

V.G. gravatar image

Hello people,

i have a problem with openCV on Android. I need to use static initialization, i followed this tutorial

http://docs.opencv.org/doc/tutorials/introduction/android_binary_package/android_binary_package.html#application-development-with-static-initialization

I have linked the correct library to my project, but when I run the application, I get a black screen

This is the code of the MainActivity

package org.opencv.samples.tutorial2;

import org.opencv.android.OpenCVLoader;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;

public class Sample2NativeCamera extends Activity {
    static {
        if (!OpenCVLoader.initDebug())
            Log.d("ERROR", "Unable to load OpenCV");
        else
            Log.d("SUCCESS", "OpenCV loaded");
    }
    private static final String TAG = "Sample::Activity";

    public static final int VIEW_MODE_RGBA = 0;
    public static final int VIEW_MODE_GRAY = 1;
    public static final int VIEW_MODE_CANNY = 2;

    private MenuItem mItemPreviewRGBA;
    private MenuItem mItemPreviewGray;
    private MenuItem mItemPreviewCanny;

    public static int viewMode = VIEW_MODE_RGBA;

    private Sample2View mView;

    public Sample2NativeCamera() {
        Log.i(TAG, "Instantiated new " + this.getClass());
    }

    @Override
    protected void onPause() {
        Log.i(TAG, "onPause");
        super.onPause();
        if (null != mView)
            mView.releaseCamera();
    }

    @Override
    protected void onResume() {
        Log.i(TAG, "onResume");
        super.onResume();
        if ((null != mView) && !mView.openCamera()) {
            AlertDialog ad = new AlertDialog.Builder(this).create();
            ad.setCancelable(false); // This blocks the 'BACK' button
            ad.setMessage("Fatal error: can't open camera!");
            ad.setButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                    finish();
                }
            });
            ad.show();
        }
    }

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.i(TAG, "onCreate");
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        Log.i(TAG, "onCreateOptionsMenu");
        mItemPreviewRGBA = menu.add("Preview RGBA");
        mItemPreviewGray = menu.add("Preview GRAY");
        mItemPreviewCanny = menu.add("Canny");
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Log.i(TAG, "Menu Item selected " + item);
        if (item == mItemPreviewRGBA)
            viewMode = VIEW_MODE_RGBA;
        else if (item == mItemPreviewGray)
            viewMode = VIEW_MODE_GRAY;
        else if (item == mItemPreviewCanny)
            viewMode = VIEW_MODE_CANNY;
        return true;
    }
}

edit retag flag offensive close merge delete

Comments

1
  1. What about prebuilt samples (I mean apk-files from SDK)? Do they work correctly?
  2. Does your application work without static initialization?
  3. What device do you have? It is possible that the native camera is broken there. Then you should try to run tutorial applications which use Java camera.

Please update your original question with answers to my questions above.

Kirill Kornyakov gravatar imageKirill Kornyakov ( 2012-09-25 05:51:24 -0600 )edit

i also have a similar problem: http://answers.opencv.org/question/2658/static-initialization-problem/ The prebuilt samples work correctly without static initialization but not with static initialization. i hope you can help me.

Droidkie gravatar imageDroidkie ( 2012-09-27 03:46:33 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
1

answered 2012-10-17 05:41:49 -0600

karthi gravatar image

When I run the samples with static initialization, I got the same issue. Please refer this link. This may help you.

edit flag offensive delete link more
0

answered 2012-09-28 12:51:01 -0600

this post is marked as community wiki

This post is a wiki. Anyone with karma >50 is welcome to improve it.

This should work.

private Sample2View mView;

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

private BaseLoaderCallback mOpenCVCallBack = new BaseLoaderCallback(this) {
    @Override
    public void onManagerConnected(int status) {
        mView = new Sample2View(mAppContext);
        setContentView(mView);

        if(!mView.openCamera()) {
            // Can't open camera
        }
    }
};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    mOpenCVCallBack.onManagerConnected(LoaderCallbackInterface.SUCCESS);
}

@Override
protected void onResume() {
    super.onResume();
    if(mView != null && !mView.openCamera()) {
        // Can't open camera
    }
}

@Override
protected void onPause() {
    super.onPause();
    if(mView != null) {
        mView.releaseCamera();
    }
}
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2012-09-24 08:03:22 -0600

Seen: 9,313 times

Last updated: Oct 20 '12