[英]How to combine multiple split images with overlapping back into an original image by using java?
[英]Java Combine multiple images into a single bigger image without overlapping
我正在嘗試使用 Java 將幾個圖像組合成一個更大的圖像。 傳入的圖像都是 127 x 寬 293 的高度。這個想法是將許多圖像傳遞給該方法,該方法獲取圖像並將它們構建成另一個更大的圖像。 將有一個較大圖像的布局,其中可以將總共 12 個可能的圖像輸入到較大的圖像中,均勻分布(2 行,每行 6 個圖像,不重疊)。 如果傳入的圖像少於 12 個,則只會填充第一個(但有多少空格),圖像的 rest 將是白色的,因為背景將是白色的。 當我運行程序時,它會打印較大的圖像,但它只會填充顯示左上角第一張圖像的第一個空間,無論傳入多少張圖像。背景也是粉紅色而不是預期的白色背景. 我只是 Java 的初學者,所以我正在努力解決其中的一些學習難題。 關於如何解決我的問題的任何建議? (代碼復制如下以供參考)謝謝!
public class ImagesCombine {
public String BuildImgs (File[] imgs)throws IOException {
int arsize = imgs.length;
File path = new File("Z:/JAVAFiles/Images/");
BufferedImage page = new BufferedImage(620,900,BufferedImage.TYPE_INT_ARGB);
Graphics2D paint;
paint = page.createGraphics();
paint.setPaint(Color.WHITE);
paint.fillRect ( 0, 0, page.getWidth(), page.getHeight() );
paint.setBackground(Color.WHITE);
String tmpname = "";
for (int i=0;i<imgs.length;i++){
if(i==0){
Image img0 = ImageIO.read(new File(path, imgs[i].getName()));
paint.drawImage(img0,0,0,null);
paint.dispose();
}
if(i==1){
Image img1 = ImageIO.read(new File(path, imgs[i].getName()));
paint.drawImage(img1,323,0,null);
paint.dispose();
}
if(i==2){
Image img2 = ImageIO.read(new File(path, imgs[i].getName()));
paint.drawImage(img2,0,142,null);
paint.dispose();
}
if(i==3){
BufferedImage img3 = ImageIO.read(new File(path, imgs[i].getName()));
paint.drawImage(img3,323,142,null);
paint.dispose();
}
if(i==4){
BufferedImage img4 = ImageIO.read(new File(path, imgs[i].getName()));
paint.drawImage(img4,0,284,null);
paint.dispose();
}
if(i==5){
BufferedImage img5 = ImageIO.read(new File(path, imgs[i].getName()));
paint.drawImage(img5,323,284,null);
paint.dispose();
}
if(i==6){
BufferedImage img6 = ImageIO.read(new File(path, imgs[i].getName()));
paint.drawImage(img6,0,426,null);
paint.dispose();
}
if(i==7){
BufferedImage img7 = ImageIO.read(new File(path, imgs[i].getName()));
paint.drawImage(img7,323,426,null);
paint.dispose();
}
if(i==8){
BufferedImage img8 = ImageIO.read(new File(path, imgs[i].getName()));
paint.drawImage(img8,0,568,null);
paint.dispose();
}
if(i==9){
BufferedImage img9 = ImageIO.read(new File(path, imgs[i].getName()));
paint.drawImage(img9,323,568,null);
paint.dispose();
}
if(i==10){
BufferedImage img10 = ImageIO.read(new File(path, imgs[i].getName()));
paint.drawImage(img10,0,710,null);
paint.dispose();
}
if(i==11){
BufferedImage img11 = ImageIO.read(new File(path, imgs[i].getName()));
paint.drawImage(img11,323,710,null);
paint.dispose();
}
}
String outpath = "Z:\\JAVAFiles\\" + imgs[0].getName().substring(0,16) + ".jpg";
OutputStream outfile = new FileOutputStream(outpath);
JPEGImageEncoder encoder2 = JPEGCodec.createJPEGEncoder(outfile);
encoder2.encode(page);
outfile.close();
return("Success");
}
}
我注意到的第一件事是您在每次繪制圖像后都在 Graphics2D 上調用dispose()
。 這可能就是為什么您在較大的圖像中只看到一個圖像被繪制的原因。 取出該調用並將其放在循環之后,您應該開始看到更多圖像。
作為旁注,您可以大大簡化您的 for 循環:
int width = 293;
int height = 127;
for (int i=0; i < Math.min(imgs.length, 12); i++){
Image image = ImageIO.read(new File(path, imgs[i].getName()));
int row = i / 6; // This will truncate to 0 or 1.
int column = i % 6; // Mod will produce the remainder of i / 6 in the range 0-5
paint.drawImage(image, column * width, row * height, null);
}
這可以在 java 中完成,下面是示例程序。
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class CombineImages {
public static void main(String[] args) throws IOException {
int imagesCount = 4;
BufferedImage images[] = new BufferedImage[imagesCount];
for(int j = 0; j < images.length; j++) {
images[j] = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = images[j].createGraphics();
g2d.drawRect(10, 10, 80, 80);
g2d.dispose();
}
int heightTotal = 0;
for(int j = 0; j < images.length; j++) {
heightTotal += images[j].getHeight();
}
int heightCurr = 0;
BufferedImage concatImage = new BufferedImage(100, heightTotal, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = concatImage.createGraphics();
for(int j = 0; j < images.length; j++) {
g2d.drawImage(images[j], 0, heightCurr, null);
heightCurr += images[j].getHeight();
}
g2d.dispose();
ImageIO.write(concatImage, "png", new File("/Users/kumarabhishek/Downloads/downloadedFiles/concat.png")); // export concat image
ImageIO.write(images[0], "png", new File("/Users/kumarabhishek/Downloads/downloadedFiles/single.png"));
}
}
您在 for 循環內調用 dispose 方法,因此當程序完成循環內代碼的第一次運行時,它會擺脫油漆 object。 當第二次瀏覽循環時,程序找不到油漆 object 所以它既不能繪制下一個圖像也不能用白色填充房間。
關閉 for 循環后立即嘗試使用 dispose 方法。
您也可以嘗試添加一些 println 方法來查看 imgs 項返回的長度。 您通常應該檢查您在循環中使用的變量中的數字,看看您是否得到了您想要的。
創建一個使用 GridLayout 的面板? 然后,您可以將 12 個 JLabel 添加到面板,每個 label 包含一個 ImageIcon。 這樣,布局管理器就可以完成定位和繪制圖像的所有艱苦工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.