-1

This is an Android app to convert an RGB image to grayscale and display it on a screen. According to logcat, I am getting an unsatisfiedLinkError from

Mat ImageMat = new Mat (image.getHeight(), image.getWidth(), CvType.CV_8U, new Scalar(4));

What is wrong?


public class ImwriteActivity extends Activity /*implements OnClickListener*/{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_imwrite);
        ImageView img = (ImageView) findViewById(R.id.imageView1);

        //get image from sdcard
        File f = new File(Environment.getExternalStorageDirectory().getPath()+"/Test.jpg"); 
        Bitmap image = BitmapFactory.decodeFile(f.getAbsolutePath());

        //convert Bitmap to Mat
        Mat ImageMat = new Mat (image.getHeight(), image.getWidth(), CvType.CV_8U, new Scalar(4));
        Bitmap myBitmap32 = image.copy(Bitmap.Config.ARGB_8888, true);
        Utils.bitmapToMat(myBitmap32, ImageMat);

        //change the color
        Imgproc.cvtColor(ImageMat, ImageMat, Imgproc.COLOR_RGB2GRAY,4);

        //convert the processed Mat to Bitmap
        Bitmap resultBitmap = Bitmap.createBitmap(ImageMat.cols(),  ImageMat.rows(),Bitmap.Config.ARGB_8888);;
        Utils.matToBitmap(ImageMat, resultBitmap);

        //Set member to the Result Bitmap. This member is displayed in an ImageView
        img.setImageBitmap(resultBitmap);
    }

}
Veedrac
  • 58,273
  • 15
  • 112
  • 169
  • 1
    This post may help you http://stackoverflow.com/questions/14693558/unsatisfiedlinkerror-n-mat-while-using-opencv2-4-3-with-android-4-0 – Giru Bhai Jun 15 '14 at 14:38
  • It's always important to tell people [what you have tried](http://whathaveyoutried.com/), including snippets of any failed attempts so that they can understand what avenues you have missed. It's important because it motivates people to answer and it's important because it makes it *easier* to give high quality, relevant answers. With the current state of the question, this hasn't been achieved. If you edit the question, it's possible that the question can be prevented from being closed and the quantity, quality and clarity of answers you get will improve as well. – Veedrac Jun 15 '14 at 14:41
  • like in the link above, - you cannot call any opencv code, unless the baseloader finished loading the opencv dlls ( remember, you're calling native code there) – berak Jun 15 '14 at 14:42
  • so just mark the answer as solved – berak Jun 15 '14 at 15:49

1 Answers1

0

onCreate is the wrong place to put your opencv code, since it depends on native, c++ code.

first you need to load the opencv so's:

private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
    @Override
    public void onManagerConnected(int status) {
        if (status == LoaderCallbackInterface.SUCCESS ) {
            // now we can call opencv code !
        } else {
            super.onManagerConnected(status);
        }
    }
};

@Override
public void onResume() {;
    super.onResume();
    OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_5,this, mLoaderCallback);
    // you may be tempted, to do something here, but it's *async*, and may take some time,
    // so any opencv call here will lead to unresolved native errors.
}


@Override
public void onCameraViewStarted(int width, int height) {
     // probably the best place for your opencv code
}
berak
  • 39,159
  • 9
  • 91
  • 89