简体   繁体   English

从值数组写入图像后,图像中的rgb像素值发生了变化

[英]rgb pixel values got changed in image after writing image from array of values

Hi my code is like this 嗨,我的代码是这样的

BufferedImage img1=new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);
for(int r=0; r<w; r++)
{
    for(int c=0; c<h; c++)
    {
        img1.setRGB(0, 0, w, h, data,0,w);
     }
}
ImageIO.write(img1,"jpg", new File("abc.jpg"));

I want to create image of width w and height h..I am having my pixel values in an int array called data(consists of combined rgb values)..ImageIO.write method creates the image but with different pixel values..please help me out..i tried very hardly..but still not getting.. 我想创建宽度为w且高度为h的图像。.我将像素值放置在一个称为data(由组合的rgb值组成)的int数组中。ImageIO.write方法创建图像但像素值不同..请帮助我出来..我非常努力..但仍然没有得到..

The reason why is because you're using JPEG compression. 原因是因为您使用的是JPEG压缩。 JPEG compression is a lossy compression algorithm, so what you have stored in memory will not be the same as you have written to file. JPEG压缩是一种有损压缩算法,所以你已经存储在内存中不会像你已经写入文件相同。 Lossy compression allows for high compression ratios, and to mimic or closely resemble what you visually see in the image, but the actual contents will not be the same. 有损压缩允许较高的压缩率,并且可以模拟或非常类似于您在图像中看到的内容,但实际内容将有所不同。 They will certainly be similar though. 他们当然会相似。

The only way you can write to an image file and keep the image pixels the same is to use a lossless compression algorithm. 可以写入图像文件并保持图像像素不变的唯一方法是使用无损压缩算法。 Try using PNG instead. 尝试改用PNG。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM