Displaying bitmap image on Android (OpenCV)

1k Views Asked by At

I'm trying to read an image from external storage as an OpenCV Mat file, convert it to a bitmap image and then display the image (review image after picture is taken). After debugging, it looks like everything is working except the setImageBitmap part. Please let me know what I might be doing wrong. Thanks!

public boolean onTouch(View v, MotionEvent event) {
    Mat img = Highgui.imread(Environment.getExternalStorageDirectory().getPath() + "/sample_picture_2015-06-09_13-24-53.jpeg");
    displayBitmap(img);
    return false;
}

public void displayBitmap(Mat img){
    Bitmap bm = Bitmap.createBitmap(img.cols(), img.rows(),Bitmap.Config.ARGB_8888);
    Utils.matToBitmap(img, bm);

    // find the imageview and draw it!

    ImageView iv = (ImageView) findViewById(R.id.imageView1);
    iv.setImageBitmap(bm);

    Log.i(TAG, "Here");
}

And here is my xml file

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<org.opencv.samples.tutorial3.Tutorial3View
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:visibility="gone"
    android:id="@+id/tutorial3_activity_java_surface_view" />

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="2dp"/>

</LinearLayout>
1

There are 1 best solutions below

0
On

Though it's too late, maybe will help others, In your onTouch,

 File imgFile = new File(Environment.getExternalStorageDirectory()+imgName);

        if (imgFile.exists()) {
            myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
            Mat img = new Mat(myBitmap.getHeight(), myBitmap.getWidth(), 
            CvType.CV_8UC4);
            displayBitmap(img);

Now it should work!