views:

63

answers:

2

Hi everyone,

I have the following Java ME code that I'd like to port to BlackBerry:

Image imgAll = Image.createImage("/fontDigits_200x20.png");
imageDigits = new Image[10];
for(int i = 0; i < imageDigits.length; i++)
    imageDigits[i] = Image.createImage(imgAll, i * 20, 0, 20, 20, Sprite.TRANS_NONE);

Basically, it's one image of ten digits that I want to split into 10 individual images and store them into an array. I looked through the docs, but can't find anything similar on EncodedImage or Graphics.

Thank you for any pointers!

UPDATE:

Good news! Apparently there's no way to crop an EncodedImage in such a way as to have a new EncodedImage which is a cropped subset of the original. However, you can do that with a Bitmap, which essentially is the same.

+1  A: 

you can use

Bitmap.getARGB(int[] argbData,
                    int offset,
                    int scanLength,
                    int x,
                    int y,
                    int width,
                    int height)

after loading your image

Bitmap imgAll = Bitmap.getBitmapResource("fontDigits_200x20.png");

and off course you can create new Bitmap from this ARGB data.

Mahdi Hijazi
Yes, the bitmap gets created, however the transparency is not preserved -- it paints the transparent area with white pixels. The reason I'm using a .png image rather than a bitmap, is to preserve the transparency. Thank you, but unfortunately I can't use it.
Levon
A: 

You can do it directly with the Bitmap.scaleInto function:

Bitmap src;
Bitmap dst = new Bitmap(64,32);
int filterType = Bitmap.FILTER_BILINEAR;
src.scaleInto(srcLeft, srcTop, srcWidth, srcHeight, dst, dstLeft, dstTop, dstWidth, dstHeight, filterType);
ddopson
Just a warning that scaleInto was introduced in 5.0, which may be fine depending on the app, but something to keep in mind
Anthony Rizk
I need this to work on 4.3 devices as well, but thank you for the tip, it will be useful in the future.
Levon
Thanks for the heads up about 5.0. I have ripped it out of my code and implemented a nice fixed-point bitmap scaler instead.
ddopson