Quantcast
Channel: IT社区推荐资讯 - ITIndex.net
Viewing all articles
Browse latest Browse all 11804

java 将两张相片合成一张,开发实用类

$
0
0

 实现将两压缩好的图片 写在一张画布上

思路,先成一个足够大的画布 放入两张相片

 

 

以下代码需要改进

 

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推荐




Viewing all articles
Browse latest Browse all 11804

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>