首页 > 编程知识 正文

java生成excel,java二维码生成

时间:2023-05-06 04:20:41 阅读:45160 作者:2611

文章目录一、二维条形码/二维码(2-dimensional bar code )概念二、二维码发展历史三、二维码分类四、二维码优缺点五、QR Code六、实例开发一、zxing生成二维码二、zxing二维码解析三、QR Code六

一.二维码/二维码(2维条形码)概念

一种图形,通过某一特定几何图形分布在一定规则的再平面(二维方向)上的黑白相间的图形记录数据符号信息。

二、二维码发展历史

一维码:只能保存数字、字符。

二维码:可以存储汉字、数字、字母、照片等。

三、基于二维码分类线性堆叠式二维码:一维条形码构建,根据需要堆积两行或多行

矩阵式二维码:个矩形空间中由黑、白像素矩阵中的不同分布进行编码(在矩阵的对应元素的位置,点的出现表示二进制"1",点的不出现表示二进制"0" )

使用邮政码:长度不同的栏进行编码,主要用于邮件编码

四、二维码优缺点为二维码优点:高密码,信息容量大; 编码范围广; 容错性高,解密可靠性高; 可以导入加密对策的低成本、容易制作且耐用。

33558www.Sina.com/QR二维码技术将成为手机病毒、钓鱼网站传播的新渠道; 泄露消息。

五. QR Code 缺点:

pdf 147---- DM---- QR代码

三大国际标准:日本登索公司1994年开发的矩阵二维码符号代码,Quick Response Code

QR code:L级(7)-m ) 15 )-q ) 25 )-h ) 30 ) ) )。

纠错能力越高,搭载信息越少

第三方jar,如http://www.Sina.com/zxingQR code jar,jqueryqrcode.js

六、实例开发1、zxing生成二维码纠错能力:

http://www.Sina.com/https://github.com/zxing /

JSP生成二维码方法:开源java

下载后必须是源代码,自己成为jar包。 包括酷睿的com和javase的com。

Zxing:

ependencygroupidcom.Google.zxing/groupidartifactidcore/artifactidversion3.3.0/version/dependencydepencygroooon artifactidversion3.0.0/version /从属关系3358 www.Sina.com/http://www.Sina.com /

//二维码publicclasscreateqrcode { publicstaticvoidmain [ ] args } { intwidth=300; //定义图像宽度int height=300; //定义图像高度的String format='png '; //图像格式string content=' https://blog.csdn.net/weixin _ 45537947 ';//定义二维码内容//定义二维码的参数HashMap hashMap=new HashMap (; hashmap.put (encodehinttype.character _ set,' utf-8 '; //编码hashmap.put (encodehinttype.error _ correction,ErrorCorrectionLevel.M ); //设置容错级别,级别越高容量越小hashmap.put (encodehinttype.margin,2 ); //设置边距//生成二维码try/

/生成矩阵 // 内容 格式 宽 高 BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hashMap); Path file = new File("F:/img.png").toPath(); //设置路径 MatrixToImageWriter.writeToPath(bitMatrix, format, file); //输出图像 } catch (Exception e) { e.printStackTrace(); } }}

前往该路径,查看生成的二维码:

2、zxing进行二维码解析

代码演示:

