java实现把两张图片合并

  1. package cn.com.bmsoft.util;  
  2.   
  3. import java.awt.Color;  
  4. import java.awt.Font;  
  5. import java.awt.Graphics2D;  
  6. import java.awt.image.BufferedImage;  
  7. import java.io.File;  
  8. import java.io.IOException;  
  9. import java.net.URL;  
  10.   
  11. import javax.imageio.ImageIO;  
  12.   
  13. /** 
  14.  * 把两张图片合并 
  15.  * @author lizhiyong  
  16.  * @version $Id: Pic.java, v 0.1 
  17.         2015-6-3 下午3:21:23 1111 Exp $ 
  18.  */  
  19. public class Pic {  
  20.     private Font       font     = new Font("宋体", Font.PLAIN, 12); // 添加字体的属性设置    
  21.   
  22.     private Graphics2D g        = null;  
  23.   
  24.     private int        fontsize = 0;  
  25.   
  26.     private int        x        = 0;  
  27.   
  28.     private int        y        = 0;  
  29.   
  30.     /**  
  31.      * 导入本地图片到缓冲区  
  32.      */  
  33.     public BufferedImage loadImageLocal(String imgName) {  
  34.         try {  
  35.             return ImageIO.read(new File(imgName));  
  36.         } catch (IOException e) {  
  37.             System.out.println(e.getMessage());  
  38.         }  
  39.         return null;  
  40.     }  
  41.   
  42.     /**  
  43.      * 导入网络图片到缓冲区  
  44.      */  
  45.     public BufferedImage loadImageUrl(String imgName) {  
  46.         try {  
  47.             URL url = new URL(imgName);  
  48.             return ImageIO.read(url);  
  49.         } catch (IOException e) {  
  50.             System.out.println(e.getMessage());  
  51.         }  
  52.         return null;  
  53.     }  
  54.   
  55.     /**  
  56.      * 生成新图片到本地  
  57.      */  
  58.     public void writeImageLocal(String newImage, BufferedImage img) {  
  59.         if (newImage != null && img != null) {  
  60.             try {  
  61.                 File outputfile = new File(newImage);  
  62.                 ImageIO.write(img, "jpg", outputfile);  
  63.             } catch (IOException e) {  
  64.                 System.out.println(e.getMessage());  
  65.             }  
  66.         }  
  67.     }  
  68.   
  69.     /**  
  70.      * 设定文字的字体等  
  71.      */  
  72.     public void setFont(String fontStyle, int fontSize) {  
  73.         this.fontsize = fontSize;  
  74.         this.font = new Font(fontStyle, Font.PLAIN, fontSize);  
  75.     }  
  76.   
  77.     /**  
  78.      * 修改图片,返回修改后的图片缓冲区(只输出一行文本)  
  79.      */  
  80.     public BufferedImage modifyImage(BufferedImage img, Object content, int x, int y) {  
  81.   
  82.         try {  
  83.             int w = img.getWidth();  
  84.             int h = img.getHeight();  
  85.             g = img.createGraphics();  
  86.             g.setBackground(Color.WHITE);  
  87.             g.setColor(Color.orange);//设置字体颜色    
  88.             if (this.font != null)  
  89.                 g.setFont(this.font);  
  90.             // 验证输出位置的纵坐标和横坐标    
  91.             if (x >= h || y >= w) {  
  92.                 this.x = h - this.fontsize + 2;  
  93.                 this.y = w;  
  94.             } else {  
  95.                 this.x = x;  
  96.                 this.y = y;  
  97.             }  
  98.             if (content != null) {  
  99.                 g.drawString(content.toString(), this.x, this.y);  
  100.             }  
  101.             g.dispose();  
  102.         } catch (Exception e) {  
  103.             System.out.println(e.getMessage());  
  104.         }  
  105.   
  106.         return img;  
  107.     }  
  108.   
  109.     /**  
  110.      * 修改图片,返回修改后的图片缓冲区(输出多个文本段) xory:true表示将内容在一行中输出;false表示将内容多行输出  
  111.      */  
  112.     public BufferedImage modifyImage(BufferedImage img, Object[] contentArr, int x, int y,  
  113.                                      boolean xory) {  
  114.         try {  
  115.             int w = img.getWidth();  
  116.             int h = img.getHeight();  
  117.             g = img.createGraphics();  
  118.             g.setBackground(Color.WHITE);  
  119.             g.setColor(Color.RED);  
  120.             if (this.font != null)  
  121.                 g.setFont(this.font);  
  122.             // 验证输出位置的纵坐标和横坐标    
  123.             if (x >= h || y >= w) {  
  124.                 this.x = h - this.fontsize + 2;  
  125.                 this.y = w;  
  126.             } else {  
  127.                 this.x = x;  
  128.                 this.y = y;  
  129.             }  
  130.             if (contentArr != null) {  
  131.                 int arrlen = contentArr.length;  
  132.                 if (xory) {  
  133.                     for (int i = 0; i < arrlen; i++) {  
  134.                         g.drawString(contentArr[i].toString(), this.x, this.y);  
  135.                         this.x += contentArr[i].toString().length() * this.fontsize / 2 + 5;// 重新计算文本输出位置    
  136.                     }  
  137.                 } else {  
  138.                     for (int i = 0; i < arrlen; i++) {  
  139.                         g.drawString(contentArr[i].toString(), this.x, this.y);  
  140.                         this.y += this.fontsize + 2;// 重新计算文本输出位置    
  141.                     }  
  142.                 }  
  143.             }  
  144.             g.dispose();  
  145.         } catch (Exception e) {  
  146.             System.out.println(e.getMessage());  
  147.         }  
  148.   
  149.         return img;  
  150.     }  
  151.   
  152.     /**  
  153.      * 修改图片,返回修改后的图片缓冲区(只输出一行文本)  
  154.      *   
  155.      * 时间:2007-10-8  
  156.      *   
  157.      * @param img  
  158.      * @return  
  159.      */  
  160.     public BufferedImage modifyImageYe(BufferedImage img) {  
  161.   
  162.         try {  
  163.             int w = img.getWidth();  
  164.             int h = img.getHeight();  
  165.             g = img.createGraphics();  
  166.             g.setBackground(Color.WHITE);  
  167.             g.setColor(Color.blue);//设置字体颜色    
  168.             if (this.font != null)  
  169.                 g.setFont(this.font);  
  170.             g.drawString("www.hi.baidu.com?xia_mingjian", w - 85, h - 5);  
  171.             g.dispose();  
  172.         } catch (Exception e) {  
  173.             System.out.println(e.getMessage());  
  174.         }  
  175.   
  176.         return img;  
  177.     }  
  178.   
  179.     public BufferedImage modifyImagetogeter(BufferedImage b, BufferedImage d) {  
  180.   
  181.         try {  
  182.             int w = b.getWidth();  
  183.             int h = b.getHeight();  
  184.   
  185.             g = d.createGraphics();  
  186.             g.drawImage(b, 10020, w, h, null);  
  187.             g.dispose();  
  188.         } catch (Exception e) {  
  189.             System.out.println(e.getMessage());  
  190.         }  
  191.   
  192.         return d;  
  193.     }  
  194.   
  195.     public static void main(String[] args) {  
  196.   
  197.         Pic tt = new Pic();  
  198.   
  199.         BufferedImage d = tt.loadImageLocal("\\ploanshare\\2\\11.jpg");  
  200.         BufferedImage b = tt.loadImageLocal("\\ploanshare\\2\\22.png");    
  201.         //往图片上写文件    
  202.         //tt.writeImageLocal("E:\\ploanshare\\2\\22.jpg", tt.modifyImage(d, "000000", 90, 90));  
  203.   
  204.         tt.writeImageLocal("\\ploanshare\\2\\cc.jpg", tt.modifyImagetogeter(b, d));    
  205.         //将多张图片合在一起    
  206.         System.out.println("success");  
  207.     }  
  208. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值