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

匆匆过客 提交于 2019-12-13 12:45:26

问题


Using GWT I would like to read a 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?


回答1:


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();



回答2:


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



来源:https://stackoverflow.com/questions/6920701/how-do-i-get-png-image-data-as-a-decoded-byte-array-using-gwt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!