Two questions about bitmaps (paint & intersection) [SOLVED]

All your problems with Audio, Video and Images.

Two questions about bitmaps (paint & intersection) [SOLVED]

Postby gskbyte » Wed Jun 02, 2010 9:15 pm

I want to do two things with a Bitmap, and I don't know what is the (best) way to do them:

- The first one is to paint something over a Bitmap, and keep the result as a Bitmap. For example, to draw a path on a bitmap.

- The second one is to intersect two bitmaps. For example, I have a Bitmap with something drawn on it and a transparent background, and I want to apply a texture contained in another bitmap to that area.

Are these two things possible? Which would be the best way to do so?

Thank you!
Last edited by gskbyte on Thu Jun 03, 2010 12:20 am, edited 1 time in total.
gskbyte
Experienced Developer
Experienced Developer
 
Posts: 52
Joined: Tue Jan 12, 2010 1:51 pm

Top

Re: Two questions about bitmaps (paint and intersection)

Postby pskink » Wed Jun 02, 2010 9:45 pm

gskbyte wrote:I want to do two things with a Bitmap, and I don't know what is the (best) way to do them:

- The first one is to paint something over a Bitmap, and keep the result as a Bitmap. For example, to draw a path on a bitmap.

- The second one is to intersect two bitmaps. For example, I have a Bitmap with something drawn on it and a transparent background, and I want to apply a texture contained in another bitmap to that area.

Are these two things possible? Which would be the best way to do so?

Thank you!


ad 1

Canvas c = new Canvas(mutableBitmap)
c.drawBitmap(originalBitmap, 0, 0, null)
c.drawPath(path, ...)

ad 2

porter-duff
pskink
pskink
Master Developer
Master Developer
 
Posts: 719
Joined: Mon Nov 24, 2008 3:49 pm

Re: Two questions about bitmaps (paint and intersection)

Postby gskbyte » Wed Jun 02, 2010 9:48 pm

Thank you, I found the answer to the first question two minutes ago. This is the code I wrote:

Code: Select all
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;

public class BitmapOperations
{
    public static Bitmap drawOnBitmap(Bitmap dest, Bitmap org)
    {
        Bitmap bmOverlay = Bitmap.createBitmap(dest.getWidth(), dest.getHeight(), dest.getConfig());
       
        Canvas canvas = new Canvas(bmOverlay);
        canvas.drawBitmap(dest, 0, 0, null);
        canvas.drawBitmap(org, 0, 0, null);
       
        return bmOverlay;
    }
   
    public static Bitmap drawOnBitmap(Bitmap dst, Path org, Paint paint)
    {
        Bitmap bmOverlay = Bitmap.createBitmap(dst.getWidth(), dst.getHeight(), dst.getConfig());
       
        Canvas canvas = new Canvas(bmOverlay);
        canvas.drawBitmap(dst, 0, 0, null);
        canvas.drawPath(org, paint);
       
        return bmOverlay;
    }
}

Thank you!
gskbyte
Experienced Developer
Experienced Developer
 
Posts: 52
Joined: Tue Jan 12, 2010 1:51 pm

Re: Two questions about bitmaps (paint and intersection)

Postby gskbyte » Wed Jun 02, 2010 9:50 pm

And this is what pskink meant with his second answer:

http://developer.android.com/reference/ ... rDuff.html

Thank you very much!
gskbyte
Experienced Developer
Experienced Developer
 
Posts: 52
Joined: Tue Jan 12, 2010 1:51 pm

Re: Two questions about bitmaps (paint and intersection)

Postby gskbyte » Wed Jun 02, 2010 10:21 pm

I've been trying to use Porter-Duff Xfermodes but all i get is a black Path over a background. This is the code i'm using:

Code: Select all
        Bitmap bmOverlay = Bitmap.createBitmap(dst.getWidth(), dst.getHeight(), dst.getConfig());
       
        Canvas canvas = new Canvas(bmOverlay);

        canvas.drawBitmap(dst, 0, 0, null);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
        canvas.drawPath(org, paint);
       
        return bmOverlay;


but I can't get the desired effect with any mode. What am I doing wrong? Thank you!
gskbyte
Experienced Developer
Experienced Developer
 
Posts: 52
Joined: Tue Jan 12, 2010 1:51 pm

Re: Two questions about bitmaps (paint and intersection)

Postby pskink » Wed Jun 02, 2010 10:31 pm

try:

paint.setColor(0x8000ff00)

before drawing path
pskink
pskink
Master Developer
Master Developer
 
Posts: 719
Joined: Mon Nov 24, 2008 3:49 pm

Top

Re: Two questions about bitmaps (paint and intersection)

Postby gskbyte » Wed Jun 02, 2010 10:43 pm

No, it doesn't work. I'm trying to do somehing similar to this: http://developer.android.com/resources/ ... modes.html

But still I get nothing. Thank you for your help :)
gskbyte
Experienced Developer
Experienced Developer
 
Posts: 52
Joined: Tue Jan 12, 2010 1:51 pm

Re: Two questions about bitmaps (paint and intersection)

Postby gskbyte » Thu Jun 03, 2010 12:17 am

Well, I have done it. This is the code for both things:

Code: Select all
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;

public class ImageUtils
{
    public static Bitmap drawOnBitmap(Bitmap dest, Bitmap org)
    {
        Bitmap bmOverlay = Bitmap.createBitmap(dest.getWidth(), dest.getHeight(), dest.getConfig());
       
        Canvas canvas = new Canvas(bmOverlay);
        canvas.drawBitmap(dest, 0, 0, null);
        canvas.drawBitmap(org, 0, 0, null);
       
        return bmOverlay;
    }
   
    public static Bitmap drawOnBitmap(Bitmap dst, Path org, Paint paint)
    {
        Bitmap bmOverlay = Bitmap.createBitmap(dst.getWidth(), dst.getHeight(), dst.getConfig());
       
        Canvas canvas = new Canvas(bmOverlay);
       
        canvas.drawBitmap(dst, 0, 0, null);
        canvas.drawPath(org, paint);
        return bmOverlay;
    }
   
    public static Bitmap applyTextureToPath(Path org, Bitmap texture, Paint paint)
    {
        Bitmap overlay = Bitmap.createBitmap(texture.getWidth(), texture.getHeight(), Bitmap.Config.ARGB_4444);

        Canvas canvas = new Canvas(overlay);
        canvas.drawPath(org, paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(texture, 0, 0, paint);
        paint.setXfermode(null);
       
        return overlay;
    }
}


Thank you for your inspiration and help.
Attachments
device.png
device.png (52.5 KiB) Viewed 1256 times
gskbyte
Experienced Developer
Experienced Developer
 
Posts: 52
Joined: Tue Jan 12, 2010 1:51 pm

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: Exabot [Bot] and 15 guests