Here I am type casting the byte array data[]
to int
. But getting positive and negetive integer values as output array in arr[i]
. But pixel values should come positive right? For grayscale (0 to 255) I think I am converting wrong way. Can anyone please suggest some code to convert my byte array data[]
to integer 1D array arr[]
?
public class Imagemat {
public static void main(String[] args) throws Exception {
//*************convert byte array*************//
BufferedImage bImage = ImageIO.read(new File("image path"));
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ImageIO.write(bImage, "jpg", bos);
byte[] data = bos.toByteArray(); //byte array data[]
//*****convert to integer array *****//
int[] arr = new int[data.length];
for(int i = 0; i < data.length; i++) {
arr[i] = (int)data[i];
}
for(int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " "); //integer array arr[i]
System.out.println();
}
}
}
Getting output like:
-1 -40 -1 -32 0 16 74 70 73 70 0 1 2 0 0 1 0 1 0 0 -1 -37 0 67 0 8 6 6 7
6 5 8 7 7 7 9 9 8 10 12 20 13 12 11 11 1........