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
|
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.awt.Image;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
public class Synthese extends JFrame{
private Image image;
Synthese(){
this.setTitle("Synthese additive");
this.setSize(600,600);
this.setLocationRelativeTo(this);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
/** ...puis on l'affiche */
this.setVisible(true);
}
void dessinSynthese(URL URLImage){
/** Accès au toolkit : */
java.awt.Toolkit toolkit = java.awt.Toolkit.getDefaultToolkit();
/** lecture de l'image : */
Image image = toolkit.getImage(URLImage);
//transformation de l'image en BufferedImage
toBufferedImage(image);
}
void 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 la nouvelle image */
BufferedImage bufferedImage = new BufferedImage(
image.getWidth(null),
image.getHeight(null),
BufferedImage.TYPE_INT_RGB );
Graphics g = bufferedImage.createGraphics();
g.drawImage(image,0,0,null);
g.dispose();
constructionImage(bufferedImage);
System.out.println("taille : ");
//return(bufferedImage);
}
}
void constructionImage(BufferedImage image){
int w = image.getWidth(null);
int h = image.getHeight(null);
//System.out.println(w);
System.out.println("taille2 : " + w*h);
int[] rgbs = new int[w*h];
image.getRGB(0,0,w,h,rgbs,0,w);
for(int i = 0;i<rgbs.length;i++){
int rouge = (rgbs[i] >>16 ) & 0xFF;
int vert = 0;
int bleu = 0;
rgbs[i] = rouge + vert + bleu;
System.out.println(rgbs[i]);
System.out.println(i);
}
image.setRGB(0,0,w,h,rgbs,0,w);
repaint();
}
public void paint(Graphics g){
Rectangle rect = this.getBounds();
if(image != null) {
g.drawImage(image, 0,0,image.getWidth(null),image.getHeight(null), this);
}
else{System.out.println("image nulle ");}
// g.drawString("Name : " + nomIm,5,image.getHeight(null) + 20);
//g.drawString("width : " + image.getWidth(null) ,5,image.getHeight(null) + 40);
// g.drawString("height : " + image.getHeight(null),5,image.getHeight(null) + 60);
}
public static void main(String[] args) {
Synthese synthese = new Synthese();
}
} |
Partager