java 将两张相片合成一张,开发实用类
实现将两压缩好的图片 写在一张画布上
思路,先成一个足够大的画布 放入两张相片
以下代码需要改进
import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import javax.imageio.ImageIO;
public class IMG {
/**
*
* @param filesrc
* @param logosrc
* @param outsrc
* @param x 位置
* @param y 位置
*/
public void composePic(String filesrc,String logosrc,String outsrc,int x,int y) {
try {
File bgfile = new File(filesrc);
Image bg_src = javax.imageio.ImageIO.read(bgfile);
File logofile = new File(logosrc);
Image logo_src = javax.imageio.ImageIO.read(logofile);
int bg_width = bg_src.getWidth(null);
int bg_height = bg_src.getHeight(null);
int logo_width = logo_src.getWidth(null);;
int logo_height = logo_src.getHeight(null);
BufferedImage tag = new BufferedImage(bg_width, bg_height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = tag.createGraphics();
g2d.drawImage(bg_src, 0, 0, bg_width, bg_height, null);
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,1.0f)); //透明度设置开始
g2d.drawImage(logo_src,x,y,logo_width,logo_height, null);
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER)); //透明度设置 结束
FileOutputStream out = new FileOutputStream(outsrc);
ImageIO.write(tag, "jpg", out);//写图片
/* JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag);*/
out.close();
}catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String args[]) {
Long star = System.currentTimeMillis();
IMG pic = new IMG();
pic.composePic("f:\\7.jpg","f:\\6.jpg","f:\\out.jpg",490,360);
Long end =System.currentTimeMillis();
System.out.print("time====:"+(end-star));
}
}
已有 0 人发表留言,猛击->> 这里<<-参与讨论
ITeye推荐
相关 [java 张相 合成] 推荐:
Java中的锁(Locks in Java)
- - 并发编程网 - ifeve.comJava PaaS 对决
- 呆瓜 - IBM developerWorks 中国 : 文档库Java浮点数
- d0ngd0ng - 译言-电脑/网络/数码科技Qt——转战Java?
- - 博客 - 伯乐在线java 验证码
- - ITeye博客Java异常
- - CSDN博客推荐文章java面试题
- - Java - 编程语言 - ITeye博客Java使用memcached
- - 互联网 - ITeye博客c:>memcached.exe -l 127.0.0.1 -m 32 -d start.