1''
1''

Reputation: 27125

Android - draw bitmap

My test Android app has a single activity, LoadImage, with two methods: an onCreate method that processes images with OpenCV, and a Display method which displays the image on the screen for 5 seconds. Here is what I have so far for the Display method:

[convert OpenCV matrix to a bitmap using an OpenCV method]

Canvas canvas = new Canvas(bitmap);
canvas.drawBitmap(bitmap, 0, 0, null);

try {
    synchronized (this) {
        wait(5000);
    }
} catch (InterruptedException e) {
}

and here is the XML file for the single activity (it's a blank screen):

<RelativeLayout 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" >
</RelativeLayout>

If I run the code as-is, I get... a blank screen. How do I get the bitmaps to show up?

Thanks so much.

Upvotes: 0

Views: 7439

Answers (2)

levocs
levocs

Reputation: 33

I added this to my acitity_main.xml (it was LinearLayout):

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

and this code to the OnCreate method for the activity for that very same xml:

Bitmap bitmap = BitmapFactory.decodeFile(fileAndPath); 
mImageView = (ImageView) findViewById(R.id.imageView1);     
mImageView.setImageBitmap(bitmap);

bitmap image shows up and remains on screen. Thanks to the above poster

Upvotes: 0

Narendra Pal
Narendra Pal

Reputation: 6604

Try this:

<RelativeLayout 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" >

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

And do this in your java code:

onCreate()
{
    ImageView imageView = (ImageView) findViewById(R.id.imageView1);
}

You are sending the bitmap to the canvas right?

So do this there in your method where you are drawing into the bitmap.

imageView.setImageBitmap(bitmap);

You can not set the canvas directly to the imageView1.

Because as you know in real life, the canvas is just a brush. In the same way here also the canvas is only a brush. so dont worry about it.your edited image is now stored in the bitmap only. So thats why you can directly set the bitmap after editng by canvas.

Upvotes: 1

Related Questions