【转】java图片处理

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