无法使用循环合并图像的Java代码。

huangapple go评论78阅读模式
英文:

Unable to Merge Images Java using Loop

问题

package imageMerge;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.*;

import javax.imageio.ImageIO;

public class Merge {

    public static void main(String[] args) {
        final File folder = new File("images");
        Merge m = new Merge();
        m.processFiles(folder);
    }

    public void processFiles(final File folder) {
        String filename = "";
        BufferedImage c = null;
        try {
            int i = 1;
            for (final File fileEntry : folder.listFiles()) {
                if (fileEntry.isFile() && !fileEntry.getName().contains("final")) {
                    filename = fileEntry.getName();
                    c = new BufferedImage(5000, 5000, BufferedImage.TYPE_INT_ARGB);
                    Graphics g = c.getGraphics();
                    System.out.println(filename);
                    BufferedImage a = ImageIO.read(new File("D:\\images\\" + filename));
                    System.out.println(a.getHeight());
                    c.createGraphics().drawImage(a, 0, 0, null);
                    i += 1;
                }
            }
            ImageIO.write(c, "png", new File("D:\\images\\final.png"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
英文:

I am trying below code to merge images but final image has only last image in output.
Using for loop i am trying to add multiple images to a single image.
Please advise.

package imageMerge;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
public class Merge {
public static void main(String[] args) {
final File folder = new File("images");
Merge m=new Merge();
m.processFiles(folder);
}
public void processFiles(final File folder) {
String filename="";
BufferedImage c=null;
try {
int i=1;
for (final File fileEntry : folder.listFiles()) {
if (fileEntry.isFile() && !fileEntry.getName().contains("final")) {
filename=fileEntry.getName();
c = new BufferedImage(5000, 5000, BufferedImage.TYPE_INT_ARGB);
Graphics g = c.getGraphics();
System.out.println(filename);
BufferedImage a = ImageIO.read(new File("D:\\images\\"+filename));
System.out.println(a.getHeight());
c.createGraphics().drawImage(a, 0, 0, null);
i+=1;
}
}
ImageIO.write( c, "png", new File ( "D:\\images\\final.png" ));
} catch (Exception e) {
e.printStackTrace();
}
}
}

答案1

得分: 1

如评论中所述,存在几个问题:

  1. 在循环中,您在每次迭代时都在覆盖最终图像的内容,最终图像的图形对象必须在循环之前声明一次。
  2. 您试图在相同的坐标(0,0)处编写每个图像。如果您想要垂直排列图像,请使用一个变量(此处为yPosition)来跟踪当前的y位置。
  3. 您不必创建new FilefileEntry 已经是您想要的 File 对象。

将它们整合起来:

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;

public class Merge {

    public static void main(final String[] args) {
        final File folder = new File("D:\\images");
        Merge m = new Merge();
        m.processFiles(folder);
    }

    public void processFiles(final File folder) {
        String filename = "";
        BufferedImage c = new BufferedImage(5000, 5000, BufferedImage.TYPE_INT_ARGB);
        Graphics g = c.createGraphics(); // 1)
        int yPosition = 0; // 2)

        try {
            int i = 1;
            for (final File fileEntry : folder.listFiles()) {
                if (fileEntry.isFile() && !fileEntry.getName().contains("final")) {
                    filename = fileEntry.getName();

                    System.out.println(filename);
                    BufferedImage a = ImageIO.read(fileEntry); // 3)
                    System.out.println(a.getHeight());
                    g.drawImage(a, 0, yPosition, null); // 2)

                    yPosition = yPosition + a.getHeight(); // 2)
                    i += 1;
                }
            }
            ImageIO.write(c, "png", new File("D:\\images\\final.png"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
英文:

As stated in the comments, there are several issues :

  1. In your loop, you are overwriting the content of the final image at each iteration, the graphics object of the final image must be declared once, before the loop.
  2. You are attempting to write each image at the same coordinates (0,0). If you want to arrange the images vertically, use a variable (yPosition here) to keep track of the current y location .
  3. You don't have to create new File, fileEntry is already the File object you want

Putting it all together :

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
public class Merge {
public static void main(final String[] args) {
final File folder = new File("D:\\images");
Merge m = new Merge();
m.processFiles(folder);
}
public void processFiles(final File folder) {
String filename = "";
BufferedImage c = new BufferedImage(5000, 5000, BufferedImage.TYPE_INT_ARGB);
Graphics g = c.createGraphics(); // 1)
int yPosition = 0; // 2)
try {
int i = 1;
for (final File fileEntry : folder.listFiles()) {
if (fileEntry.isFile() && !fileEntry.getName().contains("final")) {
filename = fileEntry.getName();
System.out.println(filename);
BufferedImage a = ImageIO.read(fileEntry); // 3)
System.out.println(a.getHeight());
g.drawImage(a, 0, yPosition, null); // 2)
yPosition = yPosition + a.getHeight(); // 2)
i += 1;
}
}
ImageIO.write(c, "png", new File("D:\\images\\final.png"));
} catch (Exception e) {
e.printStackTrace();
}
}
}

huangapple
  • 本文由 发表于 2020年8月20日 17:13:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/63501924.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定