当前位置: 移动技术网 > IT编程>开发语言>Java > java中使用zxing批量生成二维码立牌

java中使用zxing批量生成二维码立牌

2019年07月22日  | 移动技术网IT编程  | 我要评论
使用zxing批量在做好的立牌背景图的指定位置上,把指定的文本内容(链接地址、文本等)生成二维码并放在该位置,最后加上立牌编号。 步骤: 1).做好背景图,如下

使用zxing批量在做好的立牌背景图的指定位置上,把指定的文本内容(链接地址、文本等)生成二维码并放在该位置,最后加上立牌编号。

步骤:

1).做好背景图,如下图:

2).生成二维码bufferedimage对象。代码如下:

/** 
  * 
  * @title: tobufferedimage 
  * @description: 把文本转化成二维码图片对象 
  * @param text 
  *   二维码内容 
  * @param width 
  *   二维码高度 
  * @param height 
  *   二位宽度 
  * @param 
  * @param exception 
  *   设定文件 
  * @return bufferedimage 返回类型 
  * @throws 
  */ 
 public static bufferedimage tobufferedimage(string text, int width, 
   int height) throws exception { 
  int black = 0xff000000; 
  int white = 0xffffffff; 
  hashtable<encodehinttype, object> hints = new hashtable<encodehinttype, object>(); 
  hints.put(encodehinttype.character_set, "utf-8"); // 内容所使用字符集编码 
  hints.put(encodehinttype.margin, 1); 
  bitmatrix matrix = new multiformatwriter().encode(text, 
    barcodeformat.qr_code, width, height, hints); 
  bufferedimage image = new bufferedimage(width, height, 
    bufferedimage.type_int_rgb); 
  for (int x = 0; x < width; x++) { 
   for (int y = 0; y < height; y++) { 
    image.setrgb(x, y, matrix.get(x, y) ? black : white); 
   } 
  } 
  return image; 
 } 

3).在立牌背景图的指定位置上生成二维码,代码如下:

/** 
  * 
  * @title: markimagebycode 
  * @description: 向图片指定位置增加二维码 
  * @param img 
  *   二维码image对象 
  * @param srcimgpath 
  *   背景图 
  * @param targerpath 
  *   目标图 
  * @param positionwidth 
  *   位置横坐标 
  * @param positionheight 
  *   位置纵坐标 
  * @return void 返回类型 
  * @throws 
  */ 
 public static void markimagebycode(image img, string srcimgpath, 
   string targerpath, int positionwidth, int positionheight) { 
  outputstream os = null; 
  try { 
 
   image srcimg = imageio.read(new file(srcimgpath)); 
 
   bufferedimage buffimg = new bufferedimage(srcimg.getwidth(null), 
     srcimg.getheight(null), bufferedimage.type_int_rgb); 
 
   // 1、得到画笔对象 
   graphics2d g = buffimg.creategraphics(); 
 
   // 2、设置对线段的锯齿状边缘处理 
   g.setrenderinghint(renderinghints.key_interpolation, 
     renderinghints.value_interpolation_bilinear); 
 
   g.drawimage( 
     srcimg.getscaledinstance(srcimg.getwidth(null), 
       srcimg.getheight(null), image.scale_smooth), 0, 0, 
     null); 
 
   g.setcomposite(alphacomposite.getinstance(alphacomposite.src_atop, 
     alpha)); 
 
   // 3、二维码位置 
   g.drawimage(img, positionwidth, positionheight, null); 
   g.setcomposite(alphacomposite.getinstance(alphacomposite.src_over)); 
   // 4、释放资源 
   g.dispose(); 
 
   // 5、生成图片(建议生成png的,jpg会失真) 
   os = new fileoutputstream(targerpath); 
   imageio.write(buffimg, "png", os); 
 
   system.out.println("二维码图片生成成功"); 
 
  } catch (exception e) { 
   e.printstacktrace(); 
  } finally { 
   try { 
    if (null != os) 
     os.close(); 
   } catch (exception e) { 
    e.printstacktrace(); 
   } 
  } 
 } 

4).在立牌上加上立牌编号

/** 
  * 
  * @title: presstext 
  * @description:向图片指定位置加上文字 
  * @param presstext 
  *   文字内容 
  * @param srcimagefile 
  *   原图片 
  * @param destimagefile 
  *   目标图片 
  * @param x 
  *   横坐标 
  * @param y 
  *   纵坐标 
  * @param alpha 
  *   透明度 
  * @return void 返回类型 
  * @throws 
  */ 
 public final static void presstext(string presstext, string srcimagefile, 
   string destimagefile, int x, int y, float alpha) { 
  try { 
   file img = new file(srcimagefile); 
   image src = imageio.read(img); 
   int width = src.getwidth(null); 
   int height = src.getheight(null); 
   bufferedimage image = new bufferedimage(width, height, 
     bufferedimage.type_int_rgb); 
   graphics2d g = image.creategraphics(); 
   // 开文字抗锯齿 去文字毛刺 
   g.setrenderinghint(renderinghints.key_text_antialiasing, 
     renderinghints.value_text_antialias_on); 
   g.drawimage(src, 0, 0, width, height, null); 
   // 设置颜色 
   g.setcolor(new color(89, 87, 87)); 
   // 设置 font 
   g.setfont(new font("方正兰亭中黑_gbk", font.bold, 14)); 
   g.setcomposite(alphacomposite.getinstance(alphacomposite.src_atop, 
     alpha)); 
   // 第一参数->设置的内容,后面两个参数->文字在图片上的坐标位置(x,y) . 
   g.drawstring(presstext, x, y); 
   g.dispose(); 
   imageio.write((bufferedimage) image, "png", new file(destimagefile));// 输出到文件流 
  } catch (exception e) { 
   e.printstacktrace(); 
  } 
 } 

示例:

代码:

测试代码

public class codetest { 
 public static void main(string[] args) throws exception { 
  string text = "http://www.xxx.com/"; // 二维码内容 
 
  // 生成二维码 
  //生成图片二维码存放目录 
  string targetpath = "f:/qrcode/targetimg/" + utils.tostr(); 
  //创建目录 
  utils.makedirs(targetpath); 
   
  int begin = 100;//code 开始数字 
  int end = 101;//code结束数字 
  for (int i = begin; i <= end; i++) { 
   //生成含日期的16位数字如20161214000001 
   string code = utils.tostr() + utils.formatenumber(i); 
   //获取二维码对象 
   bufferedimage image = utils.tobufferedimage(text 
     + "?paycode=" + code,240,240); 
   //生成含背景图+二维码的立牌的图 
   utils.markimagebycode(image, "f:/qrcode/srcimg/src.png", 
     targetpath + "/" + code + ".png", 340, 160); 
   //立牌的图加上code编号 
   utils.presstext(code, targetpath + "/" + code + ".png", targetpath 
     + "/" + code + ".png", 390, 417, 0.5f); 
  } 
  // 生成二维码 
 } 
} 

效果:

批量生成的图片效果图如下

批量图:

utils代码:

package cn.utils.code; 
 
import java.awt.alphacomposite; 
import java.awt.color; 
import java.awt.font; 
import java.awt.graphics2d; 
import java.awt.image; 
import java.awt.renderinghints; 
import java.awt.image.bufferedimage; 
import java.io.file; 
import java.io.fileoutputstream; 
import java.io.outputstream; 
import java.text.decimalformat; 
import java.text.simpledateformat; 
import java.util.date; 
import java.util.hashtable; 
 
import javax.imageio.imageio; 
 
import com.google.zxing.barcodeformat; 
import com.google.zxing.encodehinttype; 
import com.google.zxing.multiformatwriter; 
import com.google.zxing.common.bitmatrix; 
 
/** 工具类. */ 
public abstract class utils { 
 
 /** 日期格式:yyyy-mm-dd hh:mm:ss */ 
 public static string df_datetime = "yyyymmdd"; 
 private static float alpha = 1f; 
 
 /** 
  * 
  * @title: tobufferedimage 
  * @description: 把文本转化成二维码图片对象 
  * @param text 
  *   二维码内容 
  * @param width 
  *   二维码高度 
  * @param height 
  *   二位宽度 
  * @param 
  * @param exception 
  *   设定文件 
  * @return bufferedimage 返回类型 
  * @throws 
  */ 
 public static bufferedimage tobufferedimage(string text, int width, 
   int height) throws exception { 
  int black = 0xff000000; 
  int white = 0xffffffff; 
  hashtable<encodehinttype, object> hints = new hashtable<encodehinttype, object>(); 
  hints.put(encodehinttype.character_set, "utf-8"); // 内容所使用字符集编码 
  hints.put(encodehinttype.margin, 1); 
  bitmatrix matrix = new multiformatwriter().encode(text, 
    barcodeformat.qr_code, width, height, hints); 
  bufferedimage image = new bufferedimage(width, height, 
    bufferedimage.type_int_rgb); 
  for (int x = 0; x < width; x++) { 
   for (int y = 0; y < height; y++) { 
    image.setrgb(x, y, matrix.get(x, y) ? black : white); 
   } 
  } 
  return image; 
 } 
 
