zoukankan      html  css  js  c++  java
  • 项目ITP(一) 二维码

    前言

      系列文章:[传送门]

      上几周碌碌无为,不行啊不行啊。博客园,不知道你几时改版。老家了,我不会忘记你呢。呵呵,我也会在os,csdn更新的。每天一搏,不管有用没用。

    正文 

      正文先有项目起步,项目中的需求很明确。

        利用二维码扫描,然后实现签到功能。

      自然和app挂钩。 没事,一步一步来。

    二维码

       二维码(QR(Quick Response)code),又称二维条码,它是用特定的几何图形按一定规律在平面(二维方向)上分布的黑白相间的图形,是所有信息数据的一把钥匙

      

    利用工具-zxing

      ZXing是一个开放源码的,用Java实现的多种格式的1D/2D条码图像处理库,它包含了联系到其他语言的端口。Zxing可以实现使用手机的内置的摄像头完成条形码的扫描及解码。该项目可实现的条形码编码和解码

      大家可以去了解

      https://github.com/zxing/zxing/wiki/Getting-Started-Developing

    二维码(QRCode)的生成

      /**
         * 生成二维码图片
         * @param content        内容
         * @param width          宽度
         * @param height         高度
         * @param imgPath        存储图片路径
         */
    package sedion.jeffli.wmuitp.util.zxing;
    
    import java.io.File;
    import java.util.Hashtable;
    
    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;
    
    /**
     * @author Jeff Lee
     */
    public class ZxingEncoderHelper 
    {
    
        /**
         * 生成二维码图片
         * @param content        内容
         * @param width          宽度
         * @param height         高度
         * @param imgPath        存储图片路径
         */
        public void encode(String content, int width, int height, String imgPath) 
        {
            
            Hashtable<EncodeHintType, Object> hts = new Hashtable<EncodeHintType, Object>();
            hts.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);                // 纠错等级
            hts.put(EncodeHintType.CHARACTER_SET, "utf-8");                                  // 指定编码格式为UTF-8
            
            try
            {
                
                BitMatrix bitMatrix = new MultiFormatWriter().encode(content,                //编码内容,编码类型(这里指定为二维码),
                        BarcodeFormat.QR_CODE, width, height, hts);                          //图片宽度,图片高度,设置参数 
    
                MatrixToImageWriter
                        .writeToFile(bitMatrix, "png", new File(imgPath));                    //生成的二维码图片
    
            }
            catch (Exception e) 
            {
                e.printStackTrace();
            }
        }
    
        public static void main(String[] args) 
        {
            String imgPath = "d:/33.png";
            String contents = "你好!我的博客:http://www.cnblogs.com/Alandre/";
            int width = 300, height = 300;
            ZxingEncoderHelper handler = new ZxingEncoderHelper();
            handler.encode(contents, width, height, imgPath); 
        }
    }

     #BitMatrix  设置参数顺序分别为:编码内容,编码类型,生成图片宽度,生成图片高度,设置参数  

     #MatrixToImageWriter 生成所需要的文件

     你会找到这个图片

      

     

    二维码(QRCode)的解码

      

      /**
         * 解码
         * @param imgPath        二维码图片路径
         * @return
         */
    package sedion.jeffli.wmuitp.util.zxing;
    
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.util.Hashtable;
    
    import javax.imageio.ImageIO;
    
    import com.google.zxing.BinaryBitmap;
    import com.google.zxing.DecodeHintType;
    import com.google.zxing.LuminanceSource;
    import com.google.zxing.MultiFormatReader;
    import com.google.zxing.Result;
    import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
    import com.google.zxing.common.HybridBinarizer;
    
    /**
     * @author Jeff Lee
     */
    public class ZxingDecoderHandler 
    {
    
        /**
         * 解码
         * @param imgPath        二维码图片路径
         * @return
         */
        public String decode(String imgPath) 
        {
            
            BufferedImage image = null;
            Result result = null;
            
            try 
            {
                image = ImageIO.read(new File(imgPath));
                if (image == null) 
                {
                    System.out.println("文件不存在!");                                //应该抛个异常的
                }
                
                LuminanceSource source = new BufferedImageLuminanceSource(image);
                BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
    
                Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();
                hints.put(DecodeHintType.CHARACTER_SET, "utf-8");
    
                result = new MultiFormatReader().decode(bitmap, hints);
                return result.getText();
            }
            catch (Exception e) 
            {
                e.printStackTrace();
            }
            
            return null;
        }
    
        public static void main(String[] args) 
        {
            String imgPath = "d:/33.png";
            
            ZxingDecoderHandler handler = new ZxingDecoderHandler();
            String content = handler.decode(imgPath);
            
            System.out.println("内容如下:");
            System.out.println(content);  
        }
    }

    #和生成的相反

       

    总结

      二维码生成

      二维码解码

    感谢及资源共享

        

        路上走来一步一个脚印,希望大家和我一起。

        感谢读者!很喜欢你们给我的支持。如果支持,点个赞。

        知识来源: https://github.com/zxing/zxing/wiki/Getting-Started-Developing

  • 相关阅读:
    日期和时间运算:上月最后一天
    SY全局系统字段
    内表、结构赋值转换规则
    基本类型赋值转换规则表
    嵌套结构使用:struc1-struc2-XXX
    TYPES、DATA、TYPE、LIKE、CONSTANTS、STATICS、TABLES
    ABAP WRITE、WRITE TO、FORMAT语句
    ABAP DESCRIBE语句
    数据词典与ABAP类型映射
    Field+offset(len)
  • 原文地址:https://www.cnblogs.com/Alandre/p/3693162.html
Copyright © 2011-2022 走看看