Convertir Image en Byte[]
Bonjour
Je cherche un moyen de convertir une Image en byte[]
j'ai tester cette fonction mais le resultat n'est pas bon car j'obtient l'image en la lisant depuis le telephone et si la resolution est trop grande je la reduit et c'est la que j'appele cette fonction et l'array de byte que j'ai en retour est plus grand que la foto d'origine ????
Code:
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;
} |
Merci d'avance