总所周知,二维码的内容越多,二维码越复杂,越是复杂的二维码扫码效率越慢,有时候导致直接扫码的时候直接扫不出来。

解决方法:
在生成二维码的时候,对二维码文本进行压缩,压缩内容生成二维码,在扫码的时候,把扫码获取的压缩内容传给后台,然后进行二维码解压即可获取解压内容。

具体实现方法如下:
1. 引入pom文件

     <!--二维码解析--><dependency><groupId>com.google.zxing</groupId><artifactId>core</artifactId><version>3.3.3</version></dependency><dependency><groupId>com.google.zxing</groupId><artifactId>javase</artifactId><version>3.3.3</version></dependency>

2. 写测试类

 public static void main(String[] args) throws IOException, NotFoundException {String content="假设这个文本的内容超级长";//压缩String zipContent=zipBase64(content);System.out.println("压缩内容"+zipContent);//生成二维码QRCodeUtil.getQRCode(zipContent,"jpg","G:/img/testQr2.jpg","UTF-8");//获取二维码路径String format="G:/img/testQr2.jpg";//获取压缩内容String analysis = QRCodeUtil.analysis(format, "UTF-8");//解压已压缩内容String unzipBase64 = unzipBase64(analysis);System.out.println(unzipBase64);}

3. 结果:
生成的图片如下:

控制台输出:

4.工具类

压缩解压文本:

/*** 压缩文本* @param text* @return*/public static String zipBase64(String text) {//创建一个新的字节数组输出流。缓冲区容量最初为 32 字节try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {//使用默认压缩器和缓冲区大小创建新的输出流try (DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(out)) {deflaterOutputStream.write(text.getBytes(Charset.forName("UTF-8")));}return Base64.getEncoder().encodeToString(out.toByteArray());} catch (IOException e) {}return "";}/*** 解压文本* @param text* @return*/public static String unzipBase64(String text) {try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {//使用默认解压缩器和缓冲区创建新的输出流try (OutputStream outputStream = new InflaterOutputStream(os)) {outputStream.write(Base64.getDecoder().decode(text.getBytes()));}return new String(os.toByteArray(), Charset.forName("UTF-8"));} catch (IOException e) {}return "";}

生成二维码,解析二维码工具:

package com.excel.util;import cn.hutool.extra.qrcode.BufferedImageLuminanceSource;
import com.google.zxing.*;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;/*** 二维码的解析,生成* @author lenovo*/
public class QRCodeUtil {/*** 生成二维码* @param contents 内容* @param format 二维码图片后缀* @param filepath //生成二维码路径(当包含二维码名称和后缀,忽略format)* @throws IOException* @throws NotFoundException*/public static void getQRCode(String contents,String format,String filepath) throws IOException, NotFoundException {getQRCode(contents, format, filepath,"utf-8");}/*** 生成二维码* @param contents 内容* @param format 二维码图片后缀* @param filepath //生成二维码路径(当包含二维码名称和后缀,忽略format)* @param character 二维码内容编码方式* @throws IOException* @throws NotFoundException*/public static void getQRCode(String contents,String format,String filepath,String character) throws IOException, NotFoundException {getQRCode(300,300,contents,format,filepath,character);}/*** 生成二维码* @param width 宽* @param height 高* @param contents 内容* @param format 二维码图片后缀* @param filepath //生成二维码路径(当包含二维码名称和后缀,忽略format)* @param character 二维码内容编码方式* @throws IOException* @throws NotFoundException*/public static void getQRCode(int width,int height,String contents,String format,String filepath,String character) throws IOException, NotFoundException {HashMap map = new HashMap();map.put(EncodeHintType.CHARACTER_SET,character);map.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);map.put(EncodeHintType.MARGIN, 0);try {BitMatrix bm = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height,map);Path file = new File(filepath).toPath();MatrixToImageWriter.writeToPath(bm, format, file);} catch (WriterException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}/*** 解析上传二维码内容(直解析utf-8和GBK编码的二维码)* @param QRImg 二维码图片内容* @throws IOException* @throws NotFoundException*/public static String analysis(File QRImg) throws IOException, NotFoundException {BufferedImage image = ImageIO.read(QRImg);BinaryBitmap bb = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));HashMap map = new HashMap();map.put(DecodeHintType.CHARACTER_SET, "utf-8");Result result = new MultiFormatReader().decode(bb, map);String QRContents=result.getText();if (result.getText().contains("�")){//乱码了,更换编码方式System.out.println("乱码了,更换编码格式(GBK)");map.put(DecodeHintType.CHARACTER_SET, "GBK");result = new MultiFormatReader().decode(bb, map);QRContents=result.getText();}System.out.println("二维码文本内容:"+QRContents);return QRContents;}/*** 解析本地二维码内容(直解析utf-8和GBK编码的二维码)* @param filePath 二维码图片地址* @throws IOException* @throws NotFoundException*/public static String analysis(String filePath) throws IOException, NotFoundException {BufferedImage image = ImageIO.read(new File(filePath));BinaryBitmap bb = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));HashMap map = new HashMap();map.put(DecodeHintType.CHARACTER_SET, "utf-8");Result result = new MultiFormatReader().decode(bb, map);String QRContents=result.getText();if (result.getText().contains("�")){//乱码了,更换编码方式System.out.println("乱码了,更换编码格式(GBK)");map.put(DecodeHintType.CHARACTER_SET, "GBK");result = new MultiFormatReader().decode(bb, map);QRContents=result.getText();}System.out.println("二维码文本内容:"+QRContents);return QRContents;}/*** 解析本地二维码内容,自定义编码格式* @param filePath 二维码图片地址* @param character 二维码内容编码格式* @throws IOException* @throws NotFoundException*/public static String analysis(String filePath,String character) throws IOException, NotFoundException {BufferedImage image = ImageIO.read(new File(filePath));BinaryBitmap bb = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));HashMap map = new HashMap();map.put(DecodeHintType.CHARACTER_SET,character);map.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);//复杂模式map.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);Result result = new MultiFormatReader().decode(bb, map);String QRContents=result.getText();return QRContents;}/*** 解析二维码,此方法解析一个路径的二维码图片(带logo)* path:二维码图片路径*/public static String deEncodeByPath(String path) {String content = null;BufferedImage image;try {image = ImageIO.read(new File(path));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, "GBK");hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);Result result = new MultiFormatReader().decode(binaryBitmap, hints);//解码System.out.println("图片中内容:  ");System.out.println("content: " + result.getText());content = result.getText();} catch (IOException e) {e.printStackTrace();} catch (NotFoundException e) {//这里判断如果识别不了带LOGO的图片,重新添加上一个属性try {image = ImageIO.read(new File(path));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, "GBK");//设置优化精度hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);//设置复杂模式开启(我使用这种方式就可以识别微信的二维码了)hints.put(DecodeHintType.PURE_BARCODE,Boolean.TYPE);Result result = new MultiFormatReader().decode(binaryBitmap, hints);//解码System.out.println("图片中内容:  ");System.out.println("content: " + result.getText());content = result.getText();} catch (IOException e1) {e1.printStackTrace();} catch (NotFoundException e2) {e2.printStackTrace();}}return content;}
}

二维码内容过多,生成的二维码太复杂,导致扫码的时候,过慢或者扫不出来解决方案。附二维码生成、解析工具类相关推荐

  1. Java使用pb【protobuf】压缩解决二维码内容过多导致二维码太密的问题

    文章目录 一.前言 二.pb简介 三.pb简单使用方法 四.例子 五.pb工具和模型免费下载地址 一.前言 在我们开发的过程中,可能会遇到这个问题,要展示一个二维码,二维码里有很多数据,但是数据太多了 ...

  2. C语言反转二叉树的递归和迭代解决方案(附完整源码)

    C语言反转二叉树的递归和迭代解决方案 C语言反转二叉树的递归和迭代解决方案完整源码(定义,实现,main函数测试) C语言反转二叉树的递归和迭代解决方案完整源码(定义,实现,main函数测试) #in ...

  3. Java生成和解析二维码工具类(简单经典)

    Java生成和解析二维码工具类 开箱即用,简单不废话. pom.xml引入依赖 <!-- https://mvnrepository.com/artifact/com.google.zxing/ ...

  4. 【Android数据存储】ContentProvider详细介绍(附实例源码)

    1.ContentProvider是什么? ContentProvider--内容提供者.它是一个类,这个类主要是对Android系统中进行共享的数据进行包装,并提供了一组统一的访问接口供其他程序调用 ...

  5. 案例:使用vue开发微信机器人聊天(附完整源码)

    先看效果: 实现过程: <!DOCTYPE html> <html><head><meta charset="UTF-8">< ...

  6. 【踩坑记录】mybatis-plus的insert方法,默认会生成一个uuid作为主键,导致类型不一致,存入数据库报错

    [踩坑记录]mybatis-plus的insert方法,默认会生成一个uuid作为主键,导致类型不一致,存入数据库报错 报错记录 解决方案 推荐方案 使用uuid作为主键,修改id的类型为bigint ...

  7. 如何编辑二维码内容并批量生成

    随着社会的发展,更加智能的二维码使用也变得越来越普遍,那么我们所看到的五颜六色或者各种样式的二维码都是如何添加内容和制作的呢?下面我们就中琅二维码软件来看一下如何编辑二维码内容并且实现批量生成的方法: ...

  8. Java利用Zxing生成二维码及解析二维码内容

    前言 Java 操作二维码的开源项目很多,如 SwetakeQRCode.BarCode4j.Zxing 等等 本篇文章是介绍利用Zxing来生成二维码图片在web网页上展示,同时解析二维码图片. Z ...

  9. [开源]C#二维码生成解析工具,可添加自定义Logo

    原文:[开源]C#二维码生成解析工具,可添加自定义Logo 二维码又称 QR Code,QR 全称 Quick Response,是一个近几年来移动设备上超流行的一种编码方式,它比传统的 Bar Co ...

  10. java实现二维码的生成和解析包含工具类

    1.下载jar包( jdk1.7) 链接:https://pan.baidu.com/s/1LVq_zGsp_Po8V456h1TVmA 提取码:vqyd 2.工具类Utils.java packag ...

最新文章

  1. WCF学习之旅—WCF服务的WAS寄宿(十二)
  2. 伍哥原创之安装nginx,mysql,php-fpm,redis
  3. 谈谈我的编程之路---WAMP(二)
  4. 网易云信自研大规模传输网核心系统架构剖析
  5. ‘StreamTableDescriptor‘ object has no attribute ‘register_table_sink‘
  6. airflow使用_使用AirFlow,SAS Viya和Docker像Pro一样自动化ML模型
  7. 如何有效的使用 for循环和Iterator遍历
  8. 最暖数据: 除夕有6.88亿人用微信红包传递狗年祝福
  9. 集群系统服务器,Web集群服务器及管理系统
  10. 判断输入的年月日是否合法
  11. c++ 成员初始化列表
  12. 常用正则表达式匹配(中文字符)
  13. win10系统使用快捷键无法切换窗口怎么办
  14. Java的面向对象 -- 继承
  15. BootStrap左侧菜单栏
  16. proftpd的一些简单配置
  17. 移动端web总结(一)——JDM项目总结
  18. python 爬虫 kugou音乐详细教程(requests模块)
  19. 【今日CV 计算机视觉论文速览 第140期】Wed, 3 Jul 2019
  20. Android开发技巧:我的菜单我做主

热门文章

  1. python实现输出日历_python实现输入日期打印日历
  2. jrebel离线激活_jrebel激活
  3. java解析dcm文件
  4. windos开启IIS管理器
  5. 关于strict-origin-when-cross-origin404请求的问题
  6. ubuntu编译tensorflow 支持AVX,AVX2等指令集
  7. 68个Python内置函数详解,进阶必备!
  8. 【Axure PR原型模板】微信公众小程序手机移动端高保真交互原型
  9. Linux之上传文件到服务器上
  10. matlab 按文件名排序,文件名排序Matlab程序