zoukankan      html  css  js  c++  java
  • Java 二维码生成

    
    
    import com.google.zxing.BarcodeFormat;
    import com.google.zxing.EncodeHintType;
    import com.google.zxing.WriterException;
    import com.google.zxing.client.j2se.MatrixToImageWriter;
    import com.google.zxing.common.BitMatrix;
    import com.google.zxing.qrcode.QRCodeWriter;
    import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
    import lombok.SneakyThrows;
    
    import javax.imageio.ImageIO;
    import java.awt.image.BufferedImage;
    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.util.Base64;
    import java.util.Hashtable;



    public
    class QRCodeUtil { /** * 字符集 */ private static final String CHARSET = "utf-8"; /** * 生成二维码 BufferedImage * @param content 内容 * @param width 宽 * @param height 搞 * @return BufferedImage * @throws WriterException WriterException */ public static BufferedImage genQRCode(String content,int width,int height) throws WriterException { Hashtable<EncodeHintType, Object> hints = new Hashtable<>(); //纠错能力 设置为M hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M); //设置字符集 hints.put(EncodeHintType.CHARACTER_SET, CHARSET); //边缘 hints.put(EncodeHintType.MARGIN, 5); QRCodeWriter qrCodeWriter = new QRCodeWriter(); BitMatrix bitMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, width, height,hints); return MatrixToImageWriter.toBufferedImage(bitMatrix); } /** * 生成Base64 PNG字符串 * 例如: data:image/Png;base64,123123123123123132 * @param content 内容 * @param width 宽 * @param height 搞 * @return String 字符串 * @throws WriterException WriterException * @throws IOException IOException */ @SneakyThrows public static String genQRBase64(String content, int width, int height) { try { BufferedImage bufferedImage = genQRCode(content, width,height); String imageString = encodeImageToString(bufferedImage, ImageTypeEnum.PNG.getName()); return "data:image/Png;base64," + imageString; }catch (Exception ex){ throw new Exception(XdfnisBizCodeEnum.GEN_QR_CODE_ERROR.getMessage()); } } /** * Convert BufferedImage to Base64 * @param image BufferedImage * @param formatName 图片类型, ImageTypeEnum * @return String Base64 字符串 * @throws IOException 异常 */ public static String encodeImageToString(BufferedImage image, String formatName) throws IOException { String imageString ; ByteArrayOutputStream bos = new ByteArrayOutputStream(); ImageIO.write(image, formatName, bos); byte[] imageBytes = bos.toByteArray(); Base64.Encoder encoder = Base64.getEncoder(); imageString = encoder.encodeToString(imageBytes); bos.close(); return imageString; }
  • 相关阅读:
    C# ref与out区别
    天气预报
    全面理解javascript的caller,callee,call,apply概念(修改版)
    SQL注入案例曝光,请大家提高警惕
    sql字段保存值[1,2,3,4,5]复杂取法,收藏sql函数
    MySQL故障诊断常用方法手册(含脚本、案例)
    2021 年 8 月国产数据库排行榜:秋日胜春朝
    【墨天轮专访第二期】巨杉数据库萧少聪:重视企业长期需求,打造中国的世界级产品
    【我和达梦的故事】 有奖征文活动开始啦,万元奖品池+现金奖励等你拿!
    2021年8月国产数据库排行榜:TiDB稳榜首,达梦返前三,Kingbase进十强,各厂商加速布局云生态
  • 原文地址:https://www.cnblogs.com/xiaoyu369/p/14091570.html
Copyright © 2011-2022 走看看