By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
461,955 Members | 735 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 461,955 IT Pros & Developers. It's quick & easy.

Problem in saving and loading images using ImageIO

P: 9
Hey everyone,

I'm working on a program based on steganography. For this, i need to access the individual pixels and perform my manipulations. FYI, I do not have the JAI library available with me and I have to make do with awt, imageIO and image. I'm having no issues in loading an image but when i thought I would test things and just save the same image without manipulation I was surprised. After copying the pixels from the image into an int array, I just created a new image with the same pixel array and image type. When I loaded the new image and compared both their pixels, the array was not the same! Can anyone tell me why this is happening and/or how I can get around this?

Here is some of my code:

Expand|Select|Wrap|Line Numbers
  1.  
  2. //In the class ImgStuff
  3. public BufferedImage loadImg(String fileName){
  4. BufferedImage bImg = null;
  5. try {   
  6.  
  7.             bImg = ImageIO.read(new File(fileName)); 
  8.             return bImg;
  9.         } catch (Exception e) {   
  10.             e.printStackTrace(); 
  11.             return null;
  12.         }   
  13.  
  14.     }
  15.  
  16.  
  17. //In function 'main' of  StegoTest.java
  18.  
  19. ImgStuff img = new ImgStuff();
  20. BufferedImage b = img.loadImg("D:/c.jpg");
  21. int pixels[] = new int[b.getWidth()*b.getHeight()];
  22. b.getRGB(0, 0, b.getWidth(), b.getHeight(), pixels, 0, b.getWidth());
  23. BufferedImage b2;
  24. /* b2.setRGB(0, 0, b.getWidth(), b.getHeight(), pixels, 0, b.getWidth()); <-- doesn't work this way either */
  25. File file = new File("D:/done2.jpg");
  26. try{
  27. ImageIO.write(b, "JPEG",file);
  28. }catch(Exception e){
  29. System.out.println(e);
  30. }    
  31. b2 = img.loadImg("D:/done2.jpg");
  32. int pixels2[] = new int[b2.getWidth()*b2.getHeight()];
  33. b2.getRGB(0, 0, b2.getWidth(), b2.getHeight(), pixels2, 0, b2.getWidth());
  34. /*compare the pixel arrays*/
  35. for(int i=0;i<pixels2.length;i++){
  36. if(pixels[i]!=pixels2[i]){
  37. System.out.println("No");
  38. break;
  39. }
  40. }
  41.  
Could really use your help here. I've tried "jpg", "jpeg" and various uppercase combination for the 'format' argument.

Thanks in advance,

Nikhil
Mar 17 '09 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.