IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

AWT/Swing Java Discussion :

Image "compatible" (vidéo romain guy)


Sujet :

AWT/Swing Java

  1. #1
    Membre expert
    Avatar de ®om
    Profil pro
    Inscrit en
    Janvier 2005
    Messages
    2 815
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2005
    Messages : 2 815
    Points : 3 080
    Points
    3 080
    Par défaut Image "compatible" (vidéo romain guy)
    Salut,

    Suite à cette vidéo : ftp://ftp.developpez.tv/tv/java/semi...ava-Part21.wmv
    (à 8mn20), il y a un code source que j'aimerais bien avoir en clair

    Et j'ai aussi une question, cela concerne les JPEG, mais est-ce pareil pour les PNG?

    Merci d'avance

  2. #2
    Membre confirmé
    Profil pro
    Inscrit en
    Juillet 2006
    Messages
    548
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2006
    Messages : 548
    Points : 635
    Points
    635
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
     
    public static BufferedImage loadImage(String name) {
            try {
                BufferedImage image = ImageIO.read(GuiUtils.class.getClassLoader().getResource(name));
                GraphicsConfiguration configuration = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
                BufferedImage result = configuration.createCompatibleImage(image.getWidth(), image.getHeight(), image.getTransparency());
                result.getGraphics().drawImage(image, 0, 0, null);
                result.getGraphics().dispose();
     
                return result;
            } catch (IOException e) {
                Image image = loadIcon(name).getImage();
                return ((ToolkitImage) image).getBufferedImage();
            }
        }

  3. #3
    Gfx
    Gfx est déconnecté
    Expert éminent
    Avatar de Gfx
    Inscrit en
    Mai 2005
    Messages
    1 770
    Détails du profil
    Informations personnelles :
    Âge : 42

    Informations forums :
    Inscription : Mai 2005
    Messages : 1 770
    Points : 8 178
    Points
    8 178
    Par défaut
    Pour les PNG, normalement il n'y en a pas besoin. Étant donné le coût, ne te casse pas la tête, et fais-le à chaque fois.

    Tiens ma classe :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    /*
     * 
     * Copyright 2001-2004 The Apache Software Foundation
     * 
     * Licensed under the Apache License, Version 2.0 (the "License"); you may not
     * use this file except in compliance with the License. You may obtain a copy of
     * the License at
     * 
     * http://www.apache.org/licenses/LICENSE-2.0
     * 
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
     * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
     * License for the specific language governing permissions and limitations under
     * the License.
     * 
     */
     
    package com.sun.javaone.mailman.ui.image;
     
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.GraphicsConfiguration;
    import java.awt.GraphicsEnvironment;
    import java.awt.RenderingHints;
    import java.awt.Transparency;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.net.URL;
    import javax.imageio.ImageIO;
     
    public class GraphicsUtil {
        private static GraphicsConfiguration configuration = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
     
        private GraphicsUtil() {
        }
     
        public static BufferedImage createCompatibleImage(int width, int height) {
            return configuration.createCompatibleImage(width, height);
        }
     
        public static BufferedImage createTranslucentCompatibleImage(int width, int height) {
            return configuration.createCompatibleImage(width, height, Transparency.TRANSLUCENT);
        }
     
        public static BufferedImage loadCompatibleImage(URL resource) throws IOException {
            BufferedImage image = ImageIO.read(resource);
            return toCompatibleImage(image);
        }
     
        public static BufferedImage toCompatibleImage(BufferedImage image) {
            BufferedImage compatibleImage = configuration.createCompatibleImage(image.getWidth(),
                    image.getHeight(), Transparency.TRANSLUCENT);
            Graphics g = compatibleImage.getGraphics();
            g.drawImage(image, 0, 0, null);
            g.dispose();
            return compatibleImage;
        }
     
        public static BufferedImage createThumbnailFast(BufferedImage image, int requestedThumbSize) {
            float ratio = (float) image.getWidth() / (float) image.getHeight();
     
            BufferedImage temp = new BufferedImage(requestedThumbSize,
                                                   (int) (requestedThumbSize / ratio),
                                                   image.getType());
            Graphics2D g2 = temp.createGraphics();
            g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                                RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            g2.drawImage(image, 0, 0, temp.getWidth(), temp.getHeight(), null);
            g2.dispose();
     
            return temp;
        }
     
        public static BufferedImage createThumbnail(BufferedImage image, int requestedThumbSize) {
            float ratio = (float) image.getWidth() / (float) image.getHeight();
            int width = image.getWidth();
            boolean divide = requestedThumbSize < width;
            BufferedImage thumb = image;
     
            do {
                if (divide) {
                    width /= 2;
                    if (width < requestedThumbSize) {
                        width = requestedThumbSize;
                    }
                } else {
                    width *= 2;
                    if (width > requestedThumbSize) {
                        width = requestedThumbSize;
                    }
                }
     
                BufferedImage temp = new BufferedImage(width, (int) (width / ratio),
                                                       image.getType());
                Graphics2D g2 = temp.createGraphics();
                g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                                    RenderingHints.VALUE_INTERPOLATION_BILINEAR);
                g2.drawImage(thumb, 0, 0, temp.getWidth(), temp.getHeight(), null);
                g2.dispose();
     
                thumb = temp;
            } while (width != requestedThumbSize);
     
            return thumb;
        }
    }
    Romain Guy
    Android - Mon livre - Mon blog

  4. #4
    Membre expert
    Avatar de ®om
    Profil pro
    Inscrit en
    Janvier 2005
    Messages
    2 815
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2005
    Messages : 2 815
    Points : 3 080
    Points
    3 080
    Par défaut
    Merci bien

  5. #5
    Membre averti Avatar de soad
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Février 2004
    Messages
    520
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : Suisse

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Février 2004
    Messages : 520
    Points : 439
    Points
    439
    Par défaut
    Ca sert à rendre les images compatible à quoi ?

  6. #6
    Gfx
    Gfx est déconnecté
    Expert éminent
    Avatar de Gfx
    Inscrit en
    Mai 2005
    Messages
    1 770
    Détails du profil
    Informations personnelles :
    Âge : 42

    Informations forums :
    Inscription : Mai 2005
    Messages : 1 770
    Points : 8 178
    Points
    8 178
    Par défaut
    Au matériel. Par exemple, une machine Windows avec une carte graphique "normale" traite les pixels au format INT_ARGB, autrement un entier contenant les valeurs alpha, rouge, vert et bleu. Mais il existe bien des façon de stocker une image. Un JPEG chargé par ImageIO pourra par exemple être stocké en BYTE_3BGR, c'est-à-dir sous la forme d'un tableau de 3xlargeurxhauteur de byte: tableau[0] = bleu du pixel (0,0), tableau[1] = vert du pixel (0,0), tableau[2] = rouge du pixel (0,0), tableau[3] = bleu du pixel (1,0), etc. Si le format de stockage de l'image est différent, donc incompatible, de celui du matériel, alors à chaque affichage de l'image il faudra faire la conversion. Imagine le travail quand les images sont un peu grandes... En utilisant les méthodes présentées ici, on le fait *une fois*.
    Romain Guy
    Android - Mon livre - Mon blog

  7. #7
    Membre averti Avatar de soad
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Février 2004
    Messages
    520
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : Suisse

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Février 2004
    Messages : 520
    Points : 439
    Points
    439
    Par défaut
    Citation Envoyé par Gfx
    Au matériel. Par exemple, une machine Windows avec une carte graphique "normale" traite les pixels au format INT_ARGB, autrement un entier contenant les valeurs alpha, rouge, vert et bleu. Mais il existe bien des façon de stocker une image. Un JPEG chargé par ImageIO pourra par exemple être stocké en BYTE_3BGR, c'est-à-dir sous la forme d'un tableau de 3xlargeurxhauteur de byte: tableau[0] = bleu du pixel (0,0), tableau[1] = vert du pixel (0,0), tableau[2] = rouge du pixel (0,0), tableau[3] = bleu du pixel (1,0), etc. Si le format de stockage de l'image est différent, donc incompatible, de celui du matériel, alors à chaque affichage de l'image il faudra faire la conversion. Imagine le travail quand les images sont un peu grandes... En utilisant les méthodes présentées ici, on le fait *une fois*.
    ok merci...
    C'est aussi le cas pour un objet ImageIcon ?

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [CKEditor] probleme d'image; ajout de quot lors d'insertion d'image
    Par dedel53 dans le forum Bibliothèques & Frameworks
    Réponses: 2
    Dernier message: 28/10/2007, 00h05

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo