Java圖片合並


/**
  * 縱向合並圖片,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;
  }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM