Static Initialization on OpenCV Android

asked 2014-11-03 06:55:36 -0500

dungthoso gravatar image

updated 2014-11-03 07:12:59 -0500

I'm very new with using OpenCV Android. Few days ago, I've faced trouble with Static Initialization through the tutorial here: http://docs.opencv.org/doc/tutorials/introduction/android_binary_package/dev_with_OCV_on_Android.html When I run my code on device, the Logcat comes out with this debug line:

First attempt to load libs fails

I've tried bunch of solutions such as: - Add line mOpenCVCallBack.onManagerConnected(LoaderCallbackInterface.SUCCESS); to OnCreate() - Copy OpenCVAndroid/sdk/native/libs/ to Project/libs/ - ... Didn't work for me. Any of you has taken over it? Please help Here is my java file:

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.android.Utils;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.highgui.Highgui;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.widget.ImageView;

public class MainActivity extends Activity {

    static
    {
        if(!OpenCVLoader.initDebug()) {
            Log.d("ERROR", "Unable to load OpenCV");
        } else {
            Log.d("SUCCESS", "OpenCV loaded");
        }
    }
    private ImageView mImage;
    private static final String TAG = "MyActivity";
    private Mat image, corners_;
    private static final int CAMERA_PIC_REQUEST = 1111;
    // Native method
    public native void detectCorners (long img, long corners_final);
    public native void Resize2Defined (long img, long corners_, int width, int height, long img_out);
    public native void EnhanceImage (Mat img, Mat img_out);

    private BaseLoaderCallback mLoaderCallBack = new BaseLoaderCallback(this) {
       @Override
        public void onManagerConnected(int status) {
          switch (status) {
              case LoaderCallbackInterface.SUCCESS:
              {
                 Log.i(TAG, "OpenCV loaded successfully");
                 System.loadLibrary("ScannerApp");
              } break;
              default:
              {
                  super.onManagerConnected(status);
              } break;
            }
         }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       // OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_9, this, mLoaderCallBack);

        mImage = (ImageView) findViewById(R.id.camera_image);
        //1
        Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent, CAMERA_PIC_REQUEST);
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CAMERA_PIC_REQUEST) {
            //2
            Bitmap thumbnail = (Bitmap) data.getExtras().get("data"); 
            // mImage.setImageBitmap(thumbnail);
            // Read image
            image = new Mat();
            Utils.bitmapToMat(thumbnail, image);
            corners_ = new Mat();
            detectCorners(image.getNativeObjAddr(), corners_.getNativeObjAddr());
            Mat quad_img = new Mat ();
            int width = 300, height = 300;
            Resize2Defined (image.getNativeObjAddr(), corners_.getNativeObjAddr(), width, height, quad_img.getNativeObjAddr() );
            Bitmap bmp = Bitmap.createBitmap(quad_img.cols(), quad_img.rows(), Bitmap.Config.ARGB_8888);
            Utils.matToBitmap(quad_img, bmp);
            mImage.setImageBitmap(bmp);
            //3
            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
            //4
            File file = new File(Environment.getExternalStorageDirectory()+File.separator + "image.jpg");
            try {
                file.createNewFile();
                FileOutputStream fo = new FileOutputStream(file);
                //5
                fo.write(bytes.toByteArray());
                fo.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    }
}
edit retag flag offensive close merge delete