如何扩展 PNG 文件的宽度?
How can I extend the width size in a PNG file?
我目前正在处理 PNG 图像,但我有点受阻,因为我不确定如何解决这个任务...
场景是这样的。我有一个 655x265 像素的 PNG 文件,里面有一个条形码。我需要做的是'extend'图像的宽度只是为了在图像左侧包含一个空白区域,就像这样:
问题是当我执行我的代码时,图像尺寸没有任何变化:
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();
}
你知道我做错了什么吗?这是我第一次使用图像文件,可能我误解了一些重要的东西......
编辑:解决方案在评论中提供。 Link
你可以做的是让方法获取 BufferedImage,调整它的大小,然后 return 它:
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);
....
我目前正在处理 PNG 图像,但我有点受阻,因为我不确定如何解决这个任务...
场景是这样的。我有一个 655x265 像素的 PNG 文件,里面有一个条形码。我需要做的是'extend'图像的宽度只是为了在图像左侧包含一个空白区域,就像这样:
问题是当我执行我的代码时,图像尺寸没有任何变化:
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();
}
你知道我做错了什么吗?这是我第一次使用图像文件,可能我误解了一些重要的东西......
编辑:解决方案在评论中提供。 Link
你可以做的是让方法获取 BufferedImage,调整它的大小,然后 return 它:
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);
....