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;
  }

 

转载于:https://www.cnblogs.com/lzmrex/p/9644442.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值