Java实现图片拼接

 更新时间:2018年08月07日 08:41:06   作者:Abel-Luan  
这篇文章主要为大家详细介绍了Java实现图片拼接的相关代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Java实现图片拼接的具体代码,供大家参考,具体内容如下

/**
 * 拼接图片(注:图片需长宽一致)
 * @param files  img1 ,img2
 * @param type  1:横向拼接 2:纵向拼接
 * @param targetFile 合成新的图片地址
 */
public static void mergeImage(String[] files, int type, String targetFile) {
 int len = files.length;
 if (len < 1) {
  throw new RuntimeException("图片数量小于1");
 }
 File[] src = new File[len];
 BufferedImage[] images = new BufferedImage[len];
 int[][] ImageArrays = new int[len][];
 for (int i = 0; i < len; i++) {
  try {
   src[i] = new File(files[i]);
   images[i] = ImageIO.read(src[i]);
  } catch (Exception e) {
   throw new RuntimeException(e);
  }
  int width = images[i].getWidth();
  int height = images[i].getHeight();
  ImageArrays[i] = new int[width * height];
  ImageArrays[i] = images[i].getRGB(0, 0, width, height, ImageArrays[i], 0, width);
 }
 int newHeight = 0;
 int newWidth = 0;
 for (int i = 0; i < images.length; i++) {
  // 横向
  if (type == 1) {
   newHeight = newHeight > images[i].getHeight() ? newHeight : images[i].getHeight();
   newWidth += images[i].getWidth();
  } else if (type == 2) {// 纵向
   newWidth = newWidth > images[i].getWidth() ? newWidth : images[i].getWidth();
   newHeight += images[i].getHeight();
  }
 }
 if (type == 1 && newWidth < 1) {
  return;
 }
 if (type == 2 && newHeight < 1) {
  return;
 }
 // 生成新图片
 try {
  BufferedImage ImageNew = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
  int height_i = 0;
  int width_i = 0;
  for (int i = 0; i < images.length; i++) {
   if (type == 1) {
    ImageNew.setRGB(width_i, 0, images[i].getWidth(), newHeight, ImageArrays[i], 0,
      images[i].getWidth());
    width_i += images[i].getWidth();
   } else if (type == 2) {
    ImageNew.setRGB(0, height_i, newWidth, images[i].getHeight(), ImageArrays[i], 0, newWidth);
    height_i += images[i].getHeight();
   }
  }
  //输出想要的图片
  ImageIO.write(ImageNew, targetFile.split("\\.")[1], new File(targetFile));
 } catch (Exception e) {
  throw new RuntimeException(e);
 }
}


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Springboot整合GuavaCache缓存过程解析

    Springboot整合GuavaCache缓存过程解析

    这篇文章主要介绍了springboot整合GuavaCache缓存过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-02-02
  • springboot入门篇HelloWorld(图文教程)

    springboot入门篇HelloWorld(图文教程)

    springboot springboot是一个全新的框架,它设计的目的简化spring项目的初始环境的搭建和开发,下面这篇文章主要给大家介绍了关于springboot入门篇HelloWorld的相关资料,需要的朋友可以参考下
    2023-12-12
  • Spring Boot 配置文件(application.yml、application-dev.yml、application-test.yml)

    Spring Boot 配置文件(application.yml、application-dev.y

    本文主要介绍了Spring Boot 配置文件,主要包含application.yml、application-dev.yml、application-test.yml,具有一定的参考价值,感兴趣的可以了解一下
    2024-03-03
  • Java 中解决Unsupported major.minor version 51.0的问题

    Java 中解决Unsupported major.minor version 51.0的问题

    本文主要介绍解决Unsupported major.minor version 51.0的问题, 这里给大家整理了详细资料,有需要的小伙伴可以参考下
    2016-08-08
  • Java将RTF转换为PDF格式的实现

    Java将RTF转换为PDF格式的实现

    本文主要介绍了Java将RTF转换为PDF格式的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-07-07
  • SpringBoot+Ant Design Vue实现数据导出功能方式

    SpringBoot+Ant Design Vue实现数据导出功能方式

    这篇文章主要介绍了SpringBoot+Ant Design Vue实现数据导出功能方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-01-01
  • java数据结构基础:稀疏数组

    java数据结构基础:稀疏数组

    今天带大家了解一下Java稀疏数组的相关知识,文中有非常详细的介绍及代码示例,对正在学习java的小伙伴们有很好地帮助,需要的朋友可以参考下
    2021-08-08
  • java解析Excel的方法(xls、xlsx两种格式)

    java解析Excel的方法(xls、xlsx两种格式)

    这篇文章主要介绍了java解析Excel的方法(xls、xlsx两种格式),需要的朋友可以参考下
    2018-04-04
  • SpringBoot 项目如何在tomcat容器中运行的实现方法

    SpringBoot 项目如何在tomcat容器中运行的实现方法

    这篇文章主要介绍了SpringBoot 项目如何在tomcat容器中运行的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-09-09
  • java中如何获取相关参数

    java中如何获取相关参数

    这篇文章主要介绍了java获取系统属性相关参数的方法,,需要的朋友可以参考下
    2015-07-07

最新评论