imread将无法在OpenCV中使用Android

时间:2014-07-06 16:14:20

标签: android opencv

这是我的代码:

package com.example.edgedetection;

import java.io.File;

import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Size;
import org.opencv.highgui.Highgui;
import org.opencv.imgproc.Imgproc;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity {
private BaseLoaderCallback  mOpenCVCallBack = new BaseLoaderCallback(this) {
        @Override
        public void onManagerConnected(int status) {
            switch (status) {
                    case LoaderCallbackInterface.SUCCESS:
                    {

                    } break;
                    default:
                    {
                        super.onManagerConnected(status);
                    } break;
                }
        }
        };
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        double scale = 0.1;
        imageview =  (ImageView)this.findViewById(R.id.image_view);
        Bitmap bm1=BitmapFactory.decodeFile("/mnt/sdcard/test1.bmp");
        imageview.setImageBitmap(bm1);
        Mat img = Highgui.imread("/mnt/sdcard/test1.bmp",1);

        Size dsize = new Size(img.width()*scale,img.height()*scale);
        Mat img2 = new Mat(dsize,CvType.CV_8SC1);
        Mat img3 = new Mat();
        img.convertTo(img2, CvType.CV_8SC1);
        Imgproc.Canny(img, img3, 123, 250);

        boolean flag=Highgui.imwrite("mnt/sdcard/new.jpg", img3);
        if(flag)
        {
            File f = new File("mnt/sdcard/new.jpg");
            if(f.exists())
            {
                Bitmap bm=BitmapFactory.decodeFile("mnt/sdcard/new.jpg");
                imageview.setImageBitmap(bm);
            }
        }//end if
        else{
        Toast.makeText(MainActivity.this, "===========fail to write!============!!", 3).show();
        }
    }

    @Override
    public void onResume()
    {
        super.onResume();
        OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_9, this, mOpenCVCallBack);
    }
}

我已将OpenCV 2.4.9导入到我的项目中。当我试图在我的Nexus 4上运行它时。它显示了这个:

java.lang.UnsatisfiedLinkError: No implementation found for long
org.opencv.highgui.Highgui.imread_0(java.lang.String,int) (tried 
Java_org_opencv_highgui_Highgui_imread_10 and 
Java_org_opencv_highgui_Highgui_imread_10_Ljava_lang_String_2I)

有人可以帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:1)

请将该代码从onCreate()移至onCameraViewStarted()或

case LoaderCallbackInterface.SUCCESS:
{
}

支架。

在完成加载之前,即在(异步)mOpenCVCallBack完成之后,您无法使用opencv代码。

答案 1 :(得分:0)

尝试以下代码直接通过imread访问该文件。

File root = Environment.getExternalStorageDirectory();
    File file = new File(root, "test1.bmp");
    Mat m = Highgui.imread(file.getAbsolutePath());
    if (m.data)
    {
    showToast("Height: " + m.height() + " Width: " + m.width());
    }
    else
    {
    // display error message and exit
    }