Java merge Image: Merge multiple images vertically, java vertical
Code:
/*** Append an image with the same width vertically. # Note: the width must be the same. ** @ param piclist * file path List * @ param outPath * output path */public static void yPic (List <String> piclist, String outPath) {// if (piclist = null | piclist. size () <= 0) {System. out. println ("the Image array is empty! "); Return;} try {int height = 0, // total height width = 0, // total width _ height = 0, // temporary height, or save the offset height _ height = 0, // temporary height, mainly save each height picNum = piclist. size (); // number of images File fileImg = null; // Save the read image int [] heightArray = new int [picNum]; // Save the height of each file BufferedImage buffer = null; // Save the image stream List <int []> imgRGB = new ArrayList <int []> (); // Save the RGBint [] _ imgRGB of all images; // Save the RGB data in an image for (int I = 0; I <picNum; I ++) {fileImg = New File (piclist. get (I); buffer = ImageIO. read (fileImg); heightArray [I] = _ height = buffer. getHeight (); // Image Height if (I = 0) {width = buffer. getWidth (); // Image width} height + = _ height; // obtain the total height _ imgRGB = new int [width * _ height]; // read RGB_imgRGB = buffer from the image. getRGB (0, 0, width, _ height, _ imgRGB, 0, width); imgRGB. add (_ imgRGB);} _ height = 0; // set the offset height to 0 // generate a new image BufferedImage imageResult = new BufferedImage (width, Height, BufferedImage. TYPE_INT_RGB); for (int I = 0; I <picNum; I ++) {__ height = heightArray [I]; if (I! = 0) _ height + = _ height; // calculate the offset height imageResult. setRGB (0, _ height, width, _ height, imgRGB. get (I), 0, width); // write stream} File outFile = new File (outPath); ImageIO. write (imageResult, "jpg", outFile); // write image} catch (Exception e) {e. printStackTrace ();}}
Call:
public static void main(String[] args) {List<String> list = new ArrayList<String>();list.add("f:/bak/image/1.jpg");list.add("f:/bak/image/2.jpg");list.add("f:/bak/image/1.jpg");yPic(list, "f:/bak/image/3.jpg");}
Write something more slowly later