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

一、前言

在我们开发的过程中,可能会遇到这个问题,要展示一个二维码,二维码里有很多数据,但是数据太多了,导致二维码过密,识别费劲,同时还存在安全问题,比如通过草料等软件解析,就能够知道二维码里的数据,这样也不安全。这个时候就可以试试pb来进行加密压缩了。

pb工具的百度网盘链接在最下方。

二、pb简介

pb全称protobuf,proto协议的生成和解析是开源代码,在github上搜com.google.protobuf就可以了。

三、pb简单使用方法

  1. 生成模型;
    创建一个proto文件,格式使用proto3的语法

  2. 编译成java或C#实体类
    利用protoc工具生成实体类
    包内包含了编译工具protoc.exe、一个proto格式样例、一个批量转换器

  3. 添加protobuf的包进行创建和解析
    将生成的实体类文件直接加入到工程中,并在pom文件中增加对应的包

        <dependency>
            <groupId>com.google.protobuf</groupId>
            <artifactId>protobuf-java</artifactId>
            <version>3.11.4</version>
        </dependency>

然后直接可以使用了。

四、例子

  1. 在pb工具和模型中定义
    在这里插入图片描述
    这里最好用a,b,c这样命名,可以减少字符长度。

  2. 点击bat,会生成一个包,里面包含一个文件。
    在这里插入图片描述
    把上面生成的文件放到项目中。

  3. 然后就可以进行调用了,下面相当于伪代码不能直接用,也不是核心

            if (b) {
                returnMsgEntity1.setCode(200);
                // 数据加密 这里缺少一项总金额
                ProxyApplactionDTO.Qrcode builder = ProxyApplactionDTO.Qrcode.newBuilder()
                        .setA(data.getEnterprise_code())
                        .setB(data.getEnterprise_name())
                        .setC(data.getBank() == null ? "" : data.getBank())
                        .setD(data.getAccount() == null ? "" : data.getAccount())
                        .setE(data.getAddress() == null ? "" : data.getAddress())

                        .setF(data.getPhone() == null ? "" : data.getPhone())
                        .setG(data.getTotal_price())
                        .setH(data.getNum())
                        .setI(data.getProduct_name())
                        .setJ(data.getSeller_phone())
                        .setK(data.getSeller_address())
                        .setL(data.getUnit() == null ? "" : data.getUnit())
                        .setM(data.getModel() == null ? "" : data.getModel())
                        .setN(data.getNote() == null?"":data.getNote())
                        .setO(data.getAll_price())
                        .build();
                byte[] compress = new byte[1024];
                try {
                    compress = CompressUtil.compressByte(builder.toByteArray());
                } catch (IOException e) {
                    e.printStackTrace();
                }
                // base64
                String base64 = Base64Util.encode(compress);
                redisService.setNoSeria("proxy","code",base64,1000*60*60);
                // 解
                byte[] decode = Base64Util.decode(base64);
                byte[] bytes = CompressUtil.uncompressByte(decode);
                ProxyApplactionDTO.Qrcode p2 = ProxyApplactionDTO.Qrcode.parseFrom(bytes);
  1. 核心代码,CompressUtil工具类,主要是压缩解压缩的。
package com.aisino.util.compress;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

public class CompressUtil {
    // 压缩
    public static String compress(String str) throws IOException {
        if (str == null || str.length() == 0) {
            return str;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        GZIPOutputStream gzip = new GZIPOutputStream(out);
        gzip.write(str.getBytes());
        gzip.close();
        return out.toString("ISO-8859-1");
    }
    // 压缩
    public static byte[] compressByte(byte[] bytes) throws IOException {
        if (bytes == null || bytes.length == 0) {
            return bytes;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        GZIPOutputStream gzip = new GZIPOutputStream(out);
        gzip.write(bytes);
        gzip.close();
        return out.toByteArray();
    }

    // 解压缩
    public static byte[] uncompressByte(byte[] bytes) throws IOException {
        if (bytes == null || bytes.length == 0) {
            return bytes;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ByteArrayInputStream in = new ByteArrayInputStream(bytes);
        GZIPInputStream gunzip = new GZIPInputStream(in);
        byte[] buffer = new byte[256];
        int n;
        while ((n = gunzip.read(buffer)) >= 0) {
            out.write(buffer, 0, n);
        }
        // toString()使用平台默认编码,也可以显式的指定如toString(&quot;GBK&quot;)
        return out.toByteArray();
    }
    // 解压缩
    public static String uncompress(String str) throws IOException {
        if (str == null || str.length() == 0) {
            return str;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ByteArrayInputStream in = new ByteArrayInputStream(str
                .getBytes("ISO-8859-1"));
        GZIPInputStream gunzip = new GZIPInputStream(in);
        byte[] buffer = new byte[256];
        int n;
        while ((n = gunzip.read(buffer)) >= 0) {
            out.write(buffer, 0, n);
        }
        // toString()使用平台默认编码,也可以显式的指定如toString(&quot;GBK&quot;)
        return out.toString();
    }
}

通过以上就能够吧二维码的数据压缩,并且数据越多,越好用。

五、pb工具和模型免费下载地址

下载地址

  • 32
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 34
    评论
评论 34
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

逸羽菲

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值