本文介绍了无法在Android的OpenCV中使用imread的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

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上运行它时,它显示如下:

I have import the OpenCV 2.4.9 into my project. When I tried to run it on my Nexus 4. It shows this:

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)

有人可以帮我吗?

推荐答案

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

please move that code from onCreate() to onCameraViewStarted() or into the

case LoaderCallbackInterface.SUCCESS:
{
}

括号.

在这样的加载完成之前,即(异步)mOpenCVCallBack完成之后,您不能使用opencv代码.

you can't use opencv code, before the so's finished loading, that is, after the (async) mOpenCVCallBack finished.

这篇关于无法在Android的OpenCV中使用imread的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 08:40