当前位置: 移动技术网 > IT编程>开发语言>Java > 利用java生成二维码工具类示例代码

利用java生成二维码工具类示例代码

2019年07月19日  | 移动技术网IT编程  | 我要评论

二维码介绍

二维条形码最早发明于日本,它是用某种特定的几何图形按一定规律在平面(二维方向上)分布的黑白相间的图形记录数据符号信息的,在代码编制上巧妙地利用构成计算机内部逻辑基础的“0”、“1”比特流的概念,使用若干个与二进制相对应的几何形体来表示文字数值信息,通过图象输入设备或光电扫描设备自动识读以实现信息自动处理。

如下为java生成二维码工具类,可以选择生成文件,或者直接在页面生成,话不多说了,来一起看看详细的示例代码吧。

示例代码

import java.awt.image.bufferedimage; 
import java.io.file; 
import java.io.ioexception; 
import java.nio.file.filesystems; 
import java.util.hashmap; 
import java.util.map; 
 
import javax.imageio.imageio; 
import javax.servlet.http.httpservletresponse; 
 
import org.slf4j.logger; 
import org.slf4j.loggerfactory; 
 
import com.alibaba.fastjson.jsonobject; 
import com.google.zxing.barcodeformat; 
import com.google.zxing.binarizer; 
import com.google.zxing.binarybitmap; 
import com.google.zxing.decodehinttype; 
import com.google.zxing.encodehinttype; 
import com.google.zxing.luminancesource; 
import com.google.zxing.multiformatreader; 
import com.google.zxing.multiformatwriter; 
import com.google.zxing.notfoundexception; 
import com.google.zxing.result; 
import com.google.zxing.writerexception; 
import com.google.zxing.client.j2se.bufferedimageluminancesource; 
import com.google.zxing.client.j2se.matrixtoimagewriter; 
import com.google.zxing.common.bitmatrix; 
import com.google.zxing.common.hybridbinarizer; 
 
/** 
 * 
 * 二维码工具类 
 * @see [相关类/方法] 
 * @since [产品/模块版本] 
 */ 
public class qrcodeutil 
{ 
  
 private static final logger log = loggerfactory.getlogger(qrcodeutil.class); 
  
  
 /** 
  * 
  * 生成二维码文件测试 
  * @param filepath 文件路径 
  * @param filename 文件名 
  * @param number 编号 
  * @param phone 手机号 
  * @see [类、类#方法、类#成员] 
  */ 
 public static void generatencodetest(string filepath, string filename, string number, string phone) 
 { 
   
  int width = 200; // 图像宽度 
  int height = 200; // 图像高度 
  string format = "png";// 图像类型 
   
  jsonobject json = new jsonobject(); 
  json.put("number",number); 
  json.put("phone", phone); 
  string content = json.tojsonstring();// 内容 
   
  map<encodehinttype, object> hints = new hashmap<encodehinttype, object>(); 
  hints.put(encodehinttype.character_set, "utf-8"); 
   
  try 
  { 
   bitmatrix bitmatrix = new multiformatwriter().encode(content, barcodeformat.qr_code, width, height, hints);// 生成矩阵 
   string path = filesystems.getdefault().getpath(filepath, filename).tostring(); 
   file file = new file(path); 
   matrixtoimagewriter.writetofile(bitmatrix, format, file);// 输出图像 
   system.out.println("二维码输出成功"); 
   system.out.println("图片地址:" + filepath + filename); 
   system.out.println("---------------------------"); 
  } 
  catch (writerexception e) 
  { 
   e.printstacktrace(); 
   system.out.println("二维码输出异常"); 
  } 
  catch (ioexception e) 
  { 
   e.printstacktrace(); 
   system.out.println("二维码输出异常"); 
  } 
 } 
  
 /** 
  * 
  * 解析二维码内容测试 
  * @param filepath 二维码绝对路径 
  * @see [类、类#方法、类#成员] 
  */ 
 public static void parsedecodetest(string filepath) 
 { 
  bufferedimage image; 
  try 
  { 
   image = imageio.read(new file(filepath)); 
   luminancesource source = new bufferedimageluminancesource(image); 
   binarizer binarizer = new hybridbinarizer(source); 
   binarybitmap binarybitmap = new binarybitmap(binarizer); 
    
   map<decodehinttype, object> hints = new hashmap<decodehinttype, object>(); 
   hints.put(decodehinttype.character_set, "utf-8"); 
    
   result result = new multiformatreader().decode(binarybitmap, hints);// 对图像进行解码 
   jsonobject content = jsonobject.parseobject(result.gettext()); 
    
   stringbuffer sb = new stringbuffer(); 
   sb.append("编号:") 
    .append(content.getstring("number")) 
    .append("\r\n") 
    .append("手机号码:") 
    .append(content.getstring("phone")); 
   string returntext = sb.tostring(); 
   system.out.println(returntext); 
  } 
  catch (ioexception e) 
  { 
   e.printstacktrace(); 
  } 
  catch (notfoundexception e) 
  { 
   e.printstacktrace(); 
  } 
 } 
  
  
 /** 
  * 
  * 生成二维码输出流 
  *  在jsp页面中直接展示时使用 
  *  无须保存 即生成即展示 
  * @param response 
  * @param number 编号 
  * @param phone 手机号 
  * @see [类、类#方法、类#成员] 
  */ 
 public static void generatencode(httpservletresponse response, string number, string phone) 
 { 
  jsonobject json = new jsonobject(); 
  json.put("number",number); 
  json.put("phone", phone); 
  string content = json.tojsonstring();// 内容 
   
  int width = 200; // 图像宽度 
  int height = 200; // 图像高度 
  string format = "png";// 图像类型 
   
  map<encodehinttype, object> hints = new hashmap<encodehinttype, object>(); 
  hints.put(encodehinttype.character_set, "utf-8"); 
   
  try 
  { 
    
   bitmatrix bitmatrix = new multiformatwriter().encode(content, barcodeformat.qr_code, width, height, hints);// 生成矩阵 
   matrixtoimagewriter.writetostream(bitmatrix, format, response.getoutputstream());// 输出图像 
   log.info("二维码输出成功"); 
  } 
  catch (writerexception e) 
  { 
   e.printstacktrace(); 
   log.error("二维码输出异常"); 
  } 
  catch (ioexception e) 
  { 
   e.printstacktrace(); 
   log.error("二维码输出异常"); 
  } 
 } 
  
 public static void main(string[] args) 
 { 
  generatencodetest("d:\\","zxing.png","001","13019931996"); 
  parsedecodetest("d:\\zxing.png"); 
 } 
} 

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对移动技术网的支持。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网