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


  • 2021-01-24 10:08:57

    ios后台播放视频

    很多开发者以为AVPlayer不能在后台播放视频:应用退到后台,但能播放视频的声音(ps:不是通过切换相同的音频来实现),我在开发SDK的过程中也遇到这个需求,所幸解决了这个问题。下面我就来讲讲实现的过程。

  • 2021-01-24 10:10:22

    XCode提示build successed,无法启动模拟器

    使用phonegap(cordova)创建项目后,开始时使用Xcode6.1或命令行运行项目都可启动模拟器调试;不知道什么原因命令行运行项目完全没有问题,但是Xcode运行,提示build successed,但是始终无法启动模拟器。

  • 2021-01-24 10:25:00

    iOS中的加号和减号方法

    在OC中,方法分为类方法和实例方法。 前置加号(+)的方法为类方法,这类方法是可以直接用类名来调用的,它的作用主要是创建一个实例。有人把它称为创建实例的工厂方法。 前置减号(-)的方法为实例方法,必须使用类的实例才可以调用的。

  • 2021-02-02 09:42:14

    nuxt.js抽去css文件css代码过多不利于seo

    关于nuxt.js的资料并不是很多,有时候遇到个很简单的问题,百度或者谷歌都不是很容易找到。 其实这个问题就很简单,但是也让我浪费了很多时间,所以在此共勉。