二維碼解析器

weixin_34321977發表於2018-09-04
    <dependency>
        <groupId>com.google.zxing</groupId>
        <artifactId>core</artifactId>
        <version>3.0.0</version>
    </dependency>
    
    <dependency>
        <groupId>com.google.zxing</groupId>
        <artifactId>javase</artifactId>
        <version>3.0.0</version>
    </dependency>



    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import java.util.HashMap;
    import javax.imageio.ImageIO;
    import com.google.zxing.BinaryBitmap;
    import com.google.zxing.EncodeHintType;
    import com.google.zxing.MultiFormatReader;
    import com.google.zxing.Result;
    import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
    import com.google.zxing.common.HybridBinarizer;

    //解析二維碼
    public class ReadQRcode {

        public static void main(String[] args) {
            try {
                MultiFormatReader formatReader = new MultiFormatReader();
                File file = new File("D:/code/img.png");
                BufferedImage image = ImageIO.read(file);
                BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));
                HashMap hints = new HashMap();
                hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
                Result result = formatReader.decode(binaryBitmap,hints);
                System.out.println("解析結果"+result.toString());
                System.out.println("二維碼格式型別"+result.getBarcodeFormat());
                System.out.println("二維碼文字內容"+result.getText());
            } catch (Exception e) {
            // TODO Auto-generated catch block
                e.printStackTrace();
                }
        }
    }

相關文章