Hello there, i am having the following problem, when i read an image the following way:
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:
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
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; }
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;
Thanks in advance
Comment