1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
|
import java.applet.Applet;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.swing.ImageIcon;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class Main extends Applet {
private BufferedImage image2 = null;
public void init() {
// Création d'une image
Image image = getImage(getCodeBase(), "java.jpg");
// conversion Image en BufferedImage
BufferedImage imageBuff = toBufferedImage(image);
// conversion BufferedImage en tableau 3D
int[][][] imageArray = toArray(imageBuff);
// convertion tableau 3D en BufferedImage
this.image2 = toBufferedImage(imageArray);
creerImage(image2,"C:/monimage.jpg");
}
private BufferedImage toBufferedImage(Image image) {
/* On test si l'image n'est pas déja une instance de BufferedImage */
if (image instanceof BufferedImage) {
/* cool, rien à faire */
return (BufferedImage) image;
} else {
/* On s'assure que l'image est complètement chargée */
image = new ImageIcon(image).getImage();
/* On crée une BufferedImage et on copie l'image dedans */
BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB);
bufferedImage.getGraphics().drawImage(image,0,0,null);
return bufferedImage;
}
}
private BufferedImage toBufferedImage(int[][][] array) {
/* recup la taille de l'image */
int width = array.length;
int height = array[0].length;
/* On crée une BufferedImage et on copie les données dedans */
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int rgb = (array[x][y][0]<<24) + (array[x][y][1]<<16) + (array[x][y][2]<<8) + (array[x][y][3]);
bufferedImage.setRGB(x, y, rgb);
}
}
return bufferedImage;
}
private int[][][] toArray(BufferedImage image) {
/* recup la taille de l'image */
int width = image.getWidth();
int height = image.getHeight();
/* creation du tableau */
int[][][] array = new int[width][height][4];
/* remplissage du tableau */
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int rgb = image.getRGB(x, y);
array[x][y][0] = (rgb >> 24) & 0xFF;
array[x][y][1] = (rgb >> 16) & 0xFF;
array[x][y][2] = (rgb >> 8) & 0xFF;
array[x][y][3] = rgb & 0xFF;
}
}
/* test ce qu'il y a dans le tableaux
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
System.out.print("| "+array[x][y][0]+" "+array[x][y][1]+" "+array[x][y][2]+" "+array[x][y][3]);
}
System.out.println(" |");
}
*/
// pour le Rsa : rsa=(1E09*alpha)+(1E06*rouge)+(1E03*vert)+bleu
return array;
}
public void paint(Graphics gc) {
if (this.image2!=null)
gc.drawImage(this.image2, 0, 0, null);
}
public void creerImage(Image pict, String url){
try {
int imageWidth = pict.getWidth(null);
int imageHeight = pict.getHeight(null);
// Dessine l'image dans le buffer
BufferedImage monImage = new BufferedImage(imageWidth,
imageHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = monImage.createGraphics();
graphics2D.drawImage(pict, 0, 0, imageWidth, imageHeight, null);
graphics2D.dispose();
// Sauvegarde l'image dans le fichier output
System.out.println("Création du fichier ...");
BufferedOutputStream outBuffered = new BufferedOutputStream(new
FileOutputStream(url));
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(outBuffered);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(monImage);
param.setQuality(1.0f, true);
encoder.setJPEGEncodeParam(param);
// Encodage de l'image
encoder.encode(monImage);
// Fermeture du buffer
outBuffered.close();
System.out.println("Encodage réussit, image crée.");
} catch( IOException e2 ) { e2.printStackTrace(); }
}
} |
Partager