【问题标题】:How can I extend the width size in a PNG file?如何扩展 PNG 文件中的宽度大小?
【发布时间】:2020-01-24 08:57:16
【问题描述】:

我目前正在处理 PNG 图像,但我有点受阻,因为一项任务不确定如何修复...

这就是场景。我有一个 655x265 像素的 PNG 文件,里面有一个条形码。我需要做的是“扩展”图像的宽度,以便在图像左侧包含一个空白区域,就像这样:

问题是当我执行我的代码时,图像尺寸没有任何变化:

public static void main(String[] args)
{
    try
    {
        String path = "C:\\Users\\xxx\\Desktop\\a.png";
        BufferedImage image = ImageIO.read(new File(path));
        resizeImage(path, image.getWidth() + 100, image.getHeight());
        Graphics graphics = image.getGraphics();
        graphics.setColor(Color.BLACK);
        graphics.setFont(new Font("Verdana", Font.PLAIN, 40));
        graphics.drawString("TTT", 5, 250);
        graphics.dispose();
        ImageIO.write(image, "png", new File(path));
        System.out.println("Image created");
    } catch (Exception e)
    {
        System.out.println(e.getMessage());
        e.printStackTrace();
    }
    System.out.println("Fin");
}

public static void resizeImage(String path, int newHeight, int newWidth) throws IOException
{
    File inputFile = new File(path);
    BufferedImage inputImage = ImageIO.read(inputFile);

    BufferedImage outputImage = new BufferedImage(newWidth, newHeight, inputImage.getType());

    Graphics2D graphics = outputImage.createGraphics();
    graphics.drawImage(inputImage, 0, 0, newWidth, newHeight, null);
    graphics.dispose();

    ImageIO.write(outputImage, "png", new File(path));
    inputImage.flush();
    outputImage.flush();
}

你知道我做错了什么吗?我是第一次使用图像文件吗,可能我误解了一些重要的东西......

编辑:解决方案在 cmets 中提供。 Link

【问题讨论】:

  • 嗨@Niklas,如果我错了,请纠正我,但我认为问题有所不同。另一个问题是关于如何调整图像的大小,只是为了让它更大或更短,但我问的是如何包含一个额外的部分,扩展图像的宽度但使用一个新的空白部分
  • 哦,如果要插入空格,这更准确:stackoverflow.com/questions/5836203/java-padding-image
  • 您正在 加载 图像 BufferedImage image =... 然后您将路径传递给加载图像本身的方法,更改其大小并将其写回,然后在该方法之后致电resizeImage(path, image.getWidth() + 100, image.getHeight());,您将继续处理之前加载的缓冲图像并将其写回磁盘。这意味着您的方法读取、操作和写入图像是免费的,因为周围的代码保留了原始图像的副本并覆盖了调整大小的文件。
  • @Niklas,谢谢。这个答案对我来说真的很好:)
  • Java padding image的可能重复

标签: java image graphics png graphics2d


【解决方案1】:

你可以做的是让该方法获取一个 BufferedImage,调整它的大小并返回它:

public static BufferedImage resizeImage(BufferedImage inputImage, int newHeight, int newWidth){
    BufferedImage outputImage = new BufferedImage(newWidth, newHeight, inputImage.getType());
    Graphics2D graphics = outputImage.createGraphics();
    graphics.drawImage(inputImage, 0, 0, newWidth, newHeight, null);
    graphics.dispose(); 
    outputImage.flush();
    return outputImage;
}

然后继续在你的周围方法中处理调整大小的图像:

    String path = "C:\\Users\\xxx\\Desktop\\a.png";
    BufferedImage image = ImageIO.read(new File(path));
    image = resizeImage(image, image.getWidth() + 100, image.getHeight());  // here you replace the image with the new, resized image from your method
    Graphics graphics = image.getGraphics();
    graphics.setColor(Color.BLACK);
    ....

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-01
    • 2012-03-26
    • 2020-08-15
    • 2020-03-30
    相关资源
    最近更新 更多