how do I scale a BufferedImage

前端 未结 3 1190
南笙
南笙 2021-01-12 12:05

I have viewed this question, but it does not seem to actually answer the question that I have. I have a, image file, that may be any resolution. I need to load that image

相关标签:
3条回答
  • 2021-01-12 12:10

    You can create a new BufferedImage of the size you want and then perform a scaled paint of the original image into the new one:

    BufferedImage resizedImage = new BufferedImage(new_width, new_height, BufferedImage.TYPE_INT_ARGB); 
    Graphics2D g = resizedImage.createGraphics();
    g.drawImage(image, 0, 0, new_width, new_height, null);
    g.dispose();
    
    0 讨论(0)
  • 2021-01-12 12:10

    see this website Link1

    Or This Link2

    0 讨论(0)
  • 2021-01-12 12:22

    Something like this? :

     /**
     * Resizes an image using a Graphics2D object backed by a BufferedImage.
     * @param srcImg - source image to scale
     * @param w - desired width
     * @param h - desired height
     * @return - the new resized image
     */
    private BufferedImage getScaledImage(Image srcImg, int w, int h){
        BufferedImage resizedImg = new BufferedImage(w, h, BufferedImage.TRANSLUCENT);
        Graphics2D g2 = resizedImg.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g2.drawImage(srcImg, 0, 0, w, h, null);
        g2.dispose();
        return resizedImg;
    }
    
    0 讨论(0)
提交回复
热议问题