将几张图片合并为一张图片,返回byte数组

需求:通过url数组下载图片,再竖直合成一张新的图片,具体java代码如下

  1     /**
  2      * 竖直合并图片
  3      *
  4      * @param urls
  5      * @return
  6      */
  7     public static byte[] mergePic(String urls[]) {
  8         //BufferedImage转byte数组的格式
  9         String imgFormat = "jpg";
 10         //最终图片宽、高,所有图片中最宽
 11         int desWidth = 0, desHeight = 0, maxWidth = 0;
 12         byte[] ret = null;
 13         //图片集合
 14         List<BufferedImage> images = new ArrayList<BufferedImage>();
 15         //新图片
 16         BufferedImage desImage = null;
 17         //下载图片
 18         for (String url : urls) {
 19             BufferedImage image = downloadPic(url);
 20             if (image != null) {
 21                 images.add(image);
 22             }
 23         }
 24         //没有图片返回null
 25         if (images.size() < 1) {
 26             return ret;
 27         }
 28         //合并图片
 29         //计算合成的图片长高
 30         for (BufferedImage img : images) {
 31             desWidth = img.getWidth();
 32             //获取最宽的图片
 33             if (desWidth > maxWidth) {
 34                 maxWidth = desWidth;
 35             }
 36             desHeight += img.getHeight();
 37         }
 38         desWidth = maxWidth;
 39         //创建新图片
 40         desImage = new BufferedImage(desWidth, desHeight, BufferedImage.TYPE_INT_RGB);
 41         //合并子图到新图片
 42         //像素点起始x,y坐标
 43         int w_x = 0, h_y = 0;
 44         for (BufferedImage img : images) {
 45             int width = img.getWidth();
 46             int height = img.getHeight();
 47             //从子图片中读取rgb
 48             int[] rgbArr = new int[width * height];
 49             rgbArr = img.getRGB(0, 0, width, height, rgbArr, 0, width);
 50             //开始竖直方向合并
 51             desImage.setRGB(0, h_y, width, height, rgbArr, 0, width);
 52             //更新像素点起始x,y坐标
 53             w_x += width;
 54             h_y += height;
 55 
 56         }
 57         //BufferedImage转Byte数组,默认jpg
 58         ret = BufferedImage2Byte(desImage, imgFormat);
 59         return ret;
 60     }
 61 
 62     /**
 63      * 通过url下载图片,返回BufferedImage
 64      *
 65      * @param url
 66      * @return
 67      * @throws FileNotFoundException
 68      * 
 69      */
 70     public static BufferedImage downloadPic(String url) {
 71         try {
 72             Connection conn = Jsoup.connect(url);
 73             conn.method(Connection.Method.GET).timeout(10 * 1000).ignoreContentType(true);
 74             Response res = HttpUtil.request(conn, 3, 1 * 1000);
 75             byte[] body = res.bodyAsBytes();
 76             ByteArrayInputStream in = new ByteArrayInputStream(body);
 77             BufferedImage image = ImageIO.read(in);     //将in作为输入流,读取图片存入image
 78             return image;
 79         } catch (IOException ex) {
 80             java.util.logging.Logger.getLogger(PicUtil.class.getName()).log(Level.SEVERE, null, ex);
 81             return null;
 82         }
 83     }
 84 
 85     /**
 86      * 将BufferedImage转化为byte数组
 87      *
 88      * @param bImage
 89      * @param format 图片格式"png,jpeg,gif等"
 90      * @return
 91      */
 92     public static byte[] BufferedImage2Byte(BufferedImage bImage, String format) {
 93         byte[] ret = null;
 94         ByteArrayOutputStream out = new ByteArrayOutputStream();
 95         try {
 96             ImageIO.write(bImage, format, out);
 97             ret = out.toByteArray();
 98         } catch (IOException ex) {
 99             java.util.logging.Logger.getLogger(PicUtil.class.getName()).log(Level.SEVERE, null, ex);
100             return ret;
101         }
102         return ret;
103     }

 参考博客:

[1] Java 实现图片拼接
[2] BufferedImage与byte[]互转

posted @ 2021-03-16 13:10  SirPi  阅读(456)  评论(0编辑  收藏  举报