日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

java----百度識別身份證識別api使用方法

 印度阿三17 2019-05-22

前言
百度識別api是一個非常實用的工具,他可以非常有效的進行身份信息讀取,我們可以通過他來獲取到身份證上的所有信息,百度識別不管只具有身份證識別,還有很多證件的識別api。
接下來我們說具體實現(xiàn)流程:首先要引用SDK包,然后獲取百度地圖提供的accessToken碼,然后存入圖片路徑進行數(shù)據(jù)返回。
1.引入百度地圖SDK包
(1)在http://ai.baidu.com/sdk下載SDK
(2)將下載的aip-java-sdk-version.zip解壓后,復(fù)制到工程文件夾中。
(3)在Eclipse右鍵“工程 -> Properties -> Java Build Path -> Add JARs”。
(4)添加SDK工具包aip-java-sdk-version.jar和第三方依賴工具包json-20160810.jar log4j-1.2.17.jar。
2.獲取accessToken
package com.telematics.wx.util;

import org.json.JSONObject;

import .BufferedReader;
import .InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Map;

/**

  • 獲取token類
    /
    public class AuthService {
    public static void main(String[] args) {
    String str= getAuth();
    System.out.println(str);
    }
    /
    *

    • 獲取權(quán)限token
    • @return 返回示例:
    • {
    • “access_token”: “24.460da4889caad24cccdb1fea17221975.2592000.1491995545.282335-1234567”,
    • “expires_in”: 2592000
    • }
      */
      public static String getAuth() {
      // 官網(wǎng)獲取的 API Key 更新為你注冊的
      String clientId = ";
      // 官網(wǎng)獲取的 Secret Key 更新為你注冊的
      String clientSecret = “”;
      return getAuth(clientId, clientSecret);
      }

    /**

    • 獲取API訪問token
    • 該token有一定的有效期,需要自行管理,當(dāng)失效時需重新獲取.
    • @param ak - 百度云官網(wǎng)獲取的 API Key
    • @param sk - 百度云官網(wǎng)獲取的 Securet Key
    • @return assess_token 示例:
    • “24.460da4889caad24cccdb1fea17221975.2592000.1491995545.282335-1234567”
      /
      public static String getAuth(String ak, String sk) {
      // 獲取token地址
      String authHost = "https://aip./oauth/2.0/token?";
      String getAccessTokenUrl = authHost
      // 1. grant_type為固定參數(shù)
      “grant_type=client_credentials”
      // 2. 官網(wǎng)獲取的 API Key
      “&client_id=” ak
      // 3. 官網(wǎng)獲取的 Secret Key
      “&client_secret=” sk;
      try {
      URL realUrl = new URL(getAccessTokenUrl);
      // 打開和URL之間的連接
      HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();
      connection.setRequestMethod(“GET”);
      connection.connect();
      // 獲取所有響應(yīng)頭字段
      Map<String, List> map = connection.getHeaderFields();
      // 遍歷所有的響應(yīng)頭字段
      for (String key : map.keySet()) {
      System.err.println(key “—>” map.get(key));
      }
      // 定義 BufferedReader輸入流來讀取URL的響應(yīng)
      BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
      String result = “”;
      String line;
      while ((line = in.readLine()) != null) {
      result = line;
      }
      /
      *
      * 返回結(jié)果示例
      */
      System.err.println(“result:” result);
      JSONObject jsonObject = new JSONObject(result);
      String access_token = jsonObject.getString(“access_token”);
      return access_token;
      } catch (Exception e) {
      System.err.printf(“獲取token失?。 ?;
      e.printStackTrace(System.err);
      }
      return null;
      }

}
3.進行識別
@RequestMapping(“test”)
@ResponseBody
public String test(HttpServletRequest request,HttpServletResponse response, ModelMap model,HttpSession session){
System.out.println(“進入身份識別”);
// 身份證識別url
String idcardIdentificate = “https://aip./rest/2.0/ocr/v1/idcard”;
// 本地圖片路徑
String path=session.getServletContext().getRealPath("/upload");
System.out.println(“根目錄路徑” path);
String filePath = path "\" “shenfenzheng.jpg”;
System.out.println(“圖片路徑” filePath);
try {
byte[] imgData = FileUtil.readFileByBytes(filePath);//使用工具類進行轉(zhuǎn)碼
String imgStr = Base64Util.encode(imgData);//百度地圖的所有圖片均需要base64編碼、去掉編碼頭后再進行urlencode。//import com.baidu.aip.util.Base64Util;
// 識別身份證正面id_card_side=front;識別身份證背面id_card_side=back;
String params = “id_card_side=front&” URLEncoder.encode(“image”, “UTF-8”) “=”
URLEncoder.encode(imgStr, “UTF-8”);
String token=AuthService.getAuth();//調(diào)用上面的accessToken碼代碼
String accessToken = token;
String result = HttpUtil.post(idcardIdentificate, accessToken, params);
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
}
return “0”;
}
4.工具類
package com.goocom.util;

import .*;

/**

  • 文件讀取工具類
    */
    public class FileUtil {

    /**

    • 讀取文件內(nèi)容,作為字符串返回
      */
      public static String readFileAsString(String filePath) throws IOException {
      File file = new File(filePath);
      if (!file.exists()) {
      throw new FileNotFoundException(filePath);
      }

      if (file.length() > 1024 * 1024 * 1024) {
      throw new IOException(“File is too large”);
      }

      StringBuilder sb = new StringBuilder((int) (file.length()));
      // 創(chuàng)建字節(jié)輸入流
      FileInputStream fis = new FileInputStream(filePath);
      // 創(chuàng)建一個長度為10240的Buffer
      byte[] bbuf = new byte[10240];
      // 用于保存實際讀取的字節(jié)數(shù)
      int hasRead = 0;
      while ( (hasRead = fis.read(bbuf)) > 0 ) {
      sb.append(new String(bbuf, 0, hasRead));
      }
      fis.close();
      return sb.toString();
      }

    /**

    • 根據(jù)文件路徑讀取byte[] 數(shù)組
      */
      public static byte[] readFileByBytes(String filePath) throws IOException {
      File file = new File(filePath);
      if (!file.exists()) {
      throw new FileNotFoundException(filePath);
      } else {
      ByteArrayOutputStream bos = new ByteArrayOutputStream((int) file.length());
      BufferedInputStream in = null;

       try {
           in = new BufferedInputStream(new FileInputStream(file));
           short bufSize = 1024;
           byte[] buffer = new byte[bufSize];
           int len1;
           while (-1 != (len1 = in.read(buffer, 0, bufSize))) {
               bos.write(buffer, 0, len1);
           }
      
           byte[] var7 = bos.toByteArray();
           return var7;
       } finally {
           try {
               if (in != null) {
                   in.close();
               }
           } catch (IOException var14) {
               var14.printStackTrace();
           }
      
           bos.close();
       }
      

      }
      }
      }
      到此結(jié)束,第一次寫博客有些不太熟練希望大家諒解,有什么要求和評論希望大家一起學(xué)習(xí)。

來源:http://www./content-1-202201.html

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多