OpenCV Android 2.4.10上的静态初始化

时间:2014-10-28 06:13:32

标签: java android opencv

我使用OpenCV Android非常新。几天前,我通过这里的教程遇到了静态初始化的问题:http://docs.opencv.org/doc/tutorials/introduction/android_binary_package/dev_with_OCV_on_Android.html 当我在设备上运行我的代码时,Logcat会出现这个调试行:

  

首次尝试加载库失败

我尝试了很多解决方案,例如: - 添加行mOpenCVCallBack.onManagerConnected(LoaderCallbackInterface.SUCCESS);到OnCreate() - 将OpenCVAndroid / sdk / native / libs /复制到Project / libs / - ...... 没有为我工作。 你们中的任何一个接管了吗?请帮忙 这是我的java文件:

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();
            }

        }
    }
}

1 个答案:

答案 0 :(得分:0)

确保 libopencv_java.so 位于systems / libs / armeabi /中。

另一种方法是打开使用initDebug()的 Android.mk 文件,并在 include $(CLEAR_VARS)之间添加这两行,并包含../../sdk /native/jni/OpenCV.mk

OPENCV_CAMERA_MODULES:=上

OPENCV_INSTALL_MODULES:=上

仅适用于Windows。