Filtre/error "cannot find createBufferedImage

najah01_3 Messages postés 35 Date d'inscription dimanche 24 septembre 2006 Statut Membre Dernière intervention 17 juillet 2007 - 4 janv. 2007 à 21:06
Twinuts Messages postés 5375 Date d'inscription dimanche 4 mai 2003 Statut Modérateur Dernière intervention 14 juin 2023 - 4 janv. 2007 à 23:06
salut ,

j ai testé ce code pour un filtre d image .
un message erreur "cannor find create bufferedImage"

merci d avance

voila mon code 

import java.awt.* ;
import java.awt.event.* ;
import javax.swing.*  ;
import java.io.*;
import javax.imageio.* ;
import java.awt.image.* ;




public class FiltreImage {


   static Tableau frame ;
       public static void main ( String[] args ) {
       frame = new Tableau() ;
       frame.setVisible(true) ;
       }
       }
    class Tableau extends JFrame {
     //private static final long serialVersionUID = -1654172985512850048L;
 
      static final Dimension screensize =
      Toolkit.getDefaultToolkit().getScreenSize();
       static final int largeurEcran = screensize.width;
       static final int hauteurEcran = screensize.height + 2;
      // static final int largeurEcran = 1024;
        //static final int hauteurEcran = 768;
     
     int l = 400 ;
      int h = 300 ;
      Container pane ;
     
      public Tableau() {
       super() ;
       pane = getContentPane();
       pane.setLayout(new FlowLayout());
       this.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e)  {
         System.exit(0) ;
        }});
      setBounds ((largeurEcran -l) / 2,
                      (hauteurEcran-h) / 2,l,h);
       
                      
              PhotoCanvas photo = new PhotoCanvas("lena.jpg") ;
              l = photo.getWidth();
    h = photo.getHeight();
    setBounds((largeurEcran - l) / 2,
(hauteurEcran - h) / 2, l, h);
              pane.add(photo);
       }
}


class PhotoCanvas extends JPanel {
     private static final long serialVersionUID = -425196446497911425L ;
     Image img ;
 
    public PhotoCanvas ( String s )  {
         setSize (400,300 );
         img = getToolkit().getImage(s) ;
         MediaTracker tracker = new MediaTracker(this);
        tracker.addImage(img, 0);
        try {
            tracker.waitForAll();
        } catch(InterruptedException e) {
            System.err.println(e);
        }
        setSize(img.getWidth(this),
        img.getHeight(this));
    }
    public static BufferedImage contour(BufferedImage img){
        float[] filter = { 0f, -1f, 0f, -1f, 4f, -1f, 0f, -1f, 0f };
        Kernel kernel = new Kernel(3, 3, filter);
        ConvolveOp op = new ConvolveOp(kernel);
        return op.filter(img, null);
    }


    public void paintComponent(Graphics g)  {
       try{
         g.drawImage(contour(createBufferedImag(img)),0,0,this);
        }catch(OutOfMemoryError e){
          System.err.println("Pass assez de mémoire !!!");
        }
   }


    /**
     * Transforme une image en bufferedImage
     * @param image l'image a transformer
     * @return Retourn l'image convertie ou null (si l'image source ete null)
     */
    public static BufferedImage createBufferedImage(Image img) throws OutOfMemoryError {
        if(img == null) return null;//si l'image est null pas besoin de continuer
        if (img instanceof BufferedImage)//si l'image est deja une instance de BufferedImage on la retourne
            return (BufferedImage) img;
      
        // On s'assure que l'image soit bien chargee
        img = new ImageIcon(img).getImage();


        //Determine si l'image contient ou non un pixel de transparence
        boolean hasAlpha = hasAlpha(img);


        int type;//type de couleur
      
        // On creer un BufferedImage ayant un format compatible avec l'environement graphique du pc
        BufferedImage bi = null;
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        try {
            //Maintenant on determine si le nouveau BufferedImage aura ou non un pixel de transparence
            type = Transparency.OPAQUE;
            if (hasAlpha) type = Transparency.BITMASK;


            // Creation de notre nouveau BufferedImage
            bi = ge.getDefaultScreenDevice()
                            .getDefaultConfiguration().
                                createCompatibleImage(
                                        img.getWidth(null), img .getHeight(null), type);
        } catch (HeadlessException e) {
            // Le systeme n'as pas d'ecran ?
        }


        if (bi == null) {
            // Si le BufferedImage precedement creer est null on en creer un avec un model par defaut
            type = BufferedImage.TYPE_INT_RGB;
            if (hasAlpha) type = BufferedImage.TYPE_INT_ARGB;
            bi = new BufferedImage(img.getWidth(null), img.getHeight(null), type);
        }


        // On copie l'image dans le BufferedImage
        Graphics g = bi.createGraphics();
        // maintenant on la dessine
        g.drawImage(img, 0, 0, null);
        g.dispose();
        return bi;
    }
  
    /**
     * Test si une image contient ou non un canal alpha
     * @param image l'image a tester
     * @return retourne vrai si elle en contien un
     */
    public static boolean hasAlpha(Image image) throws OutOfMemoryError {
        // si l'image et deja un BufferedImage alors pas besoin de checker le model de couleur
        if (image instanceof BufferedImage)
            return ((BufferedImage) image).getColorModel().hasAlpha();
        //utilisation d'un 'grabber' pour retrouver le model de couleur de l'image
        //normalement le grab du pixel en haut a gauche  est suffisant.
        PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);
        try {
            if(!pg.grabPixels()) return false;
        } catch (InterruptedException e) {
        }
        // on check si le model de couleur correspond ou non a un chanel alpha
        return pg.getColorModel().hasAlpha();
    }




}   
 

2 réponses

cs_DARKSIDIOUS Messages postés 15814 Date d'inscription jeudi 8 août 2002 Statut Membre Dernière intervention 4 mars 2013 130
4 janv. 2007 à 21:25
bah c'est bien beau de mettre tout le code, mais seules les quelques lignes entourant l'erreur auraient suffit !

Perso, j'ai la flemme de recherché la ligne en cause dans ce script un peu long.
0
Twinuts Messages postés 5375 Date d'inscription dimanche 4 mai 2003 Statut Modérateur Dernière intervention 14 juin 2023 111
4 janv. 2007 à 23:06
Salut,

pas sur que jpeg soit super top pour les convolution essais plutot avec png ou gif sinon penche toi sur JAI

------------------------------------
"On n'est pas au resto : ici on ne fait pas dans les plats tout cuits ..."

WORA
0
Rejoignez-nous