Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Can not get individual pixel values using getRGB

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Thanked 0 Times in 0 Posts

    Default Can not get individual pixel values using getRGB

    I want to know individual alpha red blue and green values of a pixel. Following code displays image from a file ( given as first argument ) but does not show any rgba vaures ( all values returned are 0 )
    Pl. let me know what is the error in the code :

    import java.awt.*;
    import java.awt.image.*;
    import javax.swing.*;

    public class BuffIt {
    public static void main (String args[]) {
    // Get Image
    ImageIcon icon = new ImageIcon(args[0]);
    Image image = icon.getImage();
    int totval = 0;
    int picval;
    int rd, bl, gr;
    picval = 0;
    int i,j,w,h, clr;

    // Create empty BufferedImage, sized to Image
    BufferedImage buffImage = new BufferedImage( image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
    w = image.getWidth(null);
    h = image.getHeight(null);
    System.out.println("width : " + Integer.toString(w));
    System.out.println("height : " + Integer.toString(h));

    // Draw Image into BufferedImage
    Graphics g = buffImage.getGraphics();
    g.drawImage(image, 0, 0, null);

    // Show success
    JFrame frame = new JFrame();
    JLabel label = new JLabel(new ImageIcon(buffImage));
    for (j=0;j<h;j++) {
    for (i=0;i<w;i++) {
    clr = buffImage.getRGB(i,j);
    rd = (clr & 0x00ff0000) >> 16;
    gr = (clr & 0x0000ff00) >> 8;
    bl = clr & 0x000000ff;
    picval = ( picval + rd + gr + bl);
    if ( rd > 0 || gr > 0 || bl > 0 ) {
    System.out.println("red : " + Integer.toString(rd));
    System.out.println("green : " + Integer.toString(gr));
    System.out.println("blue : " + Integer.toString(bl));

    I have also tried to get rgba values in a array int rgba[] form. But I am always getting zero values for all pixels.
    Please help.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Eastern Florida
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Can not get individual pixel values using getRGB

    Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting and preserve formatting.

    Also posted at http://www.java-forums.org/awt-swing...el-colors.html
    Last edited by Norm; June 17th, 2012 at 11:41 AM.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. returning color of pixel in loop
    By Vergil333@gmail.com in forum Loops & Control Statements
    Replies: 2
    Last Post: March 6th, 2012, 09:14 AM
  2. Break down BigDecimal to individual values
    By tarkal in forum Java Theory & Questions
    Replies: 17
    Last Post: November 16th, 2011, 09:43 AM
  3. Trying to show change on Image based on pixel values
    By javaGurl in forum Algorithms & Recursion
    Replies: 1
    Last Post: September 20th, 2011, 03:49 PM
  4. Setting individual sRGB pixel values on BufferedImages
    By nitrogenFingers in forum AWT / Java Swing
    Replies: 2
    Last Post: February 11th, 2011, 10:27 AM
  5. Get Pixel
    By Ophe in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 27th, 2011, 04:24 PM

Tags for this Thread