Java创建和读取RGB像素值不同 [英] Java Create and Read RGB pixel value different

查看:156
本文介绍了Java创建和读取RGB像素值不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

i try to create image from rgb value, this is my code :

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值的代码:

I create color image red = 40, green = 21, and blue = 60 After image was created, i try to get pixel again. But output pixel different with rgb value when i create. This is my code to get rgb value :

 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();        
    }

}

此输出:


GREEN
====
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

请帮助我。
谢谢

Please can you help me. thanks

推荐答案

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

JPEG is a 'lossy' format. It is possible the colors will not be exactly the same after saving to JPEG and reloading. Use PNG instead. E.G. Here is the output from the SSCCE shown below. 0 indicates the same color, whereas - indicates different.

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



代码



Code

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);
    }
}

这篇关于Java创建和读取RGB像素值不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