当前位置: 移动技术网 > IT编程>开发语言>Java > java实现合并图片的方法示例

java实现合并图片的方法示例

2019年07月22日  | 移动技术网IT编程  | 我要评论
本文实例讲述了java实现合并图片的方法。分享给大家供大家参考,具体如下: package com.test; import java.io.file; im

本文实例讲述了java实现合并图片的方法。分享给大家供大家参考,具体如下:

package com.test;
import java.io.file;
import java.awt.image.bufferedimage;
import javax.imageio.imageio;
public class imagecombinetest {
  public static void main(string args[]) {
    try {
      // 读取第一张图片
      file fileone = new file("/users/coolcloud/pictures/art/lena-2.jpg");
      bufferedimage imageone = imageio.read(fileone);
      int width = imageone.getwidth();
      // 图片宽度
      int height = imageone.getheight();
      // 图片高度
      // 从图片中读取rgb
      int[] imagearrayone = new int[width * height];
      imagearrayone = imageone.getrgb(0, 0, width, height, imagearrayone,
      0, width);
      // 对第二张图片做相同的处理
      file filetwo = new file("/users/coolcloud/pictures/art/lena-2.jpg");
      bufferedimage imagetwo = imageio.read(filetwo);
      int[] imagearraytwo = new int[width * height];
      imagearraytwo = imagetwo.getrgb(0, 0, width, height, imagearraytwo,
      0, width);
      // 生成新图片
      // bufferedimage imagenew = new bufferedimage(width * 2, height,
      // bufferedimage.type_int_rgb);
      bufferedimage imagenew = new bufferedimage(width*2, height*2,
      bufferedimage.type_int_rgb);
      imagenew.setrgb(0, 0, width, height, imagearrayone, 0, width);
      // 设置左半部分的rgb
      // imagenew.setrgb(width, 0, width, height, imagearraytwo, 0, width);// 设置右半部分的rgb
      // imagenew.setrgb(0, height, width, imageone.getheight()+imagetwo.getheight(), imagearraytwo, 0, width);// 设置右半部分的rgb
      imagenew.setrgb(0, height, width, height, imagearraytwo, 0, width);
      // 设置右半部分的rgb
      file outfile = new file("/users/coolcloud/pictures/generatepic.jpg");
      imageio.write(imagenew, "png", outfile);
      // 写图片
    }
    catch (exception e) {
      e.printstacktrace();
    }
  }
}

更多java相关内容感兴趣的读者可查看本站专题:《java图片操作技巧汇总》、《java日期与时间操作技巧汇总》、《java操作dom节点技巧总结》、《java文件与目录操作技巧汇总》及《java数据结构与算法教程》。

希望本文所述对大家java程序设计有所帮助。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网