zoukankan      html  css  js  c++  java
  • JAVA-使用zxing jar包生成二维码和解析二维码

    内容出处https://study.163.com/course/courseMain.htm?courseId=1209288803

    GitHub网址https://github.com/zxing/zxing

    jar包下载地址https://github.com/zxing/zxing/wiki/Getting-Started-Developing

    第一个jar包

     

     

     第二个jar包

     

     总共下载两个jar包

     将两个jar包添加到项目中

    生成二维码,源代码

    package my;
    
    import java.io.File;
    import java.nio.file.Path;
    import java.util.HashMap;
    
    import com.google.zxing.BarcodeFormat;
    import com.google.zxing.EncodeHintType;
    import com.google.zxing.MultiFormatWriter;
    import com.google.zxing.client.j2se.MatrixToImageWriter;
    import com.google.zxing.common.BitMatrix;
    import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
    
    public class test
    {
    
        public static void main(String[] args)
        {
            // TODO Auto-generated method stub
    
            int width = 300;
            int height = 300;
            String content = "https://cn.bing.com/";
            
            //二维码的参数设置
            HashMap<EncodeHintType, Object> hints = new HashMap();
            hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");//字符集
            hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);//纠错级别
            hints.put(EncodeHintType.MARGIN, 2);//空白
            //hints.put(EncodeHintType.QR_VERSION, 5);//版本1-40,内部会自动适应,不用设置
            
            try
            {
                MultiFormatWriter writer = new MultiFormatWriter();
                BitMatrix bitMatrix = writer.encode(content, BarcodeFormat.QR_CODE, width, height, hints);
                
                Path file = new File("D:\1\2\二维码.png").toPath();
                MatrixToImageWriter.writeToPath(bitMatrix, "png", file);
                
                
            }catch(Exception e)
            {
                e.printStackTrace();        
            }
            
            
        }
    
    }

    演示

    解析二维码,源代码

    package my;
    
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import java.util.HashMap;
    
    import javax.imageio.ImageIO;
    import javax.naming.spi.DirStateFactory.Result;
    
    import com.google.zxing.BinaryBitmap;
    import com.google.zxing.DecodeHintType;
    import com.google.zxing.MultiFormatReader;
    import com.google.zxing.NotFoundException;
    import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
    import com.google.zxing.common.HybridBinarizer;
    
    public class test
    {
    
        public static void main(String[] args) throws NotFoundException, IOException
        {
            // TODO Auto-generated method stub
    
            File file = new File("D:\1\2\二维码.png");
            BufferedImage image = ImageIO.read(file);
            
            //读取图片
            MultiFormatReader formatReader = new MultiFormatReader();
            BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));
            
            //解码参数
            HashMap<DecodeHintType, Object> hints = new HashMap<>();
            hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
            com.google.zxing.Result result = formatReader.decode(binaryBitmap, hints);
            
            System.out.println("类型:" + result.getBarcodeFormat());
            System.out.println("内容:" + result.getText());
            
            System.out.println("exit");
        }
    
    }

    演示

    程序员阿飞

    2021年5月21日

    作者: 阿飞

    出处: https://www.cnblogs.com/nxopen2018/>

    关于作者:专注NX开发、VC++开发、数据库、三维建模领域,请多多赐教!

    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出, 原文链接 如有问题, 可留言(博客文章底部留言)咨询.

  • 相关阅读:
    bzoj2002: [Hnoi2010]Bounce 弹飞绵羊 [分块][LCT]
    luoguP1886 滑动窗口 [单调队列]
    bzoj1047: [HAOI2007]理想的正方形
    bzoj1012: [JSOI2008]最大数maxnumber [单调队列]
    树与二叉树之二--二叉树的性质与存储
    树与二叉树之一--基本概念与存储结构
    Markdown段首空格
    C++ atan2
    凸包学习笔记
    Codeforces Round #545 (Div. 1) E. Train Car Selection
  • 原文地址:https://www.cnblogs.com/nxopen2018/p/14797652.html
Copyright © 2011-2022 走看看