bufferedreader - Java Combine multiple images into a single bigger image without overlapping -
i'm trying combine several images larger image using java. images passed in height 127 x width 293. idea number of images passed method , method takes images , builds them larger image. there going layout larger image total of 12 possible images can input larger image, spaced out evenly (2 rows of 6 images, none overlapping). if there fewer 12 images passed in, first many spaces filled, rest of image white because background going white. when run program prints larger image, fill first space showing first image in upper left, regardless of how many images passed in. background pinkish color instead of intended white background. i'm beginner java i'm trying work through of these learning pains. advice on how might able solve problem? (code copied below 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 = ""; (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"); } }
the first thing notice you're calling dispose()
on graphics2d after each image draw. why you're seeing 1 image being drawn in larger image. take out call , place after loop , should start seeing more images.
as side note, can simplify for-loop lot:
int width = 293; int height = 127; (int i=0; < math.min(imgs.length, 12); i++){ image image = imageio.read(new file(path, imgs[i].getname())); int row = / 6; // truncate 0 or 1. int column = % 6; // mod produce remainder of / 6 in range 0-5 paint.drawimage(image, column * width, row * height, null); }
Comments
Post a Comment