Java Image合并

原创 2015年11月19日 15:12:26
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import org.junit.Test;

public class MergeImageUtils {
	
	/**
	 * 将两张图片合并为一张图片
	 * @param oneSrc
	 * @param twoSrc
	 * @param mergeSrc 合并后的图片路径
	 */
	public void mergeImage(String oneSrc, String twoSrc, String mergeSrc) {
		String postFix = mergeSrc.substring(mergeSrc.lastIndexOf(".") + 1, mergeSrc.length());
		try {
			File fileOne = new File(oneSrc);//读取第一张图片
			Image src = ImageIO.read(fileOne);  
			int width = src.getWidth(null);
			int height = src.getHeight(null);
			if(width > 900 || height > 900) {
				int num = (int) Math.ceil((double)width/900);
				int num2 = (int) Math.ceil((double)height/900);
				num = num > num2 ? num : num2;
				width = width/num;
				height = height/num;
			}
	        BufferedImage bufferedImageOne = null;
			if ("png".equalsIgnoreCase(postFix.toLowerCase())) {
				bufferedImageOne = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
			} else {
				bufferedImageOne = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
			}
			bufferedImageOne.getGraphics().drawImage(src.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0,  null);
			int[] imageArrayOne = new int[width * height];// 从图片中读取RGB
			imageArrayOne = bufferedImageOne.getRGB(0, 0, width, height, imageArrayOne, 0, width);
			
			File fileTwo = new File(twoSrc);//读取第二张图片
			src = ImageIO.read(fileTwo);  
			BufferedImage bufferedImageTwo = null;
			if ("png".equalsIgnoreCase(postFix.toLowerCase())) {
				bufferedImageTwo = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
			} else {
				bufferedImageTwo = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
			}
			bufferedImageTwo.getGraphics().drawImage(src.getScaledInstance(width, height,  Image.SCALE_SMOOTH), 0, 0,  null);
			int[] imageArrayTwo = new int[width * height];// 从图片中读取RGB
			imageArrayTwo = bufferedImageTwo.getRGB(0, 0, width, height, imageArrayTwo, 0, width);
			
			//生成新图片
			BufferedImage imageNew = new BufferedImage(width*2, height, BufferedImage.TYPE_INT_RGB);
			imageNew.setRGB(0 , 0, width, height, imageArrayOne, 0, width);		//设置左半部分的RGB
			imageNew.setRGB(width, 0, width, height, imageArrayTwo, 0, width);	//设置右半部分的RGB
			File outFile = new File(mergeSrc);
			//写图片
			ImageIO.write(imageNew, postFix, outFile);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
    
	@Test
    public void testMergeImage(){
    	mergeImage("D:/source/image/1.jpg", "D:/source/image/2.png", "D:/source/image/3.png");
	}
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

JAVA版的qr_image

  • 2017-05-12 15:37
  • 16KB
  • 下载

[LeetCode-Java]48. Rotate Image

You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).解1...

java-image-scaling-master

  • 2015-07-16 09:08
  • 7.55MB
  • 下载

Android(java):database disk image is malformed (code 11)

public class DatabaseHelper extends SQLiteOpenHelper {     //类没有实例化,是不能用作父类构造器的参数,必须声明为静态        ...

Java email send image

  • 2016-02-02 14:43
  • 1.83MB
  • 下载

java ---- image单图片输出 和 动画输出

java Applet  图片&动画 窗口 输出 图片: import java.applet.Applet; import java.awt.Graphics; import java.awt....
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)