How to crop a .png EncodedImage in BlackBerry whil

2019-08-09 18:03发布

I have a single .png image with several icons on it (with transparent areas) and would like to crop individual icons from it. In Java ME it was rather straight-forward, but in BlackBerry I haven't found an equivalent. The code here shows an example with a Bitmap, however doing so paints the transparent areas with white color:

public Bitmap cropImage(Bitmap image, int x, int y, int width, int height) {
    Bitmap result = new Bitmap(width, height);
    Graphics g = new Graphics(result);
    g.drawBitmap(0, 0, width, height, image, x, y);
    return result;
}

I need the same for an EncodedImage to keep the transparency, but Graphics constructor accepts only a Bitmap. Is there any other way to accomplish this? Thank you for any tips.

UPDATE:

Transparency can be preserved if you omit the intermediate Graphics object altogether, and set the ARGB data directly to the newly created Bitmap, like so:

public Bitmap cropImage(Bitmap image, int x, int y, int width, int height) {
    Bitmap result = new Bitmap(width, height);
    int[] argbData = new int[width * height];
    image.getARGB(argbData, 0, width, x, y, width, height);
    result.setARGB(argbData, 0, width, 0, 0, width, height);
    return result;
}

标签: blackberry
2条回答
Bombasti
2楼-- · 2019-08-09 18:33

try to use

g.setGlobalAlpha(0);

before

g.drawBitmap(0, 0, width, height, image, x, y);

or you can use

drawARGB(int[] data, int offset, int scanLength, int x, int y, int width, int height) 

which preserve the alpha in the destination image.

查看更多
地球回转人心会变
3楼-- · 2019-08-09 18:48

Sorry I didn't try this code but it should give you the idea:

int[] argbData = new int[ width * height ];
image.getARGB(      argbData,
                    0,
                    width
                    x,
                    y,
                    width,
                    height);

Bitmap result = new Bitmap(width, height);
Graphics g = new Graphics(result);
g.drawARGB(argbData , 0, width, 0, 0, width, height);

return result;
查看更多
登录 后发表回答