Image DownScaling问题

时间:2013-03-14 16:45:12

标签: java image image-preloader

我需要重新调整从源图像到目标图像的大小,这就是我使用以下代码

所做的
public static BufferedImage getScaledSampledFilteredImage(
            BufferedImage img, int targetWidth, int targetHeight,
            boolean higherQuality) {

        ResampleOp resampleOp = new ResampleOp(targetWidth, targetHeight);
        resampleOp.setUnsharpenMask(AdvancedResizeOp.UnsharpenMask.VerySharp);
        resampleOp.setUnsharpenMask(AdvancedResizeOp.UnsharpenMask.Soft);
        BufferedImage rescaledImage = resampleOp.filter(img, null);

        return rescaledImage;
    }

我正在使用

Image Scaling library

但正如你在这里看到的那样,我得到了虚线,而不是我想要连续的线。

任何人都可以指导我如何实现目标?

谢谢 Mihir Parekh

Original Image

target image

3 个答案:

答案 0 :(得分:0)

您需要设置抗锯齿:

clearBuffer = new BufferedImage(img.getWidth(null),
                            img.getHeight(null), BufferedImage.TYPE_INT_RGB);
            Graphics2D graphics = clearBuffer.createGraphics();
            graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                RenderingHints.VALUE_INTERPOLATION_BICUBIC);
            graphics.setRenderingHint(RenderingHints.KEY_RENDERING,
                RenderingHints.VALUE_RENDER_QUALITY);
            graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
            graphics.drawImage(img, 0, 0, null);

答案 1 :(得分:0)

我不知道这对您是否有用,但是当我想重新缩放图片时,我正在使用此功能:

public BufferedImage scaleImage(int WIDTH, int HEIGHT, String filename) {
        BufferedImage bi = null;
        try {
            ImageIcon ii = new ImageIcon(filename);//path to image
            bi = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
            Graphics2D g2d = (Graphics2D) bi.createGraphics();
            g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY));
            g2d.drawImage(ii.getImage(), 0, 0, WIDTH, HEIGHT, null);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return bi;
    }

希望这能帮到你

答案 2 :(得分:0)

尝试其中之一。根据需要进行调整以满足您的需求。

public static Image getScaled(final Image img, final int w, final int h) {
    return img.getScaledInstance(w, h, java.awt.Image.SCALE_SMOOTH);
}

public static BufferedImage getScaled(final BufferedImage img, final int w, final int h) {
    final Image image = img.getScaledInstance(w, h, java.awt.Image.SCALE_SMOOTH);
    final BufferedImage bufferedImage = new BufferedImage(w, h, img.getType());
    bufferedImage.getGraphics().drawImage(image, 0, 0, null);
    return bufferedImage;
}