I am using Java to save and read pictures and I need each pixel to be exactly the color it was saved as but whenever I use ImageIO.write
some of the pixels are off ever so slightly. For example, one pixel might change from rgb(145, 182, 110)
to rgb(141, 184, 114)
. I have tried writing the picture to JPEGs, PNGs, and BMPs but the pixels always get changed slightly. Is there a way to make this not happen?
Edit: here is the code where I save the image. I have the pixels that I want saved in a 2D array of Pixels called pix.
BufferedImage b=new BufferedImage(pix.length, pix[0].length, BufferedImage.TYPE_3BYTE_BGR);
String name = JOptionPane
.showInputDialog("What do you want to call the file?");
for(int r=0;r<pix.length;r++){
for(int c=0;c<pix[r].length;c++){
b.setRGB(c, r, new Color(pix[r][c].getRed(),pix[r][c].getGreen(),pix[r][c].getBlue()).getRGB());
}
}
File f1 = new File(f.getAbsolutePath() + "/" + name + ".bmp");
try {
f1.createNewFile();
ImageIO.write(b, "bmp", f1);
} catch (IOException e1) {
e1.printStackTrace();
}