• zbb20181221 java,二维码


    1、maven

    <!-- 二维码 -->
            <dependency>
                <groupId>com.google.zxing</groupId>
                <artifactId>core</artifactId>
                <version>3.3.0</version>
            </dependency>
            <dependency>
                <groupId>com.google.zxing</groupId>
                <artifactId>javase</artifactId>
                <version>3.3.0</version>
            </dependency>

    2、ZxingHandler

    package com.zbb.app.erweima;
    
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.util.Hashtable;
    import java.util.Map;
    import javax.imageio.ImageIO;
    import com.google.zxing.BarcodeFormat;
    import com.google.zxing.BinaryBitmap;
    import com.google.zxing.DecodeHintType;
    import com.google.zxing.EncodeHintType;
    import com.google.zxing.LuminanceSource;
    import com.google.zxing.MultiFormatReader;
    import com.google.zxing.MultiFormatWriter;
    import com.google.zxing.Result;
    import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
    import com.google.zxing.client.j2se.MatrixToImageWriter;
    import com.google.zxing.common.BitMatrix;
    import com.google.zxing.common.HybridBinarizer;
    import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
    
    /**
     * 二维码生成器
     * 
     * @blog 
     * @author zhaozhen02
     */
    public class ZxingHandler {
    
        /**
         * 生成二维码
         * 
         * @param contents
         * @param width
         * @param height
         * @param imgPath
         * @throws Exception 
         */
        public static void encode(String contents, int width, int height, String imgPath)
                throws Exception {
            Map<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
            // 指定纠错等级
            hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
            // 指定编码格式
            hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
    
            BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width,
                    height, hints);
    
            MatrixToImageWriter.writeToStream(bitMatrix, "png", new FileOutputStream(imgPath));
    
        }
    
        /**
         * 解析二维码
         * 
         * @param imgPath
         * @return
         * @throws Exception 
         */
        public static String decode(String imgPath) throws Exception {
            BufferedImage image = null;
            Result result = null;
    
            image = ImageIO.read(new File(imgPath));
            if (image == null) {
                System.out.println("the decode image may be not exit.");
            }
            LuminanceSource source = new BufferedImageLuminanceSource(image);
            BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
    
            Map<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();
            hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
    
            result = new MultiFormatReader().decode(bitmap, hints);
            return result.getText();
    
        }
    
        public static void main(String[] args) throws Exception {
            encode("https://www.cnblogs.com/super-admin/", 300, 400,
                    "C:\Users\Administrator\Desktop\temp\test2.png");
    
            System.out.println(decode("C:\\Users\\Administrator\\Desktop\\temp\\test2.png"));
    
        }
    
    }
  • 相关阅读:
    如何给swing加上alt+x和ctrl+x快捷键
    java基础之登录程序
    RFID UHF(EPC)标签使用常识
    史密斯圆图
    C# DataTable Operations
    温度测量【RTD】
    温度测量【温度传感器类型】
    C# Debug
    c# ComboBox绑定枚举
    c# 隐藏Tab控件的标签
  • 原文地址:https://www.cnblogs.com/super-admin/p/10157026.html
Copyright © 2020-2023  润新知