/** * 纵向合并图片,ossObject.getObjectContent()返回InputStream对象 */ private BufferedImage mergeImage(List<OSSObject> ossObjects){ BufferedImage mergedImage = null; try { int totalHeight = 0; int maxWidth = 0; List<BufferedImage> imageList = new ArrayList<BufferedImage>(); for(OSSObject ossObject:ossObjects){ BufferedImage image = ImageIO.read(ossObject.getObjectContent()); imageList.add(image); totalHeight+=image.getHeight(); if(image.getWidth() > maxWidth){ maxWidth = image.getWidth(); } } mergedImage = new BufferedImage(maxWidth,totalHeight,BufferedImage.TYPE_3BYTE_BGR); for(int i=0;i<imageList.size();i++){ for(int h=0;h<imageList.get(i).getHeight();h++){ for(int w=0;w<imageList.get(i).getWidth();w++){ if(i == 0){ mergedImage.setRGB(w,h,imageList.get(i).getRGB(w,h)); }else{ int usedHeight = 0; int currentImageIndex = i; while((currentImageIndex-1) >= 0){ usedHeight += imageList.get(currentImageIndex-1).getHeight(); currentImageIndex--; } mergedImage.setRGB(w,h+usedHeight,imageList.get(i).getRGB(w,h)); } } } } //ImageIO.write(mergedImage,IMAGE_FORMAT,new File("C:\file\merged2.png")); } catch (IOException e) { e.printStackTrace(); } return mergedImage; }