在图片上写字

来源:互联网 发布:win7 如何制作mac系统 编辑:程序博客网 时间:2024/06/11 08:34
这是一个公共类:
package cn.kangban.web.controller.order;import javax.imageio.ImageIO;  import org.apache.log4j.Logger;import org.springframework.stereotype.Controller;import cn.kangban.web.controller.MasterAction;import cn.kangban.web.util.ImgResizeUtil;import cn.kangban.web.util.UploadImgUtil;import java.awt.Color;  import java.awt.Font;  import java.awt.Graphics2D;  import java.awt.RenderingHints;import java.awt.image.BufferedImage;  import java.io.File;  import java.io.IOException;  import java.io.InputStream;import java.net.URL;  import java.util.Properties;@Controllerpublic class AccreditPicAction extends MasterAction {private static Logger logger = Logger.getLogger(AccreditPicAction.class);    private Font font = new Font("微软雅黑", Font.PLAIN, 25);// 添加字体的属性设置        private Font font1 = new Font("微软雅黑", Font.PLAIN, 50);// 添加字体的属性设置         private Font font2 = new Font("微软雅黑", Font.PLAIN, 50);// 添加字体的属性设置         private Graphics2D g = null;        private int fontsize = 0;        private int x = 0;        private int y = 0;        /**      * 导入本地图片到缓冲区      */      public BufferedImage loadImageLocal(String imgName) {          try {              return ImageIO.read(new File(imgName));          } catch (IOException e) {              System.out.println(e.getMessage());          }          return null;      }        /**      * 导入网络图片到缓冲区      */      public BufferedImage loadImageUrl(String imgName) {          try {              URL url = new URL(imgName);              return ImageIO.read(url);          } catch (IOException e) {              System.out.println(e.getMessage());          }          return null;      }        /**      * 生成新图片到本地      */      public void writeImageLocal(String newImage, BufferedImage img) {          if (newImage != null && img != null) {              try {                  File outputfile = new File(newImage);                  ImageIO.write(img, "jpg", outputfile);              } catch (IOException e) {                  System.out.println(e.getMessage());              }          }      }        /**      * 设定文字的字体等      */      public void setFont(String fontStyle, int fontSize) {          this.fontsize = fontSize;          this.font = new Font(fontStyle, Font.PLAIN, fontSize);      }        /**      * 修改图片,返回修改后的图片缓冲区(只输出一行文本)      */      public BufferedImage modifyImage(BufferedImage img, Object content, int x,              int y) {            try {              int w = img.getWidth();              int h = img.getHeight();              g = img.createGraphics();              g.setBackground(Color.WHITE);             g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);            g.setColor(Color.black);//设置字体颜色              if (this.font != null)                  g.setFont(this.font);              // 验证输出位置的纵坐标和横坐标              if (x >= w || y >= h) {                  this.x = h - this.fontsize + 2;                  this.y = w;              } else {                  this.x = x;                  this.y = y;              }              if (content != null) {                  g.drawString(content.toString(), this.x, this.y);              }              g.dispose();          } catch (Exception e) {              System.out.println(e.getMessage());          }            return img;      }      /**      * 修改图片,返回修改后的图片缓冲区(只输出一行文本)      */      public BufferedImage modifyImage1(BufferedImage img, Object content, int x,              int y) {            try {              int w = img.getWidth();              int h = img.getHeight();              g = img.createGraphics();              g.setBackground(Color.WHITE);             g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);            g.setColor(Color.black);//设置字体颜色              if (this.font1 != null)                  g.setFont(this.font1);              // 验证输出位置的纵坐标和横坐标              if (x >= w || y >= h) {                  this.x = h - this.fontsize + 2;                  this.y = w;              } else {                  this.x = x;                  this.y = y;              }              if (content != null) {                  g.drawString(content.toString(), this.x, this.y);              }              g.dispose();          } catch (Exception e) {              System.out.println(e.getMessage());          }            return img;      }      /**      * 修改图片,返回修改后的图片缓冲区(只输出一行文本)      */      public BufferedImage modifyImage2(BufferedImage img, Object content, int x,              int y) {            try {              int w = img.getWidth();              int h = img.getHeight();              g = img.createGraphics();              g.setBackground(Color.WHITE);             g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);            g.setColor(Color.black);//设置字体颜色              if (this.font2 != null)                  g.setFont(this.font2);              // 验证输出位置的纵坐标和横坐标              if (x >= w || y >= h) {                  this.x = h - this.fontsize + 2;                  this.y = w;              } else {                  this.x = x;                  this.y = y;              }              if (content != null) {                  g.drawString(content.toString(), this.x, this.y);              }              g.dispose();          } catch (Exception e) {              System.out.println(e.getMessage());          }            return img;      }        /**      * 修改图片,返回修改后的图片缓冲区(输出多个文本段) xory:true表示将内容在一行中输出;false表示将内容多行输出      */      public BufferedImage modifyImage(BufferedImage img, Object[] contentArr,              int x, int y, boolean xory) {          try {              int w = img.getWidth();              int h = img.getHeight();              g = img.createGraphics();              g.setBackground(Color.WHITE);              g.setColor(Color.RED);              if (this.font != null)                  g.setFont(this.font);              // 验证输出位置的纵坐标和横坐标              if (x >= h || y >= w) {                  this.x = h - this.fontsize + 2;                  this.y = w;              } else {                  this.x = x;                  this.y = y;              }              if (contentArr != null) {                  int arrlen = contentArr.length;                  if (xory) {                      for (int i = 0; i < arrlen; i++) {                          g.drawString(contentArr[i].toString(), this.x, this.y);                          this.x += contentArr[i].toString().length()                                  * this.fontsize / 2 + 5;// 重新计算文本输出位置                      }                  } else {                      for (int i = 0; i < arrlen; i++) {                          g.drawString(contentArr[i].toString(), this.x, this.y);                          this.y += this.fontsize + 2;// 重新计算文本输出位置                      }                  }              }              g.dispose();          } catch (Exception e) {              System.out.println(e.getMessage());          }            return img;      }        /**      * 修改图片,返回修改后的图片缓冲区(只输出一行文本)      *       * 时间:2007-10-8      *       * @param img      * @return      */      public BufferedImage modifyImageYe(BufferedImage img) {            try {              int w = img.getWidth();              int h = img.getHeight();              g = img.createGraphics();              g.setBackground(Color.WHITE);              g.setColor(Color.blue);//设置字体颜色              if (this.font != null)                  g.setFont(this.font);              g.drawString("www.hi.baidu.com?xia_mingjian", w - 85, h - 5);              g.dispose();          } catch (Exception e) {              System.out.println(e.getMessage());          }            return img;      }        public BufferedImage modifyImagetogeter(BufferedImage b, BufferedImage d) {            try {              int w = b.getWidth();              int h = b.getHeight();                              g = d.createGraphics();              g.drawImage(b, 100, 10, w, h, null);              g.dispose();          } catch (Exception e) {              System.out.println(e.getMessage());          }            return d;      }        public static void main(String[] args) {        AccreditPicAction tt = new AccreditPicAction();         Properties prop = getPropertiesFile();String props = prop.getProperty("filepath");        BufferedImage d = tt.loadImageLocal(props);           String ad = "京307100001";        String agentname = "徐建/德力集团";        String wx = "weixin";        String a = "大中华区";        String b = "金牌合伙人";        String c = "眼护士眼罩";        String e = "2015年4月1日";String propes = prop.getProperty("imagepath");        if(UploadImgUtil.newFolder(propes)){        tt.writeImageLocal(propes,tt.modifyImage(d,ad,352,512));          tt.writeImageLocal(propes,tt.modifyImage(d,agentname,395,574));         tt.writeImageLocal(propes,tt.modifyImage(d,wx,316,628));         tt.writeImageLocal(propes,tt.modifyImage(d,a,202,688));         tt.writeImageLocal(propes,tt.modifyImage(d,b,499,688));         tt.writeImageLocal(propes,tt.modifyImage(d,c,624,750));         tt.writeImageLocal(propes,tt.modifyImage(d,e,524,1138));        System.out.println("success");         }            }      /** * 读取Properties配置文件 * @return 返回Properties数据 */public static Properties getPropertiesFile(){InputStream is = UploadImgUtil.class.getClassLoader().getResourceAsStream("config.properties");Properties prop = new Properties();    try {       prop.load(is);    is.close();    } catch (IOException ex) {    logger.error("读取配置文件失败,配置文件路径为:config.properties", ex);    }    return prop;}  }
调用公共类方法将字写在图片上
<pre name="code" class="java">private String createAccredit(Long agentid,String ad,String agentname ,String wx ,String identity,String a,String agentlevel,String product,String t){AccreditPicAction tt = new AccreditPicAction();AccreditPicAction tts = new AccreditPicAction();String wx1=wx.substring(0, 1);String wx2=wx.substring(wx.length()-1, wx.length());Integer s = agentname.length();Integer l = agentlevel.length();Integer namel = (900-50*s)/2;Integer level = (900-50*l)/2;Properties prop = getPropertiesFile();String props = prop.getProperty("filepath");String propes = prop.getProperty("imagepath");ServletContext s1=this.getServletContext();String temp=s1.getRealPath("/");Date date = new Date();Integer year = DateUtil.getYear(date);Integer month = DateUtil.getMonth(date);String aid = year + "/"+month+"/"+agentid+"/"+IdUtil.getIdObject();String path = propes+"auther/"+aid;String image = path +"_900_1273.jpg";String images = path +"_min_900_1273.jpg";String imagemin = path+"_300_424.jpg";String imagemins = path+"_min_300_424.jpg";        BufferedImage d = tt.loadImageLocal(props);        BufferedImage ds = tts.loadImageLocal(props);        if(UploadImgUtil.newFolder(image)){        tt.writeImageLocal(image,tt.modifyImage(d,ad,396,772));          tt.writeImageLocal(image,tt.modifyImage1(d,agentname,namel,535));         tt.writeImageLocal(image,tt.modifyImage(d,wx,396,823));         tt.writeImageLocal(image,tt.modifyImage(d,identity,396,877));         tt.writeImageLocal(image,tt.modifyImage2(d,agentlevel,level,720));         tt.writeImageLocal(image,tt.modifyImage(d,t,316,950));         ImgResizeUtil.reduceImg(image, imagemin, 33, 33);        }        if(UploadImgUtil.newFolder(images)){        tts.writeImageLocal(images,tts.modifyImage(ds,ad,396,772));          tts.writeImageLocal(images,tts.modifyImage1(ds,agentname,namel,535));         tts.writeImageLocal(images,tts.modifyImage(ds,wx1+"****"+wx2,396,823));         tts.writeImageLocal(images,tts.modifyImage(ds,"******************",396,877));         tts.writeImageLocal(images,tts.modifyImage2(ds,agentlevel,level,720));         tts.writeImageLocal(images,tts.modifyImage(ds,t,316,950));         ImgResizeUtil.reduceImg(images, imagemins, 33, 33);        }        return aid; }



0 0