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
|
import java.io.*;
import java.awt.Color;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
public class MonImage {
protected BufferedImage ImageCharger;
protected Matrice matriceNdg;
public MonImage(String input) {
try {
ImageCharger = ImageIO.read(new File(input)); // Lis l'image dans le buffer
}
catch(IOException exception)
{
System.out.println("Lecture du fichier "+input+" impossible");
}
}
public MonImage(Matrice test) {
this.matriceNdg = new Matrice(test.getLargeur(),test.getHauteur());
for(int j = 0; j < matriceNdg.getHauteur(); j++) {
for(int i = 0; i < matriceNdg.getLargeur(); i++) {
double pixel = test.getValeur(i, j);
matriceNdg.setValeur(i, j, pixel);
}
}
}
public MonImage(BufferedImage image) {
ImageCharger = image;
}
public void dessinerImage() { // Cette methode dessine l'image en noir et blanc dans le buffer
for (int x= 1; x < (ImageCharger.getWidth()-1); x++) {
for (int y= 1; y < (ImageCharger.getHeight()-1); y++) {
int gris = (int)matriceNdg.getValeur(x,y);
Color c = new Color(gris,gris,gris);
int rgb = c.getRGB();
ImageCharger.setRGB(x, y, rgb);
}
}
}
public void conversionMatrice(Matrice test) {
int nbLignes = test.getLargeur();
int nbCol = test.getHauteur();
for ( int j = 1 ; j < nbCol -1 ; j++ ) {
for ( int i = 1 ; i < nbLignes - 1 ; i++) {
int gris = (int)test.getValeur(i,j);
Color c = new Color(gris,gris,gris);
int rgb = c.getRGB();
ImageCharger.setRGB( i-1 , j-1 , rgb);
}
}
}
public void conversionMatrice2() {
System.out.println("Hello");
int largeur = matriceNdg.getLargeur();
int hauteur = matriceNdg.getHauteur();
for (int j = 0; j < hauteur; j++) {
for (int i = 0 ; i < largeur; i++) {
int gray = (int)matriceNdg.getValeur(i,j);
Color c = new Color(gray,gray,gray);
int rgb = c.getRGB();
ImageCharger.setRGB(i, j, rgb);
}
}
}
public void setMatrice(Matrice test) {
this.matriceNdg = test;
}
public void sauverImage(String nom, String format)
{
try {
File fic= new File(nom+ "." + format);
ImageIO.write(getImage(), format, fic);
System.out.println("image enregistree");
}
catch (IOException e) {
e.printStackTrace();
}
}
} |
Partager