ImageIO.write saves image with distortion

Here is my drawing 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 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 with the last setRGB parameter, it asks for a β€œsweep step”, which, frankly, I don’t know what it is. (but I found somewhere it could be the width of the image). I assume that 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 just open and save the same image.

+4
source share
1 answer

I do not know how you initialize rgbArray , but each line ends with a black pixel (outside the image). It can represent a new line if you initialized rgbArray by reading bytes directly from the image file. Or you did not properly initialize rgbArray .

Black pixels are displayed on the shift image as a diagonal line.

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

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

:

 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 specific index in the array, the next row starts at an index that width + 1 higher in the same array.

0
source

Source: https://habr.com/ru/post/1479083/


All Articles