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.


>> REGISTER NOW TO START POSTING


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

Results 1 to 2 of 2

Thread: Retreiving the RGB values from an image

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Retreiving the RGB values from an image

    Hi,

    I created an image using random numbers pixels, i.e. each pixel consists of three random numbers one for green color, another for the blue color and the last for the red color. I stored the image to a jpg file and I write a code to read and retreive the RGB values from the image, but the problem is that I retreved values that does not match the original values(when the image created). Here is the code for writing to and reading from a jpg file:

    1. Writing to an image file
    // fill the array pixels with random numbers
    .
    .
    // writing to a file
    private JLabel createImageLabel(int[] pixels)
    {         
     BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
     WritableRaster raster = image.getRaster(); 
     raster.setPixels(0, 0, width, height, pixels); 
     
          for (int i = 0; i < height; i++) {
          for (int j = 0; j < width; j++) {
            int[] data = new int[3];
     
            System.out.println("x,y: " + j + ", " + i);
            raster.getPixel(j, i, data);
            for (int p=0;p<=data.length-1;p++)
            {         
             System.out.println(data[p]+" ");
            } }}   
     
     JLabel label = new JLabel( new ImageIcon(image) );
     try{
     File f = new File("d:\\test.jpg");
     ImageIO.write(image,"jpeg",f );
     }
     catch(IOException e){System.out.println(e);}
     return label;
    }      
     
    2. Reading from an image
     
     public ReadImage()
      {
      try{
      BufferedImage image =             
      ImageIO.read(new File( "d:\\test.jpg" ));
      int w = image.getWidth();
      int h = image.getHeight();
      for (int i=0; i<h; i++){
       for (int j=0; j<w; j++){   
      int pixel = image.getRGB(j, i);
        int alpha = (pixel >> 24) & 0xff;
        int red = (pixel >> 16) & 0xff;
        int green = (pixel >> 8) & 0xff;
        int blue = (pixel) & 0xff;
        System.out.println("argb: " + alpha + ", " + red + ", " + green + ", " + blue);
       }}
     
      }
      catch(IOException e){System.out.println(e);}
    .
    .
    I greately appreciate your help.

    Regards,
    Ahmad
    Last edited by helloworld922; May 30th, 2011 at 08:42 AM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Retreiving the RGB values from an image

    JPEG is a lossy compression format. I'm not sure how much Java will try to compress a JPEG, but the more it does the more different your results will be. If you want an non-lossy compression format, try using the PNG format.
    Last edited by helloworld922; May 30th, 2011 at 08:49 AM.

Similar Threads

  1. Return the rgb values from a jpg image
    By newparticipant in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 25th, 2011, 03:47 PM
  2. Replies: 2
    Last Post: February 14th, 2011, 05:36 PM
  3. Able to set, but not get values
    By Simple in forum What's Wrong With My Code?
    Replies: 18
    Last Post: June 23rd, 2010, 04:01 PM
  4. how to compare two set values
    By humdinger in forum Collections and Generics
    Replies: 1
    Last Post: March 13th, 2010, 11:46 AM
  5. Pixel Alteration in an image/image rotation
    By bguy in forum Java Theory & Questions
    Replies: 3
    Last Post: November 1st, 2009, 10:50 AM

Tags for this Thread