Welcome!

Welcome to the Official BlackBerry Support Community Forums. This is your resource to discuss support topics with your peers, and learn from each other. New to the forum? Please visit the ‘Getting Started’ link below.
inside custom component

Java Development

Reply
New Contributor
anvu
Posts: 4
Registered: ‎08-22-2012
My Carrier: Mobifone

Apply a templete on an image

[ Edited ]

Hi,

 

I have an templete image, and I want to apply it on an image. You can find below for illustration

 

 = 

 

I want the white area in img1 will be transparent, and blue area will remain

What I want to achieve is to do the bitmap manipulation which will combine those 2 bitmap files into the final bitmap, not to draw it on the screen.

I've used the AdvancedUI sample code, which has alpha masking bitmap, but it does not solve my problem

 

Does anyone know how to do that? or have any sample code for that

 

Thanks alot for your help

Please use plain text.
Developer
peter_strange
Posts: 17,631
Registered: ‎07-14-2008

Re: Apply a template on an image

[ Edited ]

While there are a number of other options, as a start I suggest that you start developing this using a BitmapField and have two test images, one containing the image and one containing the template.  The template will have the transparent part where you want it.

 

Now these should be the same size.

 

Create a BitmapField with the image you want.  You need to extend this Field in two ways

 

1) Extend the BitmapField so that you have a setTemplate(bitmap template) method which remembers the template Bitmap.  Say this saves it i n a local variable called _template.

2) Now override paint() in the BitmapField, so that it does the following:

super.paint(<graphics>);

<graphics>.drawBitmap(0, 0, this.getWidth(). this.getHeight(), _template, 0, 0);

 

This should get you started and demonstrate the basic processes.  Once you have this working we can talk about other difficulties, like resizing the template, or changing the template.  But get this working first. 

 

And you do have to create the template image with the transparent part and make sure it is the same size as the image. 

 

Edit:

Welcome to the forum!

Please use plain text.
New Contributor
anvu
Posts: 4
Registered: ‎08-22-2012
My Carrier: Mobifone

Re: Apply a template on an image

Thanks for your response. However, what I want to achieve is to do the bitmap manipulation which will combine those 2 bitmap files into the final bitmap, not to draw it on the screen.

I've used the AdvancedUI sample code, which has alpha masking bitmap, but it does not solve my problem

Please use plain text.
Developer
simon_hain
Posts: 13,754
Registered: ‎07-29-2008
My Carrier: O2 Germany

Re: Apply a template on an image

welcome to the support forums.

you can retrieve the arg array from the bitmap and discard everything you don't want, i.e. copy the part you want to retain in a new array and create a bitmap from that.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
New Contributor
anvu
Posts: 4
Registered: ‎08-22-2012
My Carrier: Mobifone

Re: Apply a template on an image

would u plz show me how to do that?
Please use plain text.
Developer
simon_hain
Posts: 13,754
Registered: ‎07-29-2008
My Carrier: O2 Germany

Re: Apply a template on an image

the method is
http://www.blackberry.com/developers/docs/7.1.0api/net/rim/device/api/system/Bitmap.html#getARGB(int... int, int, int, int, int, int)

it already offers all parameters you need.
if you have a 100x100 bitmap and want to get the middle 10x10 you would need:
x=45
y=45
width=10
height=10

at least that would be my guess, been some time that i manipulated bitmaps on bytelevel.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
New Contributor
anvu
Posts: 4
Registered: ‎08-22-2012
My Carrier: Mobifone

Re: Apply a template on an image

Thanks, but with that method, I can only get a square part of the image, what if I want to get a circle part of a bitmap?
Please use plain text.
Developer
simon_hain
Posts: 13,754
Registered: ‎07-29-2008
My Carrier: O2 Germany

Re: Apply a template on an image

there is no API for a circle part, so you would have to calculate that yourself, and, as a bitmap is always rectangular, fill up the rest of the bytes with transparency.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Developer
peter_strange
Posts: 17,631
Registered: ‎07-14-2008

Re: Apply a template on an image

[ Edited ]

Going to change tack here.

 

The process I have described also works to paint the resultant image onto another Bitmap.

 

Assume you have a template and an image, and you want the transparent part of the template to show the corresponding part of the image.

 

Then you create a suitable size Bitmap, create Graphics context from that, paint the image onto that Graphics context and then paint the template.

 

The key parts of the code are

a) Creating the Bitmap - new Bitmap(...)

b) Getting the Graphics context.  in OS 5.0 and above this is just

Graphics g = Graphics.create(<bitmap>);

c) Then two

g.drawBitmap( ...)

as before.

 

Now you have a combined Bitmap.

 

Apolgies, I missed the "not draw on Screen" comment in your first post. 

Please use plain text.