简体   繁体   中英

ImageIO.write is saving image with distortion

Here is my painting method:

BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
bi.setRGB(0, 0, width, height, rgbIntArray, 0, width);
ImageIO.write(bi, "bmp", new File("C:/Users/Felipe/Desktop/img2.bmp"));

This is how I populate the rgbIntArray:

rgbIntArray = new int[(rgbArray.length / 3)];
int j = 0;
for (int i = 0; i < rgbArray.length; i += 3) 
{
    rgbIntArray[j] = unsignedToBytes(rgbArray[i]) + 
    unsignedToBytes(rgbArray[i + 1]) * 256 +
    unsignedToBytes(rgbArray[i + 2]) * 65536;
    j++;
}

I tested these values, they seem to be correct.

I think the problem is on the last parameter of setRGB , it asks for the "scanline stride", what to be honest I don`t have a clue what it is. (but I found somewhere it could be the width of the image). I'm assuming the other parameters are correct.

Here are the results:

Original image:

Original image http://i.minus.com/jy7iVQxtghO0l.bmp


Result:

Result http://i.minus.com/jz86D3YkuPPhG.bmp

I will manipulate the image after. I'm just opening and saving the same image.

I don't know how you initialize rgbArray , but each row ends with a black pixel (outside the image). It might represent a new line if you initialized rgbArray by reading the bytes directly from the image file. Or you didn't initialize rgbArray correctly.

The black pixels show on the sheared image as a diagonal line.

You can skip the black pixels at the end of each row by changing this:

bi.setRGB(0, 0, width, height, rgbIntArray, 0, width);

to this:

bi.setRGB(0, 0, width, height, rgbIntArray, 0, width + 1);

The last parameter, width + 1 , basically says that if a given row starts at a certain index in the array, then the next row will start at the index which is width + 1 higher in the same array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM