Java image resize, maintain aspect ratio

前端 未结 11 1426
盖世英雄少女心
盖世英雄少女心 2020-11-28 19:38

I have an image which I resize:

if((width != null) || (height != null))
{
    try{
        // scale image on disk
        BufferedImage originalImage = ImageI         


        
相关标签:
11条回答
  • 2020-11-28 20:02

    try this

    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);
    }
    
    0 讨论(0)
  • 2020-11-28 20:05

    This is my solution:

    /*
    Change dimension of Image
    */
    public static Image resizeImage(Image image, int scaledWidth, int scaledHeight, boolean preserveRatio) {  
    
        if (preserveRatio) { 
            double imageHeight = image.getHeight();
            double imageWidth = image.getWidth();
    
            if (imageHeight/scaledHeight > imageWidth/scaledWidth) {
                scaledWidth = (int) (scaledHeight * imageWidth / imageHeight);
            } else {
                scaledHeight = (int) (scaledWidth * imageHeight / imageWidth);
            }        
        }                   
        BufferedImage inputBufImage = SwingFXUtils.fromFXImage(image, null);     
        // creates output image
        BufferedImage outputBufImage = new BufferedImage(scaledWidth, scaledHeight, inputBufImage.getType());       
        // scales the input image to the output image
        Graphics2D g2d = outputBufImage.createGraphics();
        g2d.drawImage(inputBufImage, 0, 0, scaledWidth, scaledHeight, null);
        g2d.dispose();      
        return SwingFXUtils.toFXImage(outputBufImage, null);
    }
    
    0 讨论(0)
  • 2020-11-28 20:05

    Just add one more block to Ozzy's code so the thing looks like this:

    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;
        }
        // upscale if original is smaller
        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;
        }
    
        return new Dimension(new_width, new_height);
    }
    
    0 讨论(0)
  • 2020-11-28 20:08
    public class ImageTransformation {
    
    public static final String PNG = "png";
    
    public static byte[] resize(FileItem fileItem, int width, int height) {
    
        try {
            ResampleOp resampleOp = new ResampleOp(width, height);
            BufferedImage scaledImage = resampleOp.filter(ImageIO.read(fileItem.getInputStream()), null);
    
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ImageIO.write(scaledImage, PNG, baos);
    
            return baos.toByteArray();
        } catch (Exception ex) {
            throw new MapsException("An error occured during image resizing.", ex);
        }
    
    }
    
    public static byte[] resizeAdjustMax(FileItem fileItem, int maxWidth, int maxHeight) {
    
        try {
            BufferedInputStream bis = new BufferedInputStream(fileItem.getInputStream());  
            BufferedImage bufimg = ImageIO.read(bis);   
    
            //check size of image 
            int img_width = bufimg.getWidth();
            int img_height = bufimg.getHeight();
            if(img_width > maxWidth || img_height > maxHeight) {
                float factx = (float) img_width / maxWidth;
                float facty = (float) img_height / maxHeight;
                float fact = (factx>facty) ? factx : facty;
                img_width = (int) ((int) img_width / fact);
                img_height = (int) ((int) img_height / fact);
            }
    
            return resize(fileItem,img_width, img_height);
    
        } catch (Exception ex) {
            throw new MapsException("An error occured during image resizing.", ex);
        }
    
    }
    

    }

    0 讨论(0)
  • 2020-11-28 20:15

    All other answers show how to calculate the new image height in function of the new image width or vice-versa and how to resize the image using Java Image API. For those people who are looking for a straightforward solution I recommend any java image processing framework that can do this in a single line.

    The exemple below uses Marvin Framework:

    // 300 is the new width. The height is calculated to maintain aspect.
    scale(image.clone(), image, 300);
    

    Necessary import:

    import static marvin.MarvinPluginCollection.*
    
    0 讨论(0)
提交回复
热议问题