How to improve the performance of g.drawImage() method for resizing images

后端 未结 12 1484
面向向阳花
面向向阳花 2020-11-27 11:16

I have an application where users are able to upload pictures in albums but naturally the uploaded images need to be resized so there are also thumbs available and the shown

12条回答
  •  渐次进展
    2020-11-27 11:49

    Well, Jacob and I wanted to resize an Image, not a BufferedImage. So we ended up with this code:

    /**
     * we want the x and o to be resized when the JFrame is resized
     *
     * @param originalImage an x or an o. Use cross or oh fields.
     *
     * @param biggerWidth
     * @param biggerHeight
     */
    private Image resizeToBig(Image originalImage, int biggerWidth, int biggerHeight) {
        int type = BufferedImage.TYPE_INT_ARGB;
    
    
        BufferedImage resizedImage = new BufferedImage(biggerWidth, biggerHeight, type);
        Graphics2D g = resizedImage.createGraphics();
    
        g.setComposite(AlphaComposite.Src);
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    
        g.drawImage(originalImage, 0, 0, biggerWidth, biggerHeight, this);
        g.dispose();
    
    
        return resizedImage;
    }
    

提交回复
热议问题