[英]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. 我正在尝试更改图像一个像素的rgb值,然后将其写回到新文件中,但是当我读取之前更改过其值的文件时,我仍然遇到问题,它仍然具有旧值。
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: 当我更改rgb值时应该是什么:
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. 在具有getRGB()的BufferedImage类中,将在默认RGB颜色模型(TYPE_INT_ARGB)和默认sRGB颜色空间中返回一个整数像素。
TYPE_INT_ARGB is public static final in. It represents an image with 8-bit RGBA color components packed into integer pixels. TYPE_INT_ARGB是公共静态后缀。它表示将8位RGBA颜色分量打包为整数像素的图像。 The image has a DirectColorModel with alpha.
该图像具有带alpha的DirectColorModel。 The color data in this image is considered not to be premultiplied with alpha.
该图像中的颜色数据被认为未与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. 当将此类型用作BufferedImage构造函数的imageType参数时,创建的图像与JDK1.1和早期版本中创建的图像一致。
So it return everytime 255. 因此每次返回255。
Let us know if you have any query. 让我们知道您是否有任何疑问。
Thanks and Regards, 谢谢并恭祝安康,
Hardik Nai 哈迪克·奈
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.