二维码大家都不陌生,先说一下二维码的概念:
二维条码/二维码(2-dimensional bar code)是用某种特定的几何图形按一定规律在平面(二维方向上)分布的黑白相间的图形记录数据符号信息的图形。
附上我的女神:
生活中很多地方都用到二维码,它已经成为我们生活不可缺少的一部分。
我们来看下条码的发展史
为解决一维码信息容量不足的问题,二维码技术应运而生。上世纪80年代中期二维码技术在美国诞生,并迅速在欧美日等国的物流、军事、证照、电子、制造业信息化管理等领域实现大规模应用。常见的有PDF417、QR Code、Code 49、Code 16K、Code One等。
二维码可分为:线性堆叠式二维码,矩阵式二维码,邮政码
二维码的有点:高密度编码信息容量大,编码范围广,容错能力强,具有纠错功能,译码可靠性高,可引入加密措施,成本低,易制作,持久耐用
这里特别说一下强大的纠错能力:纠错功能原理:例如,需要编码的码字数据有100个,并且想对其中的一半,也就是50个码字进行纠错,则计算方法如下。纠错需要相当于码字2倍的符号,因此在这种情况下的数量为50个×2=100码字。因此,全部码字数量为200个,其中用作纠错的码字为50个,所以计算得出,相对于全部码字的纠错率就是25%。这一比率相当于QR码纠错级别中的“Q”级别。QR码具有“纠错功能”。即使编码变脏或破损,也可自动恢复数据。这一“纠错能力”具备4个级别,用户可根据使用环境选择相应的级别。调高级别,纠错能力也相应提高,但由于数据量会随之增加,编码尺寸也也会变大。
QRCode是由日本的Denso公司于1994年研制的一种矩阵式二维码,全称是Quick Response Code,也就是微信和支付宝都在使用的一种条码。
言归正传,我们来看一下如何用代码生成二维码:
代码生成二维码有三种方式:
① 使用google的zxing生成,需要引入zxing的jar包,我用的是zxing3.2.1.jar
package QRCode; import java.io.File; import java.nio.file.Path; import java.util.HashMap; 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; public class CreateQRCodeByZXing { //使用ZXing的方式生成二维码 public static void main(String[] args) { //定义图片的宽度和高度 int width =300; int height=300; String format="png"; //定义二维码的内容 String content="hello world"; //定义二维码参数 HashMap map=new HashMap(); map.put(EncodeHintType.CHARACTER_SET,"utf-8"); map.put(EncodeHintType.ERROR_CORRECTION,ErrorCorrectionLevel.M);//纠错等级L\M\Q\H map.put(EncodeHintType.MARGIN,2);//边框 try { // 参数顺序分别为:编码内容,编码类型,生成图片宽度,生成图片高度,设置参数 BitMatrix bitMatrix= new MultiFormatWriter().encode(content,BarcodeFormat.QR_CODE,width,height,map); Path file=new File("E:/zxing.png").toPath(); MatrixToImageWriter.writeToPath(bitMatrix,format, file); System.out.println("生成完成"); } catch (Exceptione) { e.printStackTrace(); } } }
② 使用QRCode生成二维码,同样需要引入jar包支持
package QRCode; import java.awt.Color; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.io.UnsupportedEncodingException; import javax.imageio.ImageIO; import com.swetake.util.Qrcode; public class CreateQRCodeByQRCode { public static void main(String[] args) throws Exception { //int width=300; //int height=300; int width=67 + 12 * ( 7 - 1); int height=67 + 12 * ( 7 - 1); Qrcode qrcode=new Qrcode(); qrcode.setQrcodeErrorCorrect('M');//纠错等级 qrcode.setQrcodeEncodeMode('B');//N代表数字,A代表a-Z,B代表其他字符 qrcode.setQrcodeVersion(7);//版本号1-40 //设置额二维码的内容 String qrData="hello world"; BufferedImage bufferedImage=new BufferedImage(width,height,BufferedImage.TYPE_INT_BGR); //画笔 Graphics2D gs = bufferedImage.createGraphics(); gs.setBackground(Color.WHITE); gs.setColor(Color.BLACK); gs.clearRect(0, 0, width, height);//清空一下画板的内容 //设置偏移量 int pixoff=2; byte[] b=qrData.getBytes("UTF-8"); if(b.length>0&&b.length<120){ boolean [][] boo=qrcode.calQrcode(b); for (int i=0;i<boo.length;i++){ for (int j=0;j<boo.length;j++){ if(boo[i][j]){ gs.fillRect(j*3+pixoff,i*3+pixoff,3, 3); } } } } gs.dispose(); bufferedImage.flush(); ImageIO.write(bufferedImage, "png", new File("E:/QRCodeByQRCode.png")); System.out.println("生成完成"); } }
③使用jQuery的qrCode插件
<%-- Created by IntelliJ IDEA. User: LiDa Date: 2018/5/14 Time: 18:18 To change this template use File | Settings | File Templates. --%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>使用JQueryQrCode生成二维码</title> <script type="text/javascript" src="./js/jquery-1.9.1.min.js"></script> <script type="text/javascript" src="./js/qrcode.js"></script> </head> <body> <div id="qrcode"></div> <script type="text/javascript"> //参数1表示图像大小,取值范围1-10;参数2表示质量,取值范围'L','M','Q','H' var content = "使用JQueryQrCode生成二维码" var qr = qrcode(8, 'M'); qr.addData(content); qr.make(); /* var dom=document.createElement('DIV'); dom.innerHTML = qr.createImgTag(); var element=document.getElementById("qrcode"); element.appendChild(dom); */ $("#qrcode").html(qr.createImgTag()); </script> </body> </html>
大家可到这里:http://mvnrepository.com/artifact/javax.mail下载jar包
本文分享自微信公众号 - 国产程序员(Monday_lida),作者:Monday
原文出处及转载信息见文内详细说明,如有侵权,请联系 yunjia_community@tencent.com 删除。
原始发表时间:2018-05-15
本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。
我来说两句