欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java生成二维码可添加logo和文字功能

程序员文章站 2024-03-06 16:38:56
废话不多说,直接给大家贴代码了,具体代码如下所示: package com.luo.wctweb.util; import java.awt.color;...

废话不多说,直接给大家贴代码了,具体代码如下所示:

package com.luo.wctweb.util; 
 import java.awt.color; 
 import java.awt.font; 
 import java.awt.graphics2d; 
 import java.awt.image.bufferedimage; 
 import java.io.bytearrayoutputstream; 
 import java.io.file; 
 import java.util.date; 
 import java.util.hashmap; 
 import java.util.map; 
 import javax.imageio.imageio; 
 import javax.servlet.http.httpservletrequest; 
 import com.google.zxing.barcodeformat; 
 import com.google.zxing.encodehinttype; 
 import com.google.zxing.multiformatwriter; 
 import com.google.zxing.writerexception; 
 import com.google.zxing.common.bitmatrix; 
 import com.google.zxing.qrcode.decoder.errorcorrectionlevel; 
 import com.lz.lsf.util.base64; 
 /** * @description: (二维码) * @author:luoguohui * @date:2015-10-29 下午05:27:13 */ 
 public class zxingcode { 
  private static final int qrcolor = 0xff000000; //默认是黑色 
  private static final int bgwhite = 0xffffffff; //背景颜色 
  public static void main(string[] args) throws writerexception  
  {  
   try  
   {  
    getlogoqrcode("https://www.baidu.com/", null, "跳转到百度的二维码"); 
   }  
   catch (exception e)  
   {  
    e.printstacktrace();  
   }  
  }  
  /** * 生成带logo的二维码图片 * * @param qrpic * @param logopic */ 
  public static string getlogoqrcode(string qrurl,httpservletrequest request,string productname) 
  { 
 // string filepath = request.getsession().getservletcontext().getrealpath("/") + "resources/images/logoimages/llhlogo.png"; 
   //filepath是二维码logo的路径,但是实际中我们是放在项目的某个路径下面的,所以路径用上面的,把下面的注释就好 
   string filepath = "c:/users/luoguohui/desktop/78310a55b319ebc4fa3aef658126cffc1f17168f.jpg"; //todo  
   string content = qrurl; 
   try 
   {  
    zxingcode zp = new zxingcode(); 
    bufferedimage bim = zp.getqr_codebufferedimage(content, barcodeformat.qr_code, 400, 400, zp.getdecodehinttype()); 
    return zp.addlogo_qrcode(bim, new file(filepath), new logoconfig(), productname); 
   } 
   catch (exception e) 
   { 
    e.printstacktrace(); 
   } 
   return null; 
  } 
  /** * 给二维码图片添加logo * * @param qrpic * @param logopic */ 
  public string addlogo_qrcode(bufferedimage bim, file logopic, logoconfig logoconfig, string productname) 
  { 
   try 
   { 
    /** * 读取二维码图片,并构建绘图对象 */ 
    bufferedimage image = bim; 
    graphics2d g = image.creategraphics(); 
    /** * 读取logo图片 */ 
    bufferedimage logo = imageio.read(logopic); 
    /** * 设置logo的大小,本人设置为二维码图片的20%,因为过大会盖掉二维码 */ 
    int widthlogo = logo.getwidth(null)>image.getwidth()*3/10?(image.getwidth()*3/10):logo.getwidth(null),  
     heightlogo = logo.getheight(null)>image.getheight()*3/10?(image.getheight()*3/10):logo.getwidth(null); 
    /** * logo放在中心 */ 
    int x = (image.getwidth() - widthlogo) / 2; 
    int y = (image.getheight() - heightlogo) / 2; 
    /** * logo放在右下角 * int x = (image.getwidth() - widthlogo); * int y = (image.getheight() - heightlogo); */ 
    //开始绘制图片 
    g.drawimage(logo, x, y, widthlogo, heightlogo, null); 
 // g.drawroundrect(x, y, widthlogo, heightlogo, 15, 15); 
 // g.setstroke(new basicstroke(logoconfig.getborder())); 
 // g.setcolor(logoconfig.getbordercolor()); 
 // g.drawrect(x, y, widthlogo, heightlogo); 
    g.dispose(); 
    //把商品名称添加上去,商品名称不要太长哦,这里最多支持两行。太长就会自动截取啦 
    if (productname != null && !productname.equals("")) { 
     //新的图片,把带logo的二维码下面加上文字 
     bufferedimage outimage = new bufferedimage(400, 445, bufferedimage.type_4byte_abgr); 
     graphics2d outg = outimage.creategraphics(); 
     //画二维码到新的面板 
     outg.drawimage(image, 0, 0, image.getwidth(), image.getheight(), null); 
     //画文字到新的面板 
     outg.setcolor(color.black);  
     outg.setfont(new font("宋体",font.bold,30)); //字体、字型、字号  
     int strwidth = outg.getfontmetrics().stringwidth(productname); 
     if (strwidth > 399) { 
 // //长度过长就截取前面部分 
 // outg.drawstring(productname, 0, image.getheight() + (outimage.getheight() - image.getheight())/2 + 5 ); //画文字 
      //长度过长就换行 
      string productname1 = productname.substring(0, productname.length()/2); 
      string productname2 = productname.substring(productname.length()/2, productname.length()); 
      int strwidth1 = outg.getfontmetrics().stringwidth(productname1); 
      int strwidth2 = outg.getfontmetrics().stringwidth(productname2); 
      outg.drawstring(productname1, 200 - strwidth1/2, image.getheight() + (outimage.getheight() - image.getheight())/2 + 12 ); 
      bufferedimage outimage2 = new bufferedimage(400, 485, bufferedimage.type_4byte_abgr); 
      graphics2d outg2 = outimage2.creategraphics(); 
      outg2.drawimage(outimage, 0, 0, outimage.getwidth(), outimage.getheight(), null); 
      outg2.setcolor(color.black);  
      outg2.setfont(new font("宋体",font.bold,30)); //字体、字型、字号  
      outg2.drawstring(productname2, 200 - strwidth2/2, outimage.getheight() + (outimage2.getheight() - outimage.getheight())/2 + 5 ); 
      outg2.dispose();  
      outimage2.flush(); 
      outimage = outimage2; 
     }else { 
      outg.drawstring(productname, 200 - strwidth/2 , image.getheight() + (outimage.getheight() - image.getheight())/2 + 12 ); //画文字  
     } 
     outg.dispose();  
     outimage.flush(); 
     image = outimage; 
    } 
    logo.flush(); 
    image.flush(); 
    bytearrayoutputstream baos = new bytearrayoutputstream(); 
    baos.flush(); 
    imageio.write(image, "png", baos); 
    //二维码生成的路径,但是实际项目中,我们是把这生成的二维码显示到界面上的,因此下面的折行代码可以注释掉 
    //可以看到这个方法最终返回的是这个二维码的imagebase64字符串 
    //前端用 <img src="data:image/png;base64,${imagebase64qrcode}"/> 其中${imagebase64qrcode}对应二维码的imagebase64字符串 
    imageio.write(image, "png", new file("c:/users/luoguohui/desktop/tdc-" + new date().gettime() + "test.png")); //todo  
    string imagebase64qrcode = base64.bytearraytobase64(baos.tobytearray()); 
    baos.close(); 
    return imagebase64qrcode; 
   } 
   catch (exception e) 
   { 
    e.printstacktrace(); 
   } 
   return null; 
  } 
  /** * 构建初始化二维码 * * @param bm * @return */ 
  public bufferedimage filetobufferedimage(bitmatrix bm) 
  { 
   bufferedimage image = null; 
   try 
   { 
    int w = bm.getwidth(), h = bm.getheight(); 
    image = new bufferedimage(w, h, bufferedimage.type_int_rgb); 
    for (int x = 0; x < w; x++) 
    { 
     for (int y = 0; y < h; y++) 
     { 
      image.setrgb(x, y, bm.get(x, y) ? 0xff000000 : 0xffccddee); 
     } 
    } 
   } 
   catch (exception e) 
   { 
    e.printstacktrace(); 
   } 
   return image; 
  } 
  /** * 生成二维码bufferedimage图片 * * @param content * 编码内容 * @param barcodeformat * 编码类型 * @param width * 图片宽度 * @param height * 图片高度 * @param hints * 设置参数 * @return */ 
  public bufferedimage getqr_codebufferedimage(string content, barcodeformat barcodeformat, int width, int height, map<encodehinttype, ?> hints) 
  { 
   multiformatwriter multiformatwriter = null; 
   bitmatrix bm = null; 
   bufferedimage image = null; 
   try 
   { 
    multiformatwriter = new multiformatwriter(); 
    // 参数顺序分别为:编码内容,编码类型,生成图片宽度,生成图片高度,设置参数 
    bm = multiformatwriter.encode(content, barcodeformat, width, height, hints); 
    int w = bm.getwidth(); 
    int h = bm.getheight(); 
    image = new bufferedimage(w, h, bufferedimage.type_int_rgb); 
    // 开始利用二维码数据创建bitmap图片,分别设为黑(0xffffffff)白(0xff000000)两色 
    for (int x = 0; x < w; x++) 
    { 
     for (int y = 0; y < h; y++) 
     { 
      image.setrgb(x, y, bm.get(x, y) ? qrcolor : bgwhite); 
     } 
    } 
   } 
   catch (writerexception e) 
   { 
    e.printstacktrace(); 
   } 
   return image; 
  } 
  /** * 设置二维码的格式参数 * * @return */ 
  public map<encodehinttype, object> getdecodehinttype() 
  { 
   // 用于设置qr二维码参数 
   map<encodehinttype, object> hints = new hashmap<encodehinttype, object>(); 
   // 设置qr二维码的纠错级别(h为*别)具体级别信息 
   hints.put(encodehinttype.error_correction, errorcorrectionlevel.h); 
   // 设置编码方式 
   hints.put(encodehinttype.character_set, "utf-8"); 
   hints.put(encodehinttype.margin, 0); 
   hints.put(encodehinttype.max_size, 350); 
   hints.put(encodehinttype.min_size, 100); 
   return hints; 
  } 
 } 
  class logoconfig 
  { 
   // logo默认边框颜色 
   public static final color default_bordercolor = color.white; 
   // logo默认边框宽度 
   public static final int default_border = 2; 
   // logo大小默认为照片的1/5 
   public static final int default_logopart = 5; 
   private final int border = default_border; 
   private final color bordercolor; 
   private final int logopart; 
   /** * creates a default config with on color {@link #black} and off color * {@link #white}, generating normal black-on-white barcodes. */ 
   public logoconfig() 
   { 
    this(default_bordercolor, default_logopart); 
   } 
   public logoconfig(color bordercolor, int logopart) 
   { 
    this.bordercolor = bordercolor; 
    this.logopart = logopart; 
   } 
   public color getbordercolor() 
   { 
    return bordercolor; 
   } 
   public int getborder() 
   { 
    return border; 
   } 
   public int getlogopart() 
   { 
    return logopart; 
   } 
  } 

以上所述是小编给大家介绍的java生成二维码可添加logo和文字功能,希望对大家有所帮助