Je suis entrain d'écrire un programme qui modifie la valeur des pixels puis l'enregistrer sous le format "png" le problème que j'ai et que sans modifier la valeur des pixel je ne trouve pas la même image d'origine.
Merci pour votre aide
Et voila le code que j'ai écrit :
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class ImageReader {
public ImageReader() {
super();
}
/**
* Method to read an image from the file system, store the result whith a
* BufferedImage object
*
* @param filename
* the filename of the picture to read
* @return the representation of the file read with a BuffereadImage object
*/
public BufferedImage readImageFile(String filename) {
try {
File f = new File(filename);
return ImageIO.read(f);
} catch (IOException ioe) {
ioe.printStackTrace();
return null;
}
}
/**
* Method to write a BufferedImage on the file system
*
* @param buffer
* the buffer representing the picture to write
* @param filename
* the name of the output picture
* @param format
* the format of the picture (could be bmp, jpg, gif, png, ...)
*/
public void writeImageFile(BufferedImage buffer, String filename,
String format) {
try {
File f = new File(filename);
ImageIO.write(buffer, format, f);
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
/**
* Method to convert a BufferedImage to an integer array, each integer
* represent a pixel
*
* @param bufferedImage
* @return a pixel map (int[]) representing each pixel of the picture
*/
public int[] bufferedImage2IntArray(BufferedImage bufferedImage) {
int w = bufferedImage.getWidth();
int h = bufferedImage.getHeight();
int[] pixelMap = new int[w * h];
bufferedImage.getRGB(0, 0, w, h, pixelMap, 0, w);
return pixelMap;
}
/**
* Method to convert a pixel matrix to a BufferedImage
*
* @param pixelMap
* the pixel map representing the picture
* @param width
* the width of the picture
* @param height
* the height of the picture
* @return the BufferedImage of the picture
*/
public BufferedImage IntArray2BufferedImage(int[] pixelMap, int width,
int height) {
BufferedImage bufferedImage = new BufferedImage(width, height,
BufferedImage.TYPE_3BYTE_BGR);
bufferedImage.setRGB(0, 0, width, height, pixelMap, 0, width);
return bufferedImage;
}
public static void main(String[] args) {
String filename = "imageOrigine.JPG";
ImageReader imageReader = new ImageReader();
BufferedImage bufferedImage = imageReader.readImageFile(filename);
int width = bufferedImage.getWidth();
int height = bufferedImage.getHeight();
int[] pixelMap = imageReader.bufferedImage2IntArray(bufferedImage);
BufferedImage NEWbufferedImage = imageReader.IntArray2BufferedImage(pixelMap,width,height) ;
imageReader.writeImageFile(NEWbufferedImage, "Sortie.jpg", "png");
}
}