I'm consuming a .NET json service that outputs a byte array. The byte arrary gets converted into the integer representation of each byte. When viewed in Fiddler, it looks like this:
{"imageBackground":[137,80,78,71,13,10,26,10,0,0,0,13,73,72,68,...]}
In Java, I've got the data back into a JSONObject, but I'm unfamiliar with Java so I'm not sure where to go from here to convert that into something usable. I suspect if I can get it back into a stream of some开发者_StackOverflow社区 sort I should be able to make it viewable as an image (PNG/JPG/etc)...
Any tips form here?
Get imageBackground
as a byte array, and then hand it off to ImageIO:
byte[] imageBackground = // set me here;
ByteArrayInputStream input = new ByteArrayInputStream(imageBackground);
try {
BufferedImage ImageIO.read(input);
// do fun stuff with the image...
}
finally {
input.close();
}
I'm not sure what your application wants to do as the image, but once you have a BufferedImage
you can use ImageIO
to convert it to another type, you can do transforms, output to a file...the sky's the limit. You can find a tutorial for that and more by Googling.
Something like this (the methods names are probably incorrect, don't know them from the top of my head)
JSONArray jBytes = theObject.getArray("imageBackground");
byte[] imData = new byte[jBytes.size()];
for (int i = 0; i < jBytes.size(); i++) {
imData[i] = jBytes.get(i);
}
That's how you make it a real byte array. Then do what stevevls posted, or whatever you want.
精彩评论