View Full Version : How to create image from integer pixels stored in array

02-12-2012, 02:16 PM
My task includes reading a .jpg image, accessing the pixels, processing them and displaying the final image
the code is as follows:

public static void main(String[] args) {
BufferedImage sourceImage = null;
try {
sourceImage = ImageIO.read(new File("IScSH.jpg"));
} catch (IOException e) { }
int type = sourceImage.getType();
int w = sourceImage.getWidth();
int h = sourceImage.getHeight();
byte[] pixels = null;
if (type == BufferedImage.TYPE_3BYTE_BGR) {
pixels = (byte[]) sourceImage.getData().getDataElements(0, 0, w, h, null);
try {
BufferedImage edgesImage = new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR);
edgesImage.getWritableTile(0, 0).setDataElements(0, 0, w, h, pixels);
ImageIO.write(edgesImage, "jpg", new File("nvRhP.jpg"));
} catch (IOException e) { }

the above code works fine, but if I write something as, before the try block begins,
for ( i = 0; i < data.length; i++) {
data[i] = pixels[offset++] & 0xff;
i.e convert the byte array to integer array and then display the image it gives wrong output. The problem is, as I have to further process these pixels I have to convert them in integer values but then the result is wrong.
Please help