栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

二维码介绍

二维条形码最早发明于日本,它是用某种特定的几何图形按一定规律在平面(二维方向上)分布的黑白相间的图形记录数据符号信息的,在代码编制上巧妙地利用构成计算机内部逻辑基础的“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; 
 
 
public class QRCodeUtil 
{ 
  
 private static final Logger log = LoggerFactory.getLogger(QRCodeUtil.class); 
  
  
  
 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 hints = new HashMap(); 
  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("二维码输出异常"); 
  } 
 } 
  
  
 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 hints = new HashMap(); 
   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("rn") 
    .append("手机号码:") 
    .append(content.getString("phone")); 
   String returnText = sb.toString(); 
   System.out.println(returnText); 
  } 
  catch (IOException e) 
  { 
   e.printStackTrace(); 
  } 
  catch (NotFoundException e) 
  { 
   e.printStackTrace(); 
  } 
 } 
  
  
  
 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 hints = new HashMap(); 
  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"); 
 } 
} 

总结

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/144346.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号