Paijo S Kom
Paijo S Kom

Reputation: 61

Java Create and Read RGB pixel value different

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

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

}

and this output :

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

Upvotes: 1

Views: 5906

Answers (1)

Andrew Thompson
Andrew Thompson

Reputation: 168845

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

Upvotes: 2

Related Questions