• 转换为base64格式


    package com.hxh.basic.project.Test02;
    
    import com.alibaba.fastjson.JSON;
    import com.google.zxing.BarcodeFormat;
    import com.google.zxing.EncodeHintType;
    import com.google.zxing.client.j2se.MatrixToImageWriter;
    import com.google.zxing.common.BitMatrix;
    import com.google.zxing.qrcode.QRCodeWriter;
    import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
    
    import java.io.ByteArrayOutputStream;
    import java.util.Hashtable;
    
    public class Test {
        public static void main(String[] args) {
            String str = "123456";
            getQRCodeImage(str);
        }
    
        /**
         * QR_CODE_DEFAULT_WIDTH 二维码默认宽
         */
        private static final int QR_CODE_DEFAULT_WIDTH = 320;
    
        /**
         * QR_CODE_DEFAULT_CHARSET 二维码默认的字符集
         */
        private static final String QR_CODE_DEFAULT_CHARSET = "utf-8";
    
        /**
         * 生成二维码,返回字节流
         *
         * @param inputString
         * @return String
         */
        public static String getQRCodeImage(String inputString) {
            System.out.println(String.format("UserServiceImpl.getQRCodeImage request inputString=%s", inputString));
    
            String result = "";
            try {
                QRCodeWriter qrCodeWriter = new QRCodeWriter();
                Hashtable hints = new Hashtable();
                hints.put(EncodeHintType.CHARACTER_SET, QR_CODE_DEFAULT_CHARSET);
                BitMatrix bitMatrix = qrCodeWriter.encode(inputString, BarcodeFormat.QR_CODE, QR_CODE_DEFAULT_WIDTH, QR_CODE_DEFAULT_WIDTH, hints);
                bitMatrix = deleteWhite(bitMatrix);
                ByteArrayOutputStream pngOutputStream = new ByteArrayOutputStream();
                MatrixToImageWriter.writeToStream(bitMatrix, "png", pngOutputStream);
                result = "data:image/png;base64," + Base64.encode(pngOutputStream.toByteArray());
                System.out.println(String.format("base64 res=%s", result));
            } catch (Exception e) {
                System.out.println(String.format("UserServiceImpl.getQRCodeImage exception! 错误信息=%s", e));
                System.out.println(String.format("com.jd.crm.gateway.service.user.impl.UserServiceImpl.getQRCodeImage(java.lang.String):result=%s", JSON.toJSON(result)));
                return "错误";
            }
            return result;
        }
    
        /**
         * 二维码方法封装
         *
         * @param matrix
         * @return BitMatrix
         */
        private static BitMatrix deleteWhite(BitMatrix matrix) {
            int[] rec = matrix.getEnclosingRectangle();
            int resWidth = rec[2] + 1;
            int resHeight = rec[3] + 1;
    
            BitMatrix resMatrix = new BitMatrix(resWidth, resHeight);
            resMatrix.clear();
            for (int i = 0; i < resWidth; i++) {
                for (int j = 0; j < resHeight; j++) {
                    if (matrix.get(i + rec[0], j + rec[1])) {
                        resMatrix.set(i, j);
                    }
                }
            }
            return resMatrix;
        }
    }
    
    // TODO 输出:UserServiceImpl.getQRCodeImage request inputString=123456
    // TODO 输出:base64 res=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOgAAADoAQAAAADN0pXVAAABEklEQVR42u3ZsRGDMAwFUHEUKRmBUTIaHo1RGIEyBYeCJDu2ORJjp/1qkuO5UhR/A0ShHmyfHc/hUkfQCh1Zaz503GhaO/mqtULrdLPmisaFR03QZpUaoP+rH1NoqyZj2rOzPl/tDNCCJnkUdtHrtIL+Vk5qsWSPBa1QGVNetc+82MTyi/xCaIVKO92w09M666TPppP/FaD3VHZRUe2zT3Yivw1AK5T5SHbrsw4v73Yz1EvLoRX6OWKSjanfBtJkh97SPH2ibudkhxY0OSPJJVHZUJf+fNqEljR9vpHkkYY8tEqz5xsu5pE9c4M2qDY36TO0WTXkOdxL2mkTel+z/344I426EFqj+buJ78kOLSj/qjdgrUGR/YlwbwAAAABJRU5ErkJggg==
    
    
  • 相关阅读:
    高级查询相关练习
    高级查询
    CRUD简单查询
    eclipse 实际运用
    控制台输入与Math Random的基本使用
    调用方法
    jQuery 基础语法
    Java第二课 分支与循环
    Java第一课
    网页项目制作收获2
  • 原文地址:https://www.cnblogs.com/Twittery/p/14831932.html
Copyright © 2020-2023  润新知