久久久精品一区ed2k-女人被男人叉到高潮的视频-中文字幕乱码一区久久麻豆樱花-俄罗斯熟妇真实视频

如何使用java實現(xiàn)通用OCR文字識別

這篇文章給大家分享的是有關(guān)如何使用java實現(xiàn)通用OCR文字識別的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

創(chuàng)新互聯(lián)堅持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站建設(shè)、成都做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時代的梨林網(wǎng)站設(shè)計、移動媒體設(shè)計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!

具體內(nèi)容如下

1.通用OCR文字識別

這種OCR只能按照識別圖片中的文字,且是按照行識別返回結(jié)果,精度較低。

首先引入依賴包:

<dependency>
  <groupId>com.baidu.aip</groupId>
  <artifactId>java-sdk</artifactId>
  <version>4.6.0</version>
</dependency>

通過OCR工具類:

package util;
 
import com.baidu.aip.ocr.AipOcr;
import org.json.JSONObject;
import java.util.HashMap;
 
 
public class OcrApi {
  private static final String APP_ID = "你的 App ID";
  private static final String API_KEY = "Xb12m5t4jS2n7";
  private static final String SECRET_KEY = "9XVx9GPcSbSUTZ";
 
  private static AipOcr getAipClient() {
    return getAipClient(API_KEY, SECRET_KEY);
  }
 
  public static AipOcr getAipClient(String apiKey, String secretKey) {
    AipOcr client = new AipOcr(APP_ID, apiKey, secretKey);
    // 可選:設(shè)置網(wǎng)絡(luò)連接參數(shù)
    client.setConnectionTimeoutInMillis(2000);
    client.setSocketTimeoutInMillis(60000);
    return client;
  }
 
  public static String result(AipOcr client) {
    // 傳入可選參數(shù)調(diào)用接口
    HashMap<String, String> options = new HashMap<>();
    options.put("language_type", "CHN_ENG");
    options.put("detect_direction", "true");
    options.put("detect_language", "true");
    options.put("probability", "true");
 
    JSONObject res = client.basicGeneralUrl(
        "https://lichunyu1234.oss-cn-shanghai.aliyuncs.com/1.png", options);
    return res.toString(2);
  }
 
  public static void main(String[] args) {
    System.out.println(result(getAipClient()));
  }
}

結(jié)果如下,識別有兩行信息(words即是識別的信息):

如何使用java實現(xiàn)通用OCR文字識別

2.高精度OCR識別身份證信息 

這種就比較高精度,且按照分類顯示,返回數(shù)據(jù)更友好,高可用。

2.1 接口說明及請求參數(shù)是地址官方截圖如下:

如何使用java實現(xiàn)通用OCR文字識別

如何使用java實現(xiàn)通用OCR文字識別

2.2 OCR身份證識別工具類

package util;
 
import com.alibaba.druid.util.Base64;
import com.alibaba.fastjson.JSONObject;
import java.io.*;
import java.net.*;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
 
public class OcrUtil {
  // Access_Token獲取
  private static final String ACCESS_TOKEN_HOST = "https://aip.baidubce.com/oauth/2.0/token?";
  // 身份證識別請求URL
  private static final String OCR_HOST = "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard?";
  // apiKey,secretKey
  private static final String API_KEY ="Xb12m5t4jS";
  private static final String SECRET_KEY = "9XVx9GPcSbSUT";
 
 
  // 獲取百度云OCR的授權(quán)access_token
  public static String getAccessToken() {
    return getAccessToken(API_KEY, SECRET_KEY);
  }
 
  /**
   * 獲取百度云OCR的授權(quán)access_token
   * @param apiKey
   * @param secretKey
   * @return
   */
  public static String getAccessToken(String apiKey, String secretKey) {
    String accessTokenURL = ACCESS_TOKEN_HOST
        // 1. grant_type為固定參數(shù)
        + "grant_type=client_credentials"
        // 2. 官網(wǎng)獲取的 API Key
        + "&client_id=" + apiKey
        // 3. 官網(wǎng)獲取的 Secret Key
        + "&client_secret=" + secretKey;
 
    try {
      URL url = new URL(accessTokenURL);
      // 打開和URL之間的連接
      HttpURLConnection connection = (HttpURLConnection) url.openConnection();
      connection.setRequestMethod("GET");
      connection.connect();
 
      // 獲取響應(yīng)頭
      Map<String, List<String>> map = connection.getHeaderFields();
      // 遍歷所有的響應(yīng)頭字段
      for (String key : map.keySet()) {
        System.out.println(key + "---->" + map.get(key));
      }
 
      // 定義 BufferedReader輸入流來讀取URL的響應(yīng)
      BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
      StringBuilder result = new StringBuilder();
      String inputLine;
      while ((inputLine = bufferedReader.readLine()) != null) {
        result.append(inputLine);
      }
      JSONObject jsonObject = JSONObject.parseObject(result.toString());
      return jsonObject.getString("access_token");
 
    } catch (Exception e) {
      e.printStackTrace();
      System.err.print("獲取access_token失敗");
    }
    return null;
  }
 
