Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

problem reading colors from a bufferedimage

$
0
0
I was working on a simple image editing program, when i came across a problem with the bufferedimage. What i am trying to do is simple, get the red, green, blue and alpha values from the image; however the numbers i am getting back are inaccurate. I then decided to make a sample class to show the issue i am having which is as follows:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;

public class SampleCode 
{
    public void CreateImage()
    {
        //Creates the image and the graphics
        BufferedImage img = new BufferedImage(50,50,BufferedImage.TYPE_INT_ARGB);
        Graphics g = img.getGraphics();
        //All the sample colors i will use
        Color color1 = new Color(10,115,16,32);
        Color color2 = new Color(32,43,38,115);
        Color color3 = new Color(200,16,101,80);
        Color color4 = new Color(201,68,13,99);
        Color color5 = new Color(60,115,12,200);
        Color color6 = new Color(1,80,19,230);
        //Draws all the colors to the image
        g.setColor(color1);
        g.fillRect(0, 0, 1, 1);
        g.setColor(color2);
        g.fillRect(1, 0, 1, 1);
        g.setColor(color3);
        g.fillRect(2, 0, 1, 1);
        g.setColor(color4);
        g.fillRect(3, 0, 1, 1);
        g.setColor(color5);
        g.fillRect(4, 0, 1, 1);
        g.setColor(color6);
        g.fillRect(5, 0, 1, 1);
        //Now the image is done, i will dispose of the graphics and the colors
        g.dispose();
        color1 = null;
        color2 = null;
        color3 = null;
        color4 = null;
        color5 = null;
        color6 = null;
        //Goes back and reads the colors from the new bufferedimage
        color1 = new Color(img.getRGB(0, 0),true);
        color2 = new Color(img.getRGB(1, 0),true);
        color3 = new Color(img.getRGB(2, 0),true);
        color4 = new Color(img.getRGB(3, 0),true);
        color5 = new Color(img.getRGB(4, 0),true);
        color6 = new Color(img.getRGB(5, 0),true);
        //Now print out each color's value
        System.out.println(color1.getRed() + " " + color1.getGreen() + " " + color1.getBlue() + " " + color1.getAlpha());
        System.out.println(color2.getRed() + " " + color2.getGreen() + " " + color2.getBlue() + " " + color2.getAlpha());
        System.out.println(color3.getRed() + " " + color3.getGreen() + " " + color3.getBlue() + " " + color3.getAlpha());
        System.out.println(color4.getRed() + " " + color4.getGreen() + " " + color4.getBlue() + " " + color4.getAlpha());
        System.out.println(color5.getRed() + " " + color5.getGreen() + " " + color5.getBlue() + " " + color5.getAlpha());
        System.out.println(color6.getRed() + " " + color6.getGreen() + " " + color6.getBlue() + " " + color6.getAlpha());
    }
}



The output for this code is:

8 112 16 32
31 42 38 115
201 16 102 80
201 67 13 99
60 115 11 200
1 80 19 230



As you can see, the values are slightly off, which in the case of an image editing program this can be a big issue. Is there another way to pull out the pixel colors without it being distorted? Thanks in advance for any help and interest in this issue!

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>