Connecting Tech Pros Worldwide Forums | Help | Site Map

Writing JPEG file from pixel array of ints

Newbie
 
Join Date: Feb 2008
Posts: 3
#1: Feb 22 '08
Hello there, i am having the following problem, when i read an image the following way:

Expand|Select|Wrap|Line Numbers
  1. public void readBitmapImage (String imagePath){
  2.         ImageDir = imagePath;
  3.  
  4.         //Read in the image file into a BufferedImage object
  5.         BufferedImage img = null;
  6.         try {
  7.             img = ImageIO.read(new File(ImageDir));
  8.         } catch (IOException c) {
  9.             JOptionPane.showMessageDialog(null, c);
  10.         }
  11.  
  12.         //Create a raster and get the data from the image
  13.         Raster raster = img.getData();
  14.         //Get the images height and width
  15.         int height = raster.getHeight();
  16.         int width = raster.getWidth();
  17.         int size = height*width;
  18.         int [] pixels = new int[size * 3];
  19.         //Get the pixels from the image and populate the array with it
  20.         raster.getPixels(0, 0, width, height, pixels);
  21.  
  22.         imageData = pixels;
  23. }
  24.  
i get the correct values for the RGB pixels respectively, in the array of integers imageData. But when i try to create a new JPG file and write it with the sama data from the same array like this:
Expand|Select|Wrap|Line Numbers
  1. BufferedImage img = null;
  2.         try {
  3.             img = ImageIO.read(new File(ImageDir));
  4.         } catch (IOException c) {
  5.             JOptionPane.showMessageDialog(null, c);
  6.             return false;
  7.         }
  8.         //Create a raster and get the data from the image
  9.         Raster raster = img.getData();
  10.         //Get the images height and width
  11.         int height = raster.getHeight();
  12.         int width = raster.getWidth();
  13.         //Create a new image
  14.         BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  15.         //Create a raster in which to write to
  16.         WritableRaster raster2 = bi.getRaster();
  17.         //Set the pixels in the raster
  18.         raster2.setPixels(0, 0, width, height, imageData);
  19.         //Attach the raster to the image
  20.         bi.setData(raster2);
  21.  
  22.         String input = JOptionPane.showInputDialog(null, "Enter a name for the new image file");
  23.         File file = new File(DesDir +  "/" + input + ".jpg");
  24. //Write new file
  25.         try{
  26.         ImageIO.write(bi, "jpg", file);
  27.         }catch(IOException e){
  28.             JOptionPane.showMessageDialog(null, e);
  29.             return false;
  30.         }
  31.         progressBar.setString("done!");
  32.         return true;
  33.  
i get different values for the pixels in the new image, so the image is not the same as the original, can anyone tell me why? do i have to change the RGB values to YCbCr values or something before writing them to a file?
Thanks in advance

BigDaddyLH's Avatar
Moderator
 
Join Date: Dec 2007
Location: Kelowna, BC Canada
Posts: 1,212
#2: Feb 22 '08

re: Writing JPEG file from pixel array of ints


I asked you on Sun's Java forums and I'm still not sure what you're trying to do. If you are trying to copy a file, why not copy it byte-by-byte? I'm also not clear on what the error is. Is the image corrupted or is it just small differences (due to compression) that don't reveal themselves to the eye. Please try to be clear.
Newbie
 
Join Date: Feb 2008
Posts: 3
#3: Feb 22 '08

re: Writing JPEG file from pixel array of ints


here for example, in the original image, the first pixel has the values (top left of image): 0, 103, 220 RGB, in the new image that was created from the array of integers holding the RGB pixel values of the original image (from the code above) has the value: 26, 78, 255 RGB respectively.

Another example, the last pixel in the original image has the values : 51, 139, 236, in the new image it has : 68, 136, 217

Why is this, any ideas?
Newbie
 
Join Date: Feb 2008
Posts: 3
#4: Feb 22 '08

re: Writing JPEG file from pixel array of ints


The filesize decreases when an image is created aswell, so i guess its because of compression? if so any ideas on a workaround?
BigDaddyLH's Avatar
Moderator
 
Join Date: Dec 2007
Location: Kelowna, BC Canada
Posts: 1,212
#5: Feb 22 '08

re: Writing JPEG file from pixel array of ints


Are you trying to make the image smaller?
Reply