1

我尝试从 rgb 值创建图像,这是我的代码:

try {
        BufferedImage img = new BufferedImage(5, 5, BufferedImage.TYPE_INT_RGB);
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 5; j++) {
                Color clc = new Color(40, 21, 60); 
                int rgb = clc.getRGB();
                img.setRGB(i, j, rgb);
            }
        }
        // retrieve image
        File outputfile = new File("D:\\saved.jpg");
        ImageIO.write(img, "jpg", outputfile);
    } catch (IOException e) {
    }

我创建彩色图像红色 = 40,绿色 = 21,蓝色 = 60 创建图像后,我尝试再次获取像素。但是当我创建时输出像素与 rgb 值不同。这是我获取 rgb 值的代码:

 public static void main(String[] args){
    try {
        BufferedImage bf = ImageIO.read(new File("D:\\saved.jpg"));
        RGB(bf);
    } catch (IOException ex) {
        Logger.getLogger(ektract.class.getName()).log(Level.SEVERE, null, ex);
    }

}
public static void RGB(BufferedImage end)
 {

    System.out.println("GREEN");
    System.out.println("====");
    for (int y = 0; y < end.getHeight(); y++)
    {
        for (int x = 0; x < end.getWidth(); x++)
        {
            int rgb = end.getRGB(x, y);
            int red = (rgb >> 16) & 0x000000FF;
            int green = (rgb >>8 ) & 0x000000FF;
            int blue = (rgb) & 0x000000FF;               
            System.out.print(red+" "+green+" "+blue+" ");                                   
        }
        System.out.println();        
    }

}

这个输出:

绿色
====
39 21 59 39 21 59 39 21 59 39 21 59 39 21 59
39 21 59 39 21 59 39 21 59 39 21 59 39 21 59
39 21 59 39 21 59 39 21 59 39 21 59 39 21 59
39 21 59 39 21 59 39 21 59 39 21 59 39 21 59
39 21 59 39 21 59 39 21 59 39 21 59 39 21 59

请你帮帮我。谢谢

4

1 回答 1

2

JPEG 是一种“有损”格式。保存为 JPEG 并重新加载后,颜色可能会不完全相同。请改用 PNG。EG 下面是 SSCCE 的输出,如下所示。 0表示颜色相同,而-表示不同。

JPG
--------------------
--------------------
--------------------
--------------------
--------------------
--------------------
PNG
00000000000000000000
00000000000000000000
00000000000000000000
00000000000000000000
00000000000000000000
00000000000000000000

代码

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;

class ImagePixelComparison {

    private static BufferedImage convertImageToFormat(
            BufferedImage img, String type) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(img, type, baos);
        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
        BufferedImage encodedImage = ImageIO.read(bais);
        return encodedImage;
    }

    private static void compareImagesByPixel(BufferedImage bi1, BufferedImage bi2) {
        for (int ii=0; ii<bi1.getHeight(); ii++) {
            for (int jj=0; jj<bi1.getWidth(); jj++) {
                boolean b = bi1.getRGB(jj, ii)==bi2.getRGB(jj, ii);
                String s = (b ? "0" : "-");
                System.out.print(s);
            }
            System.out.println();
        }
    }

    public static void main(String[] args) throws IOException {
        BufferedImage originalImage = 
                new BufferedImage(20,6,BufferedImage.TYPE_INT_RGB);
        Graphics2D g = originalImage.createGraphics();
        g.setColor(Color.RED);
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
                RenderingHints.VALUE_ANTIALIAS_ON);
        g.drawLine(0, 0, 20, 6);
        g.dispose();

        System.out.println("JPG");
        BufferedImage jpegImage = convertImageToFormat(originalImage, "jpg");
        compareImagesByPixel(originalImage,jpegImage);
        System.out.println("PNG");
        BufferedImage pngImage = convertImageToFormat(originalImage, "png");
        compareImagesByPixel(originalImage,pngImage);
    }
}
于 2013-07-27T05:15:13.253 回答