public class ReadQRCode {public static void main(String[] args) { try { /* * MultiFormatReader 多格式读取 * */ MultiFormatReader formatReader = new MultiFormatReader(); File file = new File("F:/img.png"); //读取图片buffer中 BufferedImage bufferedImage = ImageIO.read(file); /* * BinaryBitmap二进制位图 * HybridBinarizer混合二值化器 * BufferedImageLuminanceSource 图像缓存区 亮度 资源 * */ BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(bufferedImage))); //定义二维码参数 HashMap hashMap = new HashMap(); hashMap.put(EncodeHintType.CHARACTER_SET, "utf-8");//编码方式 //hashMap.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);//优化精度 //hashMap.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);//复杂模式,开启PURE_BARCODE模式 Result result = formatReader.decode(binaryBitmap,hashMap); System.out.println("解析结果:"+result.toString()); System.out.println("二维码格式类型:"+result.getBarcodeFormat());//BarcodeFormat 条形码格式 System.out.println("二维码文本内容:"+result.getText()); } catch (Exception e) { e.printStackTrace(); } }}

结果:

解析过程中出现了这个报错:com.google.zxing.NotFoundException

所以我进行了debug发现扫描二维码时,二维码所有bit都是0,然后分析了一下,发现我在生成二维码的时候白色像素填充使用的是透明色,这样在显示的时候因为背景是白色,所以看上去和用手机扫都没有问题,但是自己代码识别的时候就会把透明色识别为黑色,这样就导致整个二维码图片全是黑色像素,所以zxing抛出com.google.zxing.NotFoundException异常。
所以只需要把上面代码演示的代码中把优化精度、复杂模式的注释取消就可以解决。

倘若你把内容设置成中文后出现一堆问号,则需要:

二维码内容写成中文之后,调用读取的类,读出来的是一堆问号的解决方式:
在读取类中该为BitMatrix bitMatrix=new MultiFormatWriter().encode(new String(content.getBytes(“UTF-8”),“ISO-8859-1”), BarcodeFormat.QR_CODE, width, height,hints);
在读取类和创建类中,将编码都设为"ISO-8859-1"就能读取中文了
hints.put(EncodeHintType.CHARACTER_SET, “ISO-8859-1”);

3、使用QR Code方式生成和解析二维码

生成: http://www.swetake.com/qrcode/index-e.html

读取: https://osdn.jp/projects/qrcode/

生成二维码,代码实现:(请先导入对应的包)
CreateQRCode.java:

public class CreateQRCode { public static void main(String[] args) throws IOException { QRCode x = new QRCode(); x.setQrCodeErrorCorrect('M'); //纠错等级 x.setQrcodeEncodeMode('B'); //N代表数字,A代表a-Z,B代表其它字符 x.setQrcodeVersion(7); //版本 String qrData = "https://blog.csdn.net/weixin_45537947"; int width = 67 + 12 * (7-1); int height = 67 + 12 * (7-1); BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D gs = bufferedImage.createGraphics(); gs.setBackground(Color.WHITE); gs.setColor(Color.BLACK); gs.clearRect(0, 0, width,height); int pixoff = 2;//偏移量 byte[] d = qrData.getBytes("gb2312"); if (d.length > 0 && d.length < 120) { boolean[][] booleans = x.calQrcode(d); for (int i = 0;i < booleans.length; i++) { for (int j = 0;j < booleans.length; j++) { if (booleans[j][i]) { gs.fillRect(j * 3 + pixoff, i * 3 + pixoff, 3, 3); } } } } gs.dispose(); bufferedImage.flush(); ImageIO.write(bufferedImage, "png",new File("F://qrcode.png")); }} 4、jquery-qrcode生成二维码

jQuery-qrcode:

网址: https://github.com/jeromeetienne/jquery-qrcode

代码演示:
js的代码下载地址:
https://img.mukewang.com/down/5799a5440001040300000000.rar
qrcode.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>生成二维码</title> <script type="text/javascript" src ="<%=request.getContextPath() %>/js/jquery.min.js"></script> <script type="text/javascript" src ="<%=request.getContextPath() %>/js/jquery.qrcode.min.js"></script></head><body>生成的二维码如下:<br><div id = "qrcode"></div><script type="text/javascript"> //二选一 jQuery('#qrcode').qrcode("https://blog.csdn.net/weixin_45537947"); // jQuery('#qrcode').qrcode({width: 250, height: 250,text: "https://blog.csdn.net/weixin_45537947"});</script></body></html>

然后启动Tomcat, 访问:


关于二维码的简单学习就到这里ヾ(✿゚▽゚)ノ

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。