二维码生成读取简单示例
一、摘要
二维码又称二维条码,常见的二维码为QR Code,QR全称Quick Response,是一个近几年来移动设备上超流行的一种编码方式,它比传统的Bar Code条形码能存更多的信息,也能表示更多的数据类型。
二、背景
自从微信面世以来,二维码更是得到了广泛的传播。生成二维码有很多jar包可以实现,例如Zxing,QRcode,前者是谷歌的,后者日本的,现在用zxing以及jquery-qrcode来生成二维码,读取二维码信息。
三、推广建议
学习可快速完成生成二维码。
四、正文
下载源码链接:
https://github.com/jeromeetienne/jquery-qrcode
https://github.com/zxing
Zxing生成二维码
Pom.xml
<!-- https://mvnrepository.com/artifact/com.google.zxing/core --> <dependency> <groupId>com.google.zxing</groupId> <artifactId>core</artifactId> <version>3.3.3</version> </dependency> <!-- https://mvnrepository.com/artifact/com.google.zxing/javase --> <dependency> <groupId>com.google.zxing</groupId> <artifactId>javase</artifactId> <version>3.3.3</version> </dependency> package cn.ltian.zxing; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.client.j2se.MatrixToImageWriter; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; import java.io.File; import java.nio.file.Path; import java.util.HashMap; public class QRCode { /** * 创建二维码,写入文件 * @param content 文本信息 * @param format 图片格式 * @param width 宽度 * @param heigth 高度 * @param filePath 文件地址 */ public static void CreateQRCode(String content,String format,int width,int heigth,String filePath){ // int width = 300; // int heigth = 300; // String format = "png"; // String content = "www.baidu.com"; //定义二维码的参数 HashMap hints = new HashMap(); hints.put(EncodeHintType.CHARACTER_SET,"UTF-8"); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M); hints.put(EncodeHintType.MARGIN,2); try{ BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE,width,heigth,hints); Path file; file = new File(filePath).toPath(); // writeToFile(bitMatrix,format,file); MatrixToImageWriter.writeToPath(bitMatrix,format,file); }catch(Exception e){ e.printStackTrace(); } } } Zxing读取二维码 package cn.ltian.zxing; import com.google.zxing.BinaryBitmap; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatReader; import com.google.zxing.Result; import com.google.zxing.client.j2se.BufferedImageLuminanceSource; import com.google.zxing.common.HybridBinarizer; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.File; import java.util.HashMap; public class ReadQRCode { /** * 读取二维码信息 * @param filePath 文件地址 * @return */ public static Result readQRcode(String filePath) { Result result = null; try { MultiFormatReader multiFormatReader = new MultiFormatReader(); File file = new File(filePath); BufferedImage image = ImageIO.read(file); BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image))); HashMap hints = new HashMap(); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); result = multiFormatReader.decode(binaryBitmap, hints); System.out.println("解析结果:" + result.toString()); System.out.println("二维码格式类型:" + result.getBarcodeFormat()); System.out.println("二维码文本类型:" + result.getText()); } catch (Exception e) { e.printStackTrace(); } return result; } } jquery-qrcode生成二维码 <!DOCTYPE html> <html lang="en"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <head> <title>basic example</title> </head> <body> <script src="../../js/jquery-1.11.1.min.js"></script> <!--<script type="text/javascript" src="../jquery.qrcode.min.js"></script> --><script type="text/javascript" src="../../js/jquery.qrcode.js"></script> <script type="text/javascript" src="../../js/qrcode.js"></script> <p>Render in table</p> <div id="qrcodeTable"></div> <p>Render in canvas</p> <div id="qrcodeCanvas"></div> <a id="download" download="qrcode.jpg"></a> <a id="saveQrCode" style="cursor: pointer;">下载二维码</a> </a> <script> //jQuery('#qrcode').qrcode("this plugin is great"); jQuery('#qrcodeTable').qrcode({ render : "table", text : "http://jetienne.com" }); jQuery('#qrcodeCanvas').qrcode({ render : "canvas", text : "http://www.baidu.com", width : "200", //二维码的宽度 height : "200", //二维码的高度 background : "#ffffff", //二维码的后景色 foreground : "#000000", //二维码的前景色 src: 'logo.png' //二维码中间的图片 }); $('#saveQrCode').click(function(){ var canvas = $('#qrcodeCanvas').find("canvas").get(0); console.log(canvas); try {//解决IE转base64时缓存不足,canvas转blob下载 var blob = canvas.msToBlob(); navigator.msSaveBlob(blob, 'qrcode.jpg'); } catch (e) {//如果为其他浏览器,使用base64转码下载 var url = canvas.toDataURL('image/jpeg'); $("#download").attr('href', url).get(0).click(); } return false; }); </script> </body> </html>