英文:
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
如评论中所述,存在几个问题:
- 在循环中,您在每次迭代时都在覆盖最终图像的内容,最终图像的图形对象必须在循环之前声明一次。
- 您试图在相同的坐标(0,0)处编写每个图像。如果您想要垂直排列图像,请使用一个变量(此处为
yPosition
)来跟踪当前的y位置。 - 您不必创建
new File
,fileEntry
已经是您想要的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 :
- 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.
- 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 . - You don't have to create
new File
,fileEntry
is already theFile
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();
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论