Java 实现图片拼接

2019-05-08 18:04:05

参考地址 Java 实现图片拼接

用于将多个图片拼接成一张图片


/**
* 合并任数量的图片成一张图片
*
* @param isHorizontal
*            true代表水平合并,fasle代表垂直合并
* @param imgs
*            待合并的图片数组
* @return
* @throws IOException
*/
private BufferedImage mergeImage(boolean isHorizontal, BufferedImage... imgs) throws IOException {
   // 生成新图片
   BufferedImage destImage = null;
   // 计算新图片的长和高
   int allw = 0, allh = 0, allwMax = 0, allhMax = 0;
   // 获取总长、总宽、最长、最宽
   for (int i = 0; i < imgs.length; i++) {
       BufferedImage img = imgs[i];
       allw += img.getWidth();
       allh += img.getHeight();
       if (img.getWidth() > allwMax) {
           allwMax = img.getWidth();
       }
       if (img.getHeight() > allhMax) {
           allhMax = img.getHeight();
       }
   }
   // 创建新图片
   if (isHorizontal) {
       destImage = new BufferedImage(allw, allhMax, BufferedImage.TYPE_INT_RGB);
   } else {
       destImage = new BufferedImage(allwMax, allh, BufferedImage.TYPE_INT_RGB);
   }
   // 合并所有子图片到新图片
   int wx = 0, wy = 0;
   for (int i = 0; i < imgs.length; i++) {
       BufferedImage img = imgs[i];
       int w1 = img.getWidth();
       int h1 = img.getHeight();
       // 从图片中读取RGB
       int[] ImageArrayOne = new int[w1 * h1];
       ImageArrayOne = img.getRGB(0, 0, w1, h1, ImageArrayOne, 0, w1); // 逐行扫描图像中各个像素的RGB到数组中
       if (isHorizontal) { // 水平方向合并
           destImage.setRGB(wx, 0, w1, h1, ImageArrayOne, 0, w1); // 设置上半部分或左半部分的RGB
       } else { // 垂直方向合并
           destImage.setRGB(0, wy, w1, h1, ImageArrayOne, 0, w1); // 设置上半部分或左半部分的RGB
       }
       wx += w1;
       wy += h1;
   }
   return destImage;
}


  • 2020-05-12 10:17:07

    createElementNS和createElement区别

    指定与元素相关联的命名空间URI的字符串。创建的元素的namespaceURI属性使用namespaceURI的值进行初始化。 参见有效的命名空间URL。

  • 2020-05-13 09:37:50

    transform-origin(变形原点) 怎么用

    transform-origin是变形原点,也就是该元素围绕着那个点变形或旋转,该属性只有在设置了transform属性的时候起作用

  • 2020-05-13 09:56:35

    Could not find method google() for arguments [] on repository container.

    1、打开项目根目录下android/gradle/wrapper/gradle-wrapper.properties 将distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip中的2.14.1改成4.1 ———————————————— 版权声明:本文为CSDN博主「peachesTao」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/taoerchun/article/details/93870941

  • 2020-05-13 10:05:23

    inline svg想写介绍以及使用

    inline svg是目前前端图标解决方案的最优解(当然不仅限于图标),而且使用方式也及其简单,只要将svg图标代码当成普通的html元素来使用即可