我試圖將幾個圖像合併爲一個使用Java的較大圖像。傳入的圖像的高度都是127×寬度293.這個想法是,一些圖像被傳遞給該方法,該方法獲取圖像並將它們構建到另一個更大的圖像中。對於較大圖像將會有一個佈局,其中總共12個可能的圖像可以輸入到較大的圖像,均勻間隔(2行6個圖像,沒有重疊)。如果傳入的圖像少於12張,則只有第一張圖像會填充很多空白,其餘圖像將爲白色,因爲背景將變爲白色。當我運行該程序時,它將打印較大的圖像,但只會填充顯示左上方第一個圖像的第一個空間,而不管有多少圖像被傳入。此外,背景是粉紅色,而不是預期的白色背景。我只是一個初學Java的人,所以我正在努力解決這些學習中的一些難題。有關我如何能夠解決我的問題的任何建議? (代碼複製如下以供參考)謝謝!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");
}
}
我注意到你在前三張圖像中使用了類型圖像,但是對所有其他圖像使用了bufferedimage類型。這是否完成了一個特定的原因? –