开发者

How to read the original alpha channel from PNG in J2ME?

开发者 https://www.devze.com 2023-03-04 18:45 出处:网络
I\'m writing a simple J2ME game that uses PNG images with 8-bit alpha channel. Problem: not all hardware supports full alpha transparency rendering. However, since my game is pretty static in nature (

I'm writing a simple J2ME game that uses PNG images with 8-bit alpha channel. Problem: not all hardware supports full alpha transparency rendering. However, since my game is pretty static in nature (at the beginning, "sprites" are layed out onto background image, based on current screen size, and that's about it), I thought it would be possible to prerender those transparent images directly onto background during game initialization and use that later in game. I can't prerender them in Photoshop as their positions are not known in advance.

But, it seems there is no way to read the original alpha channel on devices that do not support sem开发者_JS百科i-transparency as it gets resampled during PNG loading. Is there some library that can help with that? Or is it a good idea to store alpha channels separately (e.g. as separate 8-bit PNG images) and manually apply them?

Thanks!


PNG Images also have transparency support if you want to create transparent image then you have read RGB data along with alpha channels and process alpha

Image transPNG=Image.createImage("/trans.png");  //load the tranparent image
int rgbData[];
transPNG.getRGB(rgbData, 0,transPNG.getWidth(), 0, 0,transPNG.getWidth(), transPNG.getHeight());
Image tranparentImage=Image.createRGBImage(rgbData, width, height, true); //process alpha
transPNG=null;

Above code shows how to create the transparent image and use.


I cant promise this will help, but you can try this way of reading the alpha channel using standard methods from Java util.

BufferedImage image = ImageIO.read(new File(name));

int[] alpha = new int[1]; //containg alpha-value for one pixel.
image.getAlphaRaster().getPixel(x, y, alpha);

System.out.println(alpha[0]); //gives the alpha value for x,y 
0

精彩评论

暂无评论...
验证码 换一张
取 消