0

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();
}
  • You should post your code so that we can see what exactly you are talking about, and hopefully help you find an answer. – XaolingBao May 22 '16 at 21:01
  • 1
    You're missing the part where you read the image. Like I said, please create a [mcve]. This code needs to read the image and then write it. And before you post that, test if it also has the same problem you've mentioned. – Tom May 22 '16 at 21:09
  • of course you have to choose the wierd type 3BYTE_BGR - why? are you some kind of hacker?? – gpasch May 22 '16 at 21:13
  • I tried others and they all gave me the same problem. That was just the last one I tried. I can change that if it solves some problem. – Yoni Subin May 22 '16 at 21:17
  • 1
    Are you sure it's the saving that is changing the pixels? Have you tried examining `pix` before saving to see whether it contains `145,182,110` or `141,184,114`? – user253751 May 22 '16 at 21:24
  • It isn't changing the value in the array, it is changing the color in the image once saved. The value in the array is the same before and after. – Yoni Subin May 22 '16 at 21:26
  • what is Pixel is some kind of class? can you post the whole program from top to bottom? – gpasch May 22 '16 at 23:18
  • @gpasch What do you think is weird about `TYPE_3BYTE_BGR`? There's nothing wrong with using it, in fact it's very common... – Harald K May 23 '16 at 08:42

0 Answers0