• Java生成二维码


    Java生成二维码和解析二维码。

    public class QrcodeUtil {
        private static Logger logger = LogManager.getLogger();
    
        public static String createQrcode(String dir, String content) {
            return createQrcode(dir, content, 300, 300);
        }
    
        public static String createQrcode(String dir, String content, int width, int height) {
            try {
                String qrcodeFormat = "png";
                HashMap<EncodeHintType, String> hints = new HashMap<EncodeHintType, String>();
                hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
                BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
    
                File file = new File(dir, UUID.randomUUID().toString() + "." + qrcodeFormat);
                MatrixToImageWriter.writeToPath(bitMatrix, qrcodeFormat, file.toPath());
                return file.getAbsolutePath();
            } catch (Exception e) {
                logger.error("", e);
            }
            return "";
        }
    
        public static String decodeQr(String filePath) {
            String retStr = "";
            if ("".equalsIgnoreCase(filePath) && filePath.length() == 0) {
                return "图片路径为空!";
            }
            try {
                BufferedImage bufferedImage = ImageIO.read(new FileInputStream(filePath));
                LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);
                Binarizer binarizer = new HybridBinarizer(source);
                BinaryBitmap bitmap = new BinaryBitmap(binarizer);
                HashMap<DecodeHintType, Object> hintTypeObjectHashMap = new HashMap<>();
                hintTypeObjectHashMap.put(DecodeHintType.CHARACTER_SET, "UTF-8");
                Result result = new MultiFormatReader().decode(bitmap, hintTypeObjectHashMap);
                retStr = result.getText();
            } catch (Exception e) {
                logger.error("", e);
            }
            return retStr;
        }
  • 相关阅读:
    多线程(一)初步使用
    数据迁移:MSSQL脚本文件过大,客户端没有足够的内存继续执行程序
    统计数据,数据库就只有8,9,10的,而前端需要返回连续的记录
    Windows10禁用update
    C#模拟HTTP POST 请求
    C#中Equals和= =(等于号)的比较(转)
    .net framework4与其client profile版本的区别
    centos7 安装mysql
    JAVA中使用ASN.1
    使用gradle建立java application
  • 原文地址:https://www.cnblogs.com/se7end/p/9598697.html
Copyright © 2020-2023  润新知