1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
public static byte[] getByteArray(Image img) {
byte[] bytes = null;
int width = img.getWidth();
int height = img.getHeight();
int[] pixels = new int[width * height];
img.getRGB(pixels, 0, width, 0, 0, width, height);
try {
// convert int[] to byte[]
ByteArrayOutputStream bout = new ByteArrayOutputStream();
DataOutputStream dout = new DataOutputStream(bout);
// I suggest writing these first: you'll need them to convert back
dout.writeInt(width);
dout.writeInt(height);
// then the pixels
for (int i = 0; i < pixels.length; i++) {
dout.writeInt(pixels[i]);
}
bytes = bout.toByteArray();
} catch (IOException e) {
e.printStackTrace();
}
return bytes;
} |
Partager