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 3 of 3

Thread: Using BufferedImage to read and write to an image file

  1. #1
    Member
    Join Date
    Feb 2013
    Posts
    57
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Question Using BufferedImage to read and write to an image file

    Below is the following sample code that reads in RGB values using BufferedImage, and then simply writes them back out again to file. The resultant image is perfect, and looks good. No worries there.

    I run a print test to print out the first 10 RBG int values. This is to test the "test.png" file, and then to test the resultant image - "new-test.png". For some reason I am getting different RBG values between the two files.

    E.g. (The first 3 RGB int values)

    test.png : -16704215, -16704215, -16704215

    new-test.png : -16638935, -16638935, -16573142

    Can anyone identify to why I am getting different RBG values that are printed out for both test files?

    try
        { 
        BufferedImage imgBuf = ImageIO.read(new File("test.png"));//also testing with GIFs, JPEGs
        int w = imgBuf.getWidth();
        int h = imgBuf.getHeight();
        int[] RGBarray = imgBuf.getRGB(0,0,w,h,null,0,w);
     
        //Arrays to store their respective component values
        int [][] redPixels = new int [h][w]; 
        int [][] greenPixels = new int [h][w]; 
        int [][] bluePixels = new int [h][w];
     
        for(int i=0; i<=10; i++)
        {
           //print out the first 10 RGB int values - testing purposes
           System.out.println(RGBarray[i]);
        }
     
        //Separate the RGB int values into 3 array, red, green and blue ....
        int x=0;
        for(int row=0; row<h; row++)
        {
           for(int col=0; col<w; col++)
           {
              redPixels[row][col] = ((RGBarray[x]>>16)&0xff);
              greenPixels[row][col] = ((RGBarray[x]>>8)&0xff);
              bluePixels[row][col] = (RGBarray[x]&0xff);
              x++;
           }
        }
     
        //set pixels back using the setRBG() ...
        BufferedImage bufferedImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
     
        for(int row=0; row<h; row++) 
        {
           for(int col=0; col<w; col++)
           {
              //use bit shifting to re-form the RGB int again
              int rgb = (redPixels[row][col] & 0xff) << 16 | (greenPixels[row][col] & 0xff) << 8 | (bluePixels[row][col] & 0xff);
     
              bufferedImage.setRGB(col, row, rgb);
           }
        }
      }
      catch(IOException i){}; // This exception format is only temporary !


  2. #2
    Forum VIP
    Join Date
    Jun 2011
    Posts
    382
    My Mood
    Busy
    Thanks
    49
    Thanked 112 Times in 95 Posts
    Blog Entries
    5

    Default Re: Using BufferedImage to read and write to an image file

    int rgb = (redPixels[row][col] & 0xff) << 16 | (greenPixels[row][col] & 0xff) << 8 | (bluePixels[row][col] & 0xf

    The blue channel is being masked with 0xf. Change this to 0xFF to get both bytes.

    Additionally, your input file is a png so it may have an alpha channel. You can pick up that channel with ((RGBarray[x]>>24)&0xff) and then change the output to BufferedImage.TYPE_INT_ARGB

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,979
    Thanks
    56
    Thanked 2,359 Times in 2,330 Posts

    Default Re: Using BufferedImage to read and write to an image file

    When working with bytes in an int, use the Integer class's toHexString() method so you can more easily see any differences in the values in each byte.
          System.out.println(Integer.toHexString(-16704215) + " " + Integer.toHexString(-16638935));  // ff011d29 ff021c29
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Read/Write class file
    By Anaphase21 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 26th, 2013, 03:14 PM
  2. write & read/load to/from file
    By klskl in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: November 26th, 2013, 10:39 PM
  3. help with GUI .. read file and write file
    By kernal in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 14th, 2012, 11:38 AM
  4. Read and write file in Java
    By waiheng1986 in forum File Input/Output Tutorials
    Replies: 1
    Last Post: March 18th, 2012, 12:54 PM
  5. Java I/O File code; how to read/write file
    By ryu2le in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: September 18th, 2011, 06:51 PM

Tags for this Thread