开发者

How do I get PNG image data as a decoded byte array using GWT?

Using GWT I would like to read a开发者_Go百科 PNG image and have the data accessible to me as a decoded byte array.

On the client side I get the image using an ImageBundle, I then instantiate an Image and call setUrl.

At this point, how do I get the image byte array from the image?


I'm not sure if there is no cross browser way to get the byte array of an image cause we are in JavaScript land after all. You can create a new canvas element, add the image to the canvas.context2D and read the canvas.context2D.data atribute to get an array with RGBA values of every pixel.

Seems gwt supports the canvas element in the latest version so you can archive this in pure Java: http://google-web-toolkit.googlecode.com/svn/javadoc/latest/com/google/gwt/canvas/dom/client/ImageData.html


It's been a while since I did direct file i/o in Java, but here's something based off some of my old code:

InputStream in = this.getClass().getResourceAsStream("/com/path/to/file.png");
ByteArrayOutputStream out = new ByteArrayOutputStream();
int readByte = 0;
while ((readByte = in.read()) != -1) {
    out.write(readByte);
}
out.flush();
byte[] bytes = out.toByteArray();
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