どのように私は、PNGファイルの幅のサイズを拡張することができますか?

Inazense:

私は現在、PNG画像で働いていると私はタスクがないことを確認する方法を修正することので少しはブロックされたんです...

これはシナリオです。私はそれのバーコード内部で655x265ピクセルのPNGファイルを持っています。私は何をする必要があることはちょうどこのように、単に画像の左側の空白地帯が含まれるように画像の幅「を広げる」です。

サイズ変更やPNGファイル、Javaでキャンバスを拡張

問題は、何も私は私のコードを実行する画像寸法で起こらないということです。

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

あなたは私が間違ってやっているものを知っていますか?画像ファイルを扱う私の最初の回の一つであり、おそらく私は重要な何かを誤解し...

編集:ソリューションがコメントで提供します。リンク

GameDroids:

何あなたができることは、この方法は、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);
    ....

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=315750&siteId=1