调整图像大小的问题

时间:2017-06-23 16:06:01

标签: java image image-resizing

我正在尝试用Java调整图像大小,但我遇到了下一个问题: enter image description here

这是原始图片,但在调整大小代码后,结果如下: enter image description here

我使用的代码是下一个:

public BufferedImage res20x20(BufferedImage image){

    int type = image.getType() == 0? BufferedImage.TYPE_INT_ARGB : image.getType();
    BufferedImage resizedIm= new BufferedImage(20, 20, type);
    Graphics2D g= resizedIm.createGraphics();
    g.drawImage(image, 0, 0, 20, 20, null);
    g.dispose();
    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);
    return resizedIm;
}

可以找到整个代码here。 结果我想要的是: enter image description here

1 个答案:

答案 0 :(得分:1)

试试这个

public BufferedImage res20x20(BufferedImage image){

    int type = image.getType() == 0? BufferedImage.TYPE_INT_ARGB : image.getType();
    BufferedImage resizedIm= new BufferedImage(20, 20, type);
    Image scaledImage = inputImage.getScaledInstance(20, 20, Image.SCALE_SMOOTH);
    resizedIm.getGraphics().drawImage(scaledImage, 0, 0, null);
    return resizedIm;
}

Image.SCALE_SMOOTH是用于缩放图像的算法类型,您可以尝试使用不同的值来获得更好地满足您需求的结果