| | |
| | | import cn.huge.module.sys.dto.IdcardOcrResultDTO; |
| | | import com.baidu.aip.ocr.AipOcr; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.json.JSONArray; |
| | | import org.json.JSONObject; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * @title: 百度云OCR工具类 |
| | |
| | | public static final String APP_ID = "27535980"; |
| | | public static final String API_KEY = "6U6tSA75gKf4UMXvHgDpzOC9"; |
| | | public static final String SECRET_KEY = "BBjjCDhy2sAVO0jfiaGn8OWIGE470lWU"; |
| | | // 个人账号-18607214221 |
| | | public static final String APP_ID_TEXT = "113806304"; |
| | | public static final String API_KEY_TEXT = "ajKtDuIs8xr0XvjbUZLPGi8C"; |
| | | public static final String SECRET_KEY_TEXT = "vgMrG5BJbUfGwKLda1yPWovTQWuRFWDB"; |
| | | |
| | | public static void main(String[] args) { |
| | | |
| | |
| | | } |
| | | } |
| | | |
| | | |
| | | public static Map<String, Object> ocrText(byte[] images) { |
| | | AipOcr client = new AipOcr(APP_ID_TEXT, API_KEY_TEXT, SECRET_KEY_TEXT); |
| | | // 传入可选参数调用接口 |
| | | HashMap<String, String> options = new HashMap<String, String>(); |
| | | options.put("language_type", "CHN_ENG"); |
| | | options.put("detect_direction", "true"); |
| | | options.put("detect_language", "true"); |
| | | options.put("probability", "true"); |
| | | |
| | | Map<String, Object> map = new HashMap<String, Object>(); |
| | | // 参数为本地图片二进制数组 |
| | | JSONObject res = client.basicAccurateGeneral(images, options); |
| | | map.put("wordsResultNum", res.getInt("words_result_num")); |
| | | map.put("direction", res.getInt("direction")); |
| | | |
| | | List<String> wordsResultList = new ArrayList<>(); |
| | | JSONArray jsonArray = res.getJSONArray("words_result"); |
| | | for(int i = 0; i < jsonArray.length(); i++){ |
| | | JSONObject wordResult = jsonArray.getJSONObject(i); |
| | | String words = wordResult.getString("words"); |
| | | if(StringUtils.isNotBlank(words)){ |
| | | wordsResultList.add(words); |
| | | } |
| | | } |
| | | map.put("wordsResult", wordsResultList); |
| | | return map; |
| | | } |
| | | |
| | | } |
| | | /** |
| | | * -------------------_ooOoo_------------------- |