  /**
   * 獲取身份證識別后的數(shù)據(jù)
   * @param imageUrl
   * @param idCardSide
   * @return
   */
  public static String getStringIdentityCard(File imageUrl, String idCardSide) {
    // 身份證OCR的http URL+鑒權(quán)token
    String OCRUrl = OCR_HOST+"access_token="+getAccessToken();
    System.out.println(OCRUrl);
    System.out.println("***************************************************");
    System.out.println(getAccessToken());
    // 對圖片進行base64處理
    String image = encodeImageToBase64(imageUrl);
    // 請求參數(shù)
    String requestParam = "detect_direction=true&id_card_side="+idCardSide+"&image="+image;
 
    try {
      // 請求OCR地址
      URL url = new URL(OCRUrl);
      HttpURLConnection connection = (HttpURLConnection) url.openConnection();
      // 設(shè)置請求方法為POST
      connection.setRequestMethod("POST");
 
      // 設(shè)置請求頭
      connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
      connection.setRequestProperty("apiKey", API_KEY);
      connection.setDoOutput(true);
      connection.getOutputStream().write(requestParam.getBytes(StandardCharsets.UTF_8));
      connection.connect();
 
      // 定義 BufferedReader輸入流來讀取URL的響應(yīng)
      BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
      StringBuilder result = new StringBuilder();
      String inputLine;
      while ((inputLine = bufferedReader.readLine()) != null) {
        result.append(inputLine);
      }
      bufferedReader.close();
      return result.toString();
    } catch (Exception e) {
      e.printStackTrace();
      System.err.println("身份證OCR識別異常");
      return null;
    }
  }
 
  /**
   * 對圖片url進行Base64編碼處理
   * @param imageUrl
   * @return
   */
  public static String encodeImageToBase64(File imageUrl) {
    // 將圖片文件轉(zhuǎn)化為字節(jié)數(shù)組字符串,并對其進行Base64編碼處理
    byte[] data = null;
    try {
      InputStream inputStream = new FileInputStream(imageUrl);
      data = new byte[inputStream.available()];
      inputStream.read(data);
      inputStream.close();
 
      // 對字節(jié)數(shù)組Base64編碼
      return URLEncoder.encode(Base64.byteArrayToBase64(data), "UTF-8");
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    }
 
  }
 
  /**
   * 提取OCR識別身份證有效信息
   * @param
   * @return
   */
  public static Map<String, String> getIdCardInfo(MultipartFile image, int idCardSide) {
    String value = getStringIdentityCard(image, idCardSide);
    String side;
    if (idCardSide == 1) {
      side = "正面";
    }else {
      side = "背面";
    }
    Map<String, String> map = new HashMap<>();
    JSONObject jsonObject = JSONObject.parseObject(value);
    JSONObject words_result = jsonObject.getJSONObject("words_result");
    if (words_result == null || words_result.isEmpty()) {
      throw new MyException("請?zhí)峁┥矸葑C"+side+"圖片");
    }
    for (String key : words_result.keySet()) {
      JSONObject result = words_result.getJSONObject(key);
      String info = result.getString("words");
      switch (key) {
        case "姓名":
          map.put("name", info);
          break;
        case "性別":
          map.put("sex", info);
          break;
        case "民族":
          map.put("nation", info);
          break;
        case "出生":
          map.put("birthday", info);
          break;
        case "住址":
          map.put("address", info);
          break;
        case "公民身份號碼":
          map.put("idNumber", info);
          break;
        case "簽發(fā)機關(guān)":
          map.put("issuedOrganization", info);
          break;
        case "簽發(fā)日期":
          map.put("issuedAt", info);
          break;
        case "失效日期":
          map.put("expiredAt", info);
          break;
      }
    }
    return map;
 
  }
 
}

官方返回示例:

如何使用java實現(xiàn)通用OCR文字識別

對于身份證識別有個大坑:

1.有的base64編碼后有頭部“Base64:”要去掉,阿里巴巴的base64可以正常使用。

2.OCR識別官方只說明圖片要Base64編碼,但是實際上還是要再UrlEncode再編碼一次才可以。

感謝各位的閱讀!關(guān)于“如何使用java實現(xiàn)通用OCR文字識別”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

本文標(biāo)題:如何使用java實現(xiàn)通用OCR文字識別
文章路徑:http://sd-ha.com/article12/gpssgc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、定制開發(fā)、網(wǎng)站制作、外貿(mào)網(wǎng)站建設(shè)企業(yè)網(wǎng)站制作、商城網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運營