I'm trying to combine several images into a larger image using Java. The images that are passed in are all height 127 x width 293. The idea is that a number of images are passed to the method and the method takes the images and builds them into another larger image. There is going to be a layout for the larger image where a total of 12 possible images can be input to the larger image, spaced out evenly (2 rows of 6 images, none overlapping). If there are fewer than 12 images passed in, then only the first however many spaces will be filled, the rest of the image will be white because the background is to going to be white. When I run the program it prints the larger image, but it will only fill the first space showing the first image in the upper left, regardless of how many images are passed in. Also the background is a pinkish color instead of the intended white background. I'm only a beginner with Java so I'm trying to work through some of these learning pains. Any advice on how I might be able to solve my problem? (Code is copied below for reference) Thanks!
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();
开发者_StackOverflow }
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");
}
}
The first thing I notice is that you're calling dispose()
on the Graphics2D after each image draw. This is probably why you're only seeing one image being drawn in the larger image. Take out that call and place it after the loop and you should start seeing more images.
As a side note, you can simplify your for-loop a lot:
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);
}
You are calling the dispose method inside a for loop so when the program completes the first run of the code inside the loop it then gets rid of the paint object. When it's time to browse the loop for a second time, the program can't find the paint object so it can neither draw the next image nor fill the room with white.
Try using the dispose method right after you close your for loop.
You can also try adding some println methods to see what length does the imgs item return. You should generally check the numbers inside the variables you use in your loop to see if you are getting what you want.
Create a panel that uses a GridLayout? Then you can add 12 JLabels to the panel with each label containing an ImageIcon. This way the layout manager does all the hard work of positioning and painting the images.
This can be done in java, below is the sample program.
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"));
}
}
精彩评论