0

我需要将源图像的大小调整为目标图像,这就是我已经使用以下代码所做的

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;
    }

我正在使用

图像缩放库

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

谁能指导我如何实现我的目标?

谢谢你米希尔帕雷克

原始图像

目标图像

4

3 回答 3

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;
}
于 2013-03-15T04:20:48.500 回答
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;
    }

希望这对你有帮助

于 2013-03-14T17:00:46.550 回答
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);
于 2013-03-14T16:50:27.570 回答