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


  • 2017-01-21 21:44:29

    详解 ESLint 规则,规范你的代码

    在很久之前就想通过工具来规范自己的代码风格,减少程序出错的概率,如果看过我的 一个前端程序猿的Sublime Text3的自我修养 ,这篇博客的朋友,肯定知道在当时我使用 SublimeLinter-jshint 插件来规范风格,但是实际上一直懒癌发作也没去看它的文档,使用着它默认的规则。不过现在是时候切换到 ESLint 了!

  • 2017-01-23 23:09:16

    使用 CSS3 实现超炫的 Loading(加载)动画效果

     SpinKit 是一套网页动画效果,包含8种基于 CSS3 实现的很炫的加载动画。借助 CSS3 Animation 的强大功能来创建平滑,易于定制的动画。SpinKit 的目标不是提供一个每个浏览器都兼容的解决方案,而是给现代浏览器提供更优的技术实现方案和更佳的使用体验。(为保证最佳的效果,请在 Chrome、Firefox 和 Safari 等现代浏览器中浏览)