 /** 
  * 
  * @title: markimagebycode 
  * @description: 向图片指定位置增加二维码 
  * @param img 
  *   二维码image对象 
  * @param srcimgpath 
  *   背景图 
  * @param targerpath 
  *   目标图 
  * @param positionwidth 
  *   位置横坐标 
  * @param positionheight 
  *   位置纵坐标 
  * @return void 返回类型 
  * @throws 
  */ 
 public static void markimagebycode(image img, string srcimgpath, 
   string targerpath, int positionwidth, int positionheight) { 
  outputstream os = null; 
  try { 
 
   image srcimg = imageio.read(new file(srcimgpath)); 
 
   bufferedimage buffimg = new bufferedimage(srcimg.getwidth(null), 
     srcimg.getheight(null), bufferedimage.type_int_rgb); 
 
   // 1、得到画笔对象 
   graphics2d g = buffimg.creategraphics(); 
 
   // 2、设置对线段的锯齿状边缘处理 
   g.setrenderinghint(renderinghints.key_interpolation, 
     renderinghints.value_interpolation_bilinear); 
 
   g.drawimage( 
     srcimg.getscaledinstance(srcimg.getwidth(null), 
       srcimg.getheight(null), image.scale_smooth), 0, 0, 
     null); 
 
   g.setcomposite(alphacomposite.getinstance(alphacomposite.src_atop, 
     alpha)); 
 
   // 3、二维码位置 
   g.drawimage(img, positionwidth, positionheight, null); 
   g.setcomposite(alphacomposite.getinstance(alphacomposite.src_over)); 
   // 4、释放资源 
   g.dispose(); 
 
   // 5、生成图片(建议生成png的,jpg会失真) 
   os = new fileoutputstream(targerpath); 
   imageio.write(buffimg, "png", os); 
 
   system.out.println("二维码图片生成成功"); 
 
  } catch (exception e) { 
   e.printstacktrace(); 
  } finally { 
   try { 
    if (null != os) 
     os.close(); 
   } catch (exception e) { 
    e.printstacktrace(); 
   } 
  } 
 } 
 
 /** 
  * 
  * @title: presstext 
  * @description:向图片指定位置加上文字 
  * @param presstext 
  *   文字内容 
  * @param srcimagefile 
  *   原图片 
  * @param destimagefile 
  *   目标图片 
  * @param x 
  *   横坐标 
  * @param y 
  *   纵坐标 
  * @param alpha 
  *   透明度 
  * @return void 返回类型 
  * @throws 
  */ 
 public final static void presstext(string presstext, string srcimagefile, 
   string destimagefile, int x, int y, float alpha) { 
  try { 
   file img = new file(srcimagefile); 
   image src = imageio.read(img); 
   int width = src.getwidth(null); 
   int height = src.getheight(null); 
   bufferedimage image = new bufferedimage(width, height, 
     bufferedimage.type_int_rgb); 
   graphics2d g = image.creategraphics(); 
   // 开文字抗锯齿 去文字毛刺 
   g.setrenderinghint(renderinghints.key_text_antialiasing, 
     renderinghints.value_text_antialias_on); 
   g.drawimage(src, 0, 0, width, height, null); 
   // 设置颜色 
   g.setcolor(new color(89, 87, 87)); 
   // 设置 font 
   g.setfont(new font("方正兰亭中黑_gbk", font.bold, 14)); 
   g.setcomposite(alphacomposite.getinstance(alphacomposite.src_atop, 
     alpha)); 
   // 第一参数->设置的内容,后面两个参数->文字在图片上的坐标位置(x,y) . 
   g.drawstring(presstext, x, y); 
   g.dispose(); 
   imageio.write((bufferedimage) image, "png", new file(destimagefile));// 输出到文件流 
  } catch (exception e) { 
   e.printstacktrace(); 
  } 
 } 
 
 // 日期转字符串 
 
 /** 将日期格式化为string,默认格式为yyyy-mm-dd hh:mm:ss,默认日期为当前日期. */ 
 public static string tostr() { 
  return tostr(df_datetime); 
 } 
 
 /** 将日期格式化为string,格式由参数format指定,默认日期为当前日期,format值可使用本类常量或自定义. */ 
 public static string tostr(string format) { 
  return tostr(format, new date()); 
 } 
 
 /** 将日期格式化为string,默认格式为yyyy-mm-dd hh:mm:ss,日期由参数date指定. */ 
 public static string tostr(date date) { 
  return tostr(df_datetime, date); 
 } 
 
 /** 将日期格式化为string,格式由参数format指定,日期由参数date指定,format值可使用本类常量或自定义. */ 
 public static string tostr(string format, date date) { 
  return new simpledateformat(format).format(date); 
 } 
 
 public static string formatenumber(int num) { 
  decimalformat df = new decimalformat("000000"); 
  string str2 = df.format(num); 
  return str2; 
 } 
 
 public static boolean makedirs(string filepath) { 
 
  file folder = new file(filepath); 
  return (folder.exists() && folder.isdirectory()) ? true : folder 
    .mkdirs(); 
 } 
 
} 

使用的技术:

1.使用的zxing生成二维码工具。

1)下载地址:

2).maven配置

<dependency> 
   <groupid>com.google.zxing</groupid> 
   <artifactid>core</artifactid> 
   <version>2.2</version> 
  </dependency> 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网