简体   繁体   中英

Value won't change with ImageIO.write() in Java

I'm trying to change the rgb value of one pixel of an image, then write it back into a new file, but i have the problem when read the file whose value i have changed before, it's still have the old value.

Here is my code:

    BufferedImage image = ImageIO.read(new File("input.jpg"));
    System.out.println(image.getRGB(0, 0) & 0xff);
    image.setRGB(0, 0, -2);
    System.out.println(image.getRGB(0, 0) & 0xff);
    ImageIO.write(image, "jpg", new File("output.jpg"));
    BufferedImage output = ImageIO.read(new File("output.jpg"));
    System.out.println(output.getRGB(0, 0) & 0xff);

My code's output is:

    255
    254
    255

What it supposed to be when i changed the rgb value:

    255
    254
    254

In BufferedImage class having getRGB() returns an integer pixel in the default RGB color model (TYPE_INT_ARGB) and default sRGB colorspace.

TYPE_INT_ARGB is public static final in. It represents an image with 8-bit RGBA color components packed into integer pixels. The image has a DirectColorModel with alpha. The color data in this image is considered not to be premultiplied with alpha.

When this type is used as the imageType argument to a BufferedImage constructor, the created image is consistent with images created in the JDK1.1 and earlier releases.

So it return everytime 255.

Let us know if you have any query.

Thanks and Regards,

Hardik Nai

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