ImportJava.awt.image.BufferedImage;ImportJava.io.File;Importjava.io.IOException;ImportJavax.imageio.ImageIO;/** * @authorOucq *@version3 6:13:55 PM * * This class implements the merging of images, which can be combined horizontally or vertically. */ Public classImageutil {/** * @paramfileUrl * File absolute path or relative path *@returncached images to read *@throwsIOException * Path error or an IO exception thrown when the file does not exist*/ Public StaticBufferedImage getbufferedimage (String fileUrl)throwsIOException {File f=NewFile (FILEURL); returnImageio.read (f); } /** * @paramsavedimg * Images to save *@paramSavedir * Saved directory *@paramfilename * Saved file name, must have suffix, such as "beauty.jpg" *@paramformat * file formats: JPG, PNG, or BMP *@return */ Public Static Booleansaveimage (BufferedImage savedimg, String savedir, String fileName, string format) {BooleanFlag =false; //first check that the saved picture is in the correct formatString[] Legalformats = {"JPG", "jpg", "PNG", "PNG", "BMP", "BMP" }; inti = 0; for(i = 0; i < legalformats.length; i++) { if(Format.equals (Legalformats[i])) { Break; } } if(i = = legalformats.length) {//picture format not supportedSystem.out.println ("Not save supported picture Formats!")); return false; } //Check that the file suffix and saved format are consistentString postfix = filename.substring (Filename.lastindexof ('. ') + 1); if(!postfix.equalsignorecase (format)) {System.out.println ("The file suffix to save is inconsistent with the saved format!"); return false; } String fileUrl= Savedir +FileName; File File=NewFile (FILEURL); Try{flag=imageio.write (savedimg, format, file); } Catch(IOException e) {e.printstacktrace (); } returnFlag; } /*** The two graphs to be merged must satisfy such a premise that the height must be equal if the horizontal direction is merged, and the width must be equal if the vertical direction is merged. * Mergeimage method does not make judgment, own judgment. * * @paramIMG1 * The first picture to be merged *@paramImg2 * Second picture with merge *@paramIsHorizontal * is true when horizontal orientation is merged, false indicates vertical merge *@returnreturns the merged BufferedImage object *@throwsIOException*/ Public StaticBufferedImage Mergeimage (Booleanishorizontal, BufferedImage img1, BufferedImage img2)throwsIOException {intW1 =img1.getwidth (); intH1 =img1.getheight (); intW2 =img2.getwidth (); intH2 =img2.getheight (); //reading RGB from a picture int[] Imagearrayone =New int[W1 *H1]; Imagearrayone= Img1.getrgb (0, 0, W1, H1, Imagearrayone, 0, W1);//scan the RGB of each pixel in the image into an array in progressive rows int[] Imagearraytwo =New int[W2 *H2]; Imagearraytwo= Img2.getrgb (0, 0, W2, H2, Imagearraytwo, 0, W2); //Create a new pictureBufferedImage Destimage =NULL; if(ishorizontal) {//Horizontal Orientation MergeDestimage =NewBufferedImage (W1 +W2, H1, Bufferedimage.type_int_rgb); Destimage.setrgb (0, 0, W1, H1, Imagearrayone, 0, W1);//sets the RGB of the upper or left partDestimage.setrgb (W1, 0, W2, H2, Imagearraytwo, 0, W2); } Else{//vertically MergeDestimage =NewBufferedImage (W1, H1 +H2, BUFFEREDIMAGE.TYPE_INT_RGB); Destimage.setrgb (0, 0, W1, H1, Imagearrayone, 0, W1);//sets the RGB of the upper or left partDestimage.setrgb (0, H1, W2, H2, Imagearraytwo, 0, W2);//set the RGB of the lower half } returnDestimage; } /**Merge any number of images into a single picture *@paramishorizontal True for horizontal merging, Fasle for vertical merging *@paramIMGs array of images to merge *@return * @throwsIOException*/ Public StaticBufferedImage Mergeimage (BooleanIsHorizontal, BufferedImage ... imgs)throwsIOException {//Create a new pictureBufferedImage Destimage =NULL; //calculate the length and height of new pictures intALLW = 0, Allh = 0, Allwmax = 0, Allhmax = 0; for(BufferedImage img:imgs) {ALLW+=img.getwidth (); ALLH+=img.getheight (); if(Img.getwidth () >Allwmax) {Allwmax=img.getwidth (); } ; if(Img.getheight () >Allhmax) {Allhmax=img.getheight (); } ; } //Create a new picture if(ishorizontal) {destimage=Newbufferedimage (ALLW, Allhmax, Bufferedimage.type_int_rgb); } Else{destimage=Newbufferedimage (Allwmax, ALLH, Bufferedimage.type_int_rgb); } //Merge all child images into new pictures intWX = 0, WY = 0; for(inti = 0; i < imgs.length; i++) {bufferedimage img=Imgs[i]; intW1 =img.getwidth (); intH1 =img.getheight (); //reading RGB from a picture int[] Imagearrayone =New int[W1 *H1]; Imagearrayone= Img.getrgb (0, 0, W1, H1, Imagearrayone, 0, W1);//scan the RGB of each pixel in the image into an array in progressive rows if(ishorizontal) {//Horizontal Orientation MergeDestimage.setrgb (WX, 0, W1, H1, Imagearrayone, 0, W1);//sets the RGB of the upper or left part}Else{//vertically MergeDESTIMAGE.SETRGB (0, WY, W1, H1, Imagearrayone, 0, W1);//sets the RGB of the upper or left part} WX+=W1; WY+=H1; } returnDestimage; } Public Static voidMain (string[] args) {//read files to be mergedBufferedImage BI1 =NULL; BufferedImage Bi2=NULL; Try{bi1= Getbufferedimage ("Src/one.jpg"); Bi2= Getbufferedimage ("Src/two.jpg"); } Catch(IOException e) {e.printstacktrace (); } //call the Mergeimage method to get the merged imageBufferedImage destimg =NULL; Try{destimg= Mergeimage (false, Bi1, Bi2, BI2); } Catch(IOException e) {e.printstacktrace (); } //Save ImageSaveImage (destimg, "src/", "allv.jpg", "JPG"); System.out.println ("Vertical merge Complete!"); //////////////////////////////////////////////////////////////////////////////////System.out.println ("Below is the case of horizontal merging:"); Try{bi1= Getbufferedimage ("Src/one.jpg"); Bi2= Getbufferedimage ("Src/two.jpg"); } Catch(IOException e) {e.printstacktrace (); } //call the Mergeimage method to get the merged image Try{destimg= Mergeimage (true, Bi1, Bi2, BI2); } Catch(IOException e) {e.printstacktrace (); } //Save ImageSaveImage (destimg, "src/", "allh.jpg", "JPG"); System.out.println ("Horizontal merge Complete!"); }}
Merge multiple images into one, code (can be merged vertically and horizontally)