zoukankan      html  css  js  c++  java
  • 百度AI接口---身份证识别Demo

    题记:自己是做java web的,但是本人以前接触并学习很多图像的知识,所以对图像很敏感。下面以百度的一个接口,实现身份证识别案例

    1、需要百度开发者AppID、SecretKey 、API Key。

    先注册,然后进入https://console.bce.baidu.com/ai/?fromai=1#/ai/ocr/overview/index,这个网址,选择文字识别,创建应用,获取AppID、SecretKey 、API Key。

    2、创建一个maven项目,pom.xml如下:

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.fx</groupId>
      <artifactId>demo</artifactId>
      <packaging>war</packaging>
      <version>0.0.1-SNAPSHOT</version>
      <name>demo Maven Webapp</name>
      <url>http://maven.apache.org</url>
      <dependencies>
        <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.11</version>
                <scope>test</scope>
        </dependency>
            
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>
        
        <!-- 百度文字识别SDK -->
        <dependency>
            <groupId>com.baidu.aip</groupId>
            <artifactId>java-sdk</artifactId>
            <version>4.6.1</version>
        </dependency>
      </dependencies>
      <build>
        <finalName>demo</finalName>
      </build>
    </project>

    3、创建BaiduConfig.java、demo.java、Utils.java

    package com.fx.config;
    public class BaiduConfig {
        public static final String APP_ID = "***";//自己的
        public static final String API_KEY = "****";//自己的
        public static final String SECRET_KEY = "***";//自己的
    
    }
    package com.fx.demo;
    
    import java.util.HashMap;
    import org.json.JSONObject;
    import com.baidu.aip.ocr.AipOcr;
    import com.fx.config.BaiduConfig;
    import com.fx.utils.Utils;
    
    public class demo {
        
         //设置APPID/AK/SK
        public static void main(String[] args) {
            IDCardRecogize();
           
        }
       public static void IDCardRecogize() {
           AipOcr client = new AipOcr(BaiduConfig.APP_ID, BaiduConfig.API_KEY, BaiduConfig.SECRET_KEY);
        // 传入可选参数调用接口
            HashMap<String, String> options = new HashMap<String, String>();
            options.put("detect_direction", "true");
            options.put("detect_risk", "false");
            //背面照
            //String idCardSide = "back";
            
            //前面照
            String idCardSide = "front";
            
            // 参数为本地图片路径
            //String image = "D:\back.jpg";
            //String image = "D:\front.jpg";
           String image = "F:\picture\shenfenzheng.jpg";//身份证照片路径
            JSONObject res = client.idcard(image, idCardSide, options);
            System.out.println(res.toString(2));
    
            // 参数为本地图片二进制数组
            byte[] file = Utils.readImageFile(image);
            res = client.idcard(file, idCardSide, options);
            System.out.println(res.toString(2));
    
       }
       
      
    }
    package com.fx.utils;
    
    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    
    import javax.imageio.stream.FileImageInputStream;
    
    public class Utils {
            
        /**
            将图像转为二进制数组
       * @param path
       * @return
       */
      public static byte[] readImageFile(String path){
            byte[] data = null;
            FileImageInputStream input = null;
            try {
              input = new FileImageInputStream(new File(path));
              ByteArrayOutputStream output = new ByteArrayOutputStream();
              byte[] buf = new byte[1024];
              int numBytesRead = 0;
              while ((numBytesRead = input.read(buf)) != -1) {
              output.write(buf, 0, numBytesRead);
              }
              data = output.toByteArray();
              output.close();
              input.close();
            }
            catch (FileNotFoundException ex1) {
              ex1.printStackTrace();
            }
            catch (IOException ex1) {
              ex1.printStackTrace();
            }
            return data;
          }
    }

    这样就可以了,直接运行吧!不懂的或者报错的,可以留言........。

  • 相关阅读:
    Aspnet_regsql.exe命令行使用小结
    ListView用法学习
    < %=...%>< %#... %>< % %>< %@ %>
    jQuery入门简介
    oracle基础琐碎总结删除数据
    WindowsPhone基础琐碎总结数据绑定(一)
    ADO.NET基础琐碎总结参数化查询
    oracle基础琐碎总结Where和Having的区别与联系
    第一次使用 Windows Live Writer
    WindowsPhone基础琐碎总结数据绑定(二)
  • 原文地址:https://www.cnblogs.com/ywjfx/p/10093366.html
Copyright © 2011-2022 走看看