zoukankan      html  css  js  c++  java
  • 识别二维码

    一、pom

    <dependency>
                <groupId>com.google.zxing</groupId>
                <artifactId>core</artifactId>
                <version>3.3.3</version>
            </dependency>
            <dependency>
                <groupId>com.google.zxing</groupId>
                <artifactId>javase</artifactId>
                <version>3.3.3</version>
            </dependency>
            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus-generator</artifactId>
                <version>3.3.0</version>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>1.2.28</version>
            </dependency>
            <dependency>
                <groupId>commons-beanutils</groupId>
                <artifactId>commons-beanutils</artifactId>
                <version>1.9.3</version>
            </dependency>
            <dependency>
                <groupId>commons-collections</groupId>
                <artifactId>commons-collections</artifactId>
                <version>3.2.1</version>
            </dependency>
            <dependency>
                <groupId>commons-lang</groupId>
                <artifactId>commons-lang</artifactId>
                <version>2.6</version>
            </dependency>
            <dependency>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
                <version>1.1.1</version>
            </dependency>
            <dependency>
                <groupId>net.sf.ezmorph</groupId>
                <artifactId>ezmorph</artifactId>
                <version>1.0.6</version>
            </dependency>
            <dependency>
            <groupId>net.sf.json-lib</groupId>
            <artifactId>json-lib</artifactId>
            <version>2.2.3</version>
            <classifier>jdk15</classifier>
            </dependency>

    二、识别两种方式

    方式一:

    package com.roncoo.mybatis.generator;
    
    import com.alibaba.fastjson.JSON;
    import com.google.zxing.*;
    import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
    import com.google.zxing.common.HybridBinarizer;
    import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
    import net.sf.json.JSONObject;
    
    import javax.imageio.ImageIO;
    import java.awt.image.BufferedImage;
    import java.io.*;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.nio.file.Path;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    /**
     * @author ZhangRF
     */
    public class JianSuo {
    
        public static String getJsonData(JSONObject jsonParam, String urls, String access_token) {
            StringBuffer sb=new StringBuffer();
            urls += access_token;
            try {
                // 创建url资源
                URL url = new URL(urls);
                // 建立http连接
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                // 设置允许输出
                conn.setDoOutput(true);
                // 设置允许输入
                conn.setDoInput(true);
                // 设置不用缓存
                conn.setUseCaches(false);
                // 设置传递方式
                conn.setRequestMethod("POST");
                // 设置维持长连接
                conn.setRequestProperty("Connection", "Keep-Alive");
                // 设置文件字符集:
                conn.setRequestProperty("Charset", "UTF-8");
                // 转换为字节数组
                byte[] data = (jsonParam.toString()).getBytes();
                // 设置文件长度
                conn.setRequestProperty("Content-Length", String.valueOf(data.length));
                // 设置文件类型:
                conn.setRequestProperty("contentType", "application/json");
                // 开始连接请求
                conn.connect();
                OutputStream out = new DataOutputStream(conn.getOutputStream()) ;
                // 写入请求的字符串
                out.write((jsonParam.toString()).getBytes());
                out.flush();
                out.close();
                // 请求返回的状态
                if (HttpURLConnection.HTTP_OK == conn.getResponseCode()){
                    // 请求返回的数据
                    InputStream in1 = conn.getInputStream();
                    try {
                        String readLine=new String();
                        BufferedReader responseReader=new BufferedReader(new InputStreamReader(in1,"UTF-8"));
                        while((readLine=responseReader.readLine())!=null){
                            sb.append(readLine).append("
    ");
                        }
                        responseReader.close();
                    } catch (Exception e1) {
                        e1.printStackTrace();
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return sb.toString();
        }
        public static void main(String[] args) throws Exception {
            //定义二维码的参数
            HashMap hints = new HashMap();
            hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
            //L级:约可纠错7%的数据码字,M级:约可纠错15%的数据码字,Q级:约可纠错25%的数据码字,H级:约可纠错30%的数据码字
            hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.Q);
            hints.put(EncodeHintType.MARGIN, 2);
    
            Path file = new File("C:\Users\Administrator\Desktop\123.jpg").toPath();
            MultiFormatReader formatReader = new MultiFormatReader();
            BufferedImage image = ImageIO.read(file.toFile());
            BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));
            Result result = formatReader.decode(binaryBitmap, hints);
            System.out.println("二维码解析结果:" + result.toString());
            System.out.println("二维码的格式:" + result.getBarcodeFormat());
            System.out.println("二维码的文本内容:" + result.getText());
    
            Map<String,Object> map = new HashMap<>();
            map.put("begin_date","2018-11-01");
            map.put("end_date","2018-11-01");
    //        String outStr = JsonUtil.mapToJson(map);
            //map转json对象
            String outStr = JSON.toJSONString(map);
            String s = getJsonData(JSONObject.fromObject(outStr),
                    result.toString(),
                    "access_token");
    //        Map maps=(Map) JSON.parse(s);
            System.out.println(s);
    //        System.out.println(maps.get("list").toString());
    //        List<Map<String, Object>> mapList = JsonUtil.parseJSONList(maps.get("list").toString());
        }
    }

    方式二、

    package com.roncoo.mybatis.generator;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.net.MalformedURLException;
    import java.net.URL;
    /**
     * 通过URL读取页面内容
     * */
    
    public class MyClass {
    
        public static void main(String[] args){
    
            System.out.println("hello world !!!");
    
            try {
                //创建一个URL实例
                URL url = new URL("https://weixin110.qq.com/s/06906701888");
    
                try {
                    //通过URL的openStrean方法获取URL对象所表示的自愿字节输入流
                    InputStream is = url.openStream();
                    InputStreamReader isr = new InputStreamReader(is,"utf-8");
    
                    //为字符输入流添加缓冲
                    BufferedReader br = new BufferedReader(isr);
                    String data = br.readLine();//读取数据
    
                    while (data!=null){//循环读取数据
                        System.out.println(data);//输出数据
                        data = br.readLine();
                    }
                    br.close();
                    isr.close();
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
        }
    }
  • 相关阅读:
    MVC6 发布IIS
    Spring-Spring Bean后置处理器
    Java中重载和重写
    Java基础
    Python高级教程-多重继承
    Spring Bean声明周期
    Spring Bean作用域
    Spring-ApplicationContext容器
    Spring-BeanFactory容器
    Spring-Spring IoC容器
  • 原文地址:https://www.cnblogs.com/zhangrongfei/p/12888292.html
Copyright © 2011-2022 走看看