Writing JPEG file from pixel array of ints

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cagdasal
    New Member
    • Feb 2008
    • 3

    Writing JPEG file from pixel array of ints

    Hello there, i am having the following problem, when i read an image the following way:

    Code:
    public void readBitmapImage (String imagePath){
    		ImageDir = imagePath;
    		
    		//Read in the image file into a BufferedImage object
    		BufferedImage img = null;
    		try {
    		    img = ImageIO.read(new File(ImageDir));
    		} catch (IOException c) {
    			JOptionPane.showMessageDialog(null, c);
    		}
    
    		//Create a raster and get the data from the image
    		Raster raster = img.getData();
    		//Get the images height and width
    		int height = raster.getHeight();
    		int width = raster.getWidth();
    		int size = height*width;
    		int [] pixels = new int[size * 3];
    		//Get the pixels from the image and populate the array with it
    		raster.getPixels(0, 0, width, height, pixels);
    		
    		imageData = pixels;
    }
    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:
    Code:
    BufferedImage img = null;
    		try {
    		    img = ImageIO.read(new File(ImageDir));
    		} catch (IOException c) {
    			JOptionPane.showMessageDialog(null, c);
    			return false;
    		}
    		//Create a raster and get the data from the image
    		Raster raster = img.getData();
    		//Get the images height and width
    		int height = raster.getHeight();
    		int width = raster.getWidth();
    		//Create a new image
    		BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    		//Create a raster in which to write to
    		WritableRaster raster2 = bi.getRaster();
    		//Set the pixels in the raster
    		raster2.setPixels(0, 0, width, height, imageData);
    		//Attach the raster to the image
    		bi.setData(raster2);
    		
    		String input = JOptionPane.showInputDialog(null, "Enter a name for the new image file");
    		File file = new File(DesDir +  "/" + input + ".jpg");
    //Write new file
    		try{
    		ImageIO.write(bi, "jpg", file);
    		}catch(IOException e){
    			JOptionPane.showMessageDialog(null, e);
    			return false;
    		}
    		progressBar.setString("done!");
    		return true;
    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
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #2
    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.

    Comment

    • cagdasal
      New Member
      • Feb 2008
      • 3

      #3
      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?

      Comment

      • cagdasal
        New Member
        • Feb 2008
        • 3

        #4
        The filesize decreases when an image is created aswell, so i guess its because of compression? if so any ideas on a workaround?

        Comment

        • BigDaddyLH
          Recognized Expert Top Contributor
          • Dec 2007
          • 1216

          #5
          Are you trying to make the image smaller?

          Comment

          Working...