2010-04-30 30 views
8

C'est le code que j'ai, ça fonctionne, pas parfaitement mais c'est le cas, le problème est que les vignettes redimensionnées ne collent pas sur le rectangle blanc Drawn, briser le rapport d'aspect des images, voici le code, quelqu'un pourrait me suggérer une solution, s'il vous plaît?Java: Détection du format de l'image, redimensionnement (échelle) et enregistrement au format JPEG

Merci

import java.awt.Color; 
import java.awt.Graphics2D; 
import java.awt.Image; 
import java.awt.RenderingHints; 
import java.awt.geom.Rectangle2D; 
import java.awt.image.BufferedImage; 
import java.io.BufferedInputStream; 
import java.io.ByteArrayInputStream; 
import java.io.ByteArrayOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 

import javax.imageio.ImageIO; 

import org.apache.commons.logging.Log; 
import org.apache.commons.logging.LogFactory; 

public class ImageScalerImageIoImpl implements ImageScaler { 

private static final String OUTPUT_FORMAT_ID = "jpeg"; 

// Re-scaling image 
public byte[] scaleImage(byte[] originalImage, int targetWidth, 
    int targetHeight) { 

    try { 
    InputStream imageStream = new BufferedInputStream(
    new ByteArrayInputStream(originalImage)); 
    Image image = (Image) ImageIO.read(imageStream); 

    int thumbWidth = targetWidth; 
    int thumbHeight = targetHeight; 

    // Make sure the aspect ratio is maintained, so the image is not skewed 
     double thumbRatio = (double)thumbWidth/(double)thumbHeight; 
     int imageWidth = image.getWidth(null); 
     int imageHeight = image.getHeight(null); 
     double imageRatio = (double)imageWidth/(double)imageHeight; 
     if (thumbRatio < imageRatio) { 
      thumbHeight = (int)(thumbWidth/imageRatio); 
     } else { 
      thumbWidth = (int)(thumbHeight * imageRatio); 
     } 

    // Draw the scaled image 
    BufferedImage thumbImage = new BufferedImage(thumbWidth, 
    thumbHeight, BufferedImage.TYPE_INT_RGB); 
    System.out.println("Thumb width Buffered: " + thumbWidth + " || Thumb height Buffered: " + thumbHeight); 

    Graphics2D graphics2D = thumbImage.createGraphics(); 
    // Use of BILNEAR filtering to enable smooth scaling 
    graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, 
    RenderingHints.VALUE_INTERPOLATION_BILINEAR); 
    // graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null); 

    // White Background 
    graphics2D.setPaint(Color.WHITE); 
    graphics2D.fill(new Rectangle2D.Double(0, 0, targetWidth, 
    targetHeight)); 
    graphics2D.fillRect(0, 0, targetWidth, targetHeight); 

    System.out.println("Target width: " + targetWidth + " || Target height: " + targetHeight); 

    // insert the resized thumbnail between X and Y of the image 
    graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null); 

    System.out.println("Thumb width: " + thumbWidth + " || Thumb height: " + thumbHeight); 

    // Write the scaled image to the outputstream 
    ByteArrayOutputStream out = new ByteArrayOutputStream(); 
    ImageIO.write(thumbImage, OUTPUT_FORMAT_ID, out); 
    return out.toByteArray(); 

    } catch (IOException ioe) { 
    throw new ImageResizingException(ioe); 
    } 
} 

} 

Répondre

9

Vous pouvez facilement faire évoluer votre image en utilisant Image « s méthode getScaledInstance:

BufferedImage img = ImageIO.read(new File("image.jpg")); 
int scaleX = (int) (img.getWidth() * 0.5); 
int scaleY = (int) (img.getHeight() * 0.5); 

Image newImg = img.getScaledInstance(scaleX, scaleY, Image.SCALE_SMOOTH); 

Une fois que vous avez obtenu votre échelle Image vous pouvez "convertir" revenir en un BufferedImage tel que décrit here.

Enfin, utilisez la classe ImageIO pour écrire votre BufferedImage dans un fichier.

+0

La mise à l'échelle doit être dynamique en fonction du conteneur Canvas cible. De toute façon, je l'ai résolu moi-même avec un bon refactoring et quelques corrections logiques. Merci quand même – BoDiE2003

+0

le lien ne fonctionne pas. conseils pour la transformation Image en BufferedImage? – Karussell

+0

Quel lien ne fonctionne pas? Je les ai juste essayés tous et ils ont l'air ok. – Adamski