75 votes

Redimensionnement de l'image Java, maintien des proportions

J'ai une image que je redimensionne:

 if((width != null) || (height != null))
{
    try{
        // scale image on disk
        BufferedImage originalImage = ImageIO.read(file);
        int type = originalImage.getType() == 0? BufferedImage.TYPE_INT_ARGB
                                               : originalImage.getType();

        BufferedImage resizeImageJpg = resizeImage(originalImage, type, 200, 200);
        ImageIO.write(resizeImageJpg, "jpg", file); 

       } catch(IOException e) {
           System.out.println(e.getMessage());
       }
}
 

Voici comment je redimensionne l'image:

 private static BufferedImage resizeImage(BufferedImage originalImage, int type,
                                         Integer img_width, Integer img_height)
{
    BufferedImage resizedImage = new BufferedImage(img_width, img_height, type);
    Graphics2D g = resizedImage.createGraphics();
    g.drawImage(originalImage, 0, 0, img_width, img_height, null);
    g.dispose();

    return resizedImage;
}
 

Maintenant, le problème est que je dois également maintenir le format d'image. Autrement dit, j'ai besoin de la nouvelle image 200/200 pour contenir la nouvelle image à l'échelle. Quelque chose comme ça: entrez la description de l'image ici

J'ai essayé certaines choses mais je n'ai pas fonctionné comme prévu. Toute aide est appréciée. Merci beaucoup.

109voto

Ozzy Points 4358

C'est vraiment un peu de maths.

Mais c'est parti:

 Dimension imgSize = new Dimension(500, 100);
Dimension boundary = new Dimension(200, 200);
 

Fonction pour renvoyer la nouvelle taille en fonction de la frontière

 public static Dimension getScaledDimension(Dimension imgSize, Dimension boundary) {

    int original_width = imgSize.width;
    int original_height = imgSize.height;
    int bound_width = boundary.width;
    int bound_height = boundary.height;
    int new_width = original_width;
    int new_height = original_height;

    // first check if we need to scale width
    if (original_width > bound_width) {
        //scale width to fit
        new_width = bound_width;
        //scale height to maintain aspect ratio
        new_height = (new_width * original_height) / original_width;
    }

    // then check if we need to scale even with the new height
    if (new_height > bound_height) {
        //scale height to fit instead
        new_height = bound_height;
        //scale width to maintain aspect ratio
        new_width = (new_height * original_width) / original_height;
    }

    return new Dimension(new_width, new_height);
}
 

Si quelqu'un a également besoin du code de redimensionnement de l'image, voici une solution décente .

Si vous n'êtes pas sûr de la solution ci-dessus, il existe différentes façons d'obtenir le même résultat.

5voto

daveb Points 24831

Vous voudrez vérifier Image.getScaledInstance () , et plus dans cette réponse: Meilleures pratiques pour les images à l'échelle JAVA

1voto

Pedro Points 1

essaye ça

 float rateX =  (float)jpDisplayImagen.getWidth()/(float)img.getWidth();
float rateY = (float)jpDisplayImagen.getHeight()/(float)img.getHeight();
if (rateX>rateY){
    int W=(int)(img.getWidth()*rateY);
    int H=(int)(img.getHeight()*rateY);
    jpDisplayImagen.getGraphics().drawImage(img, 0, 0,W,H, null);
}
else{
    int W=(int)(img.getWidth()*rateX);
    int H=(int)(img.getHeight()*rateX);
    jpDisplayImagen.getGraphics().drawImage(img, 0, 0,W,H, null);
}
 

Prograide.com

Prograide est une communauté de développeurs qui cherche à élargir la connaissance de la programmation au-delà de l'anglais.
Pour cela nous avons les plus grands doutes résolus en français et vous pouvez aussi poser vos propres questions ou résoudre celles des autres.

Powered by:

X