拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 Java使用pb【protobuf】压缩解决二维码内容过多导致二维码太密的问题

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

白鹭 - 2022-01-23 1969 0 0

文章目录

      • 一、前言
      • 二、pb简介
      • 三、pb简单使用方法
      • 四、例子
      • 五、pb工具和模型免费下载地址

一、前言

在我们开发的程序中,可能会遇到这个问题,要展示一个二维码,二维码里有很多资料,但是资料太多了,导致二维码过密,识别费劲,同时还存在安全问题,比如通过草料等软件决议,就能够知道二维码里的资料,这样也不安全,这个时候就可以试试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工具和模型免费下载地址

下载地址

标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *