異常com.baidu.ocr.sdk.exception.SDKError[283604]App identifier unmatch.錯誤的packname或bundleId.logId::30309247
https://download.csdn.net/download/pyfysf/10406761
最終實現的效果(識別的有些慢,是由於我的網速原因。-_-)
最近有個小項目使用到了OCR技術,順便到網上搜索了一下,大家都在使用百度的API。所以我就調用了百度的接口。在使用的過程中也是遇到了各種各樣的錯誤。
比如TOKEN ERROR了。等等。
注冊登錄百度雲平台
首先注冊百度賬號,點擊這里跳轉到百度API接口首頁
點擊控制台進行登錄注冊。
選擇服務,創建應用
選擇需要包名的朋友看過來 >>>>> https://blog.csdn.net/pyfysf/article/details/86438769
這個AK和SK是需要在代碼中使用到的
配置SDK,查看文檔調用接口。
點擊這里進入API文檔;
博主使用的是Android平台的SDK。
根據步驟進行SDK工程配置。
配置完工程之后博主就很驚喜的去調用方法進行寫代碼了。但是,logcat總是報錯。說獲取token失敗,packname錯誤或者AK和SK錯誤。
這里我就很是納悶。我根本沒有設置項目的包名,並且我的AK和SK是正確的。大家有知道解決方法,求大神在評論區指教博主。博主在這里叩謝。
然后經過我查詢資料,我選擇請求API,從而不去調用百度封裝的方法。
查閱API文檔。
實現代碼片段(不提供xml布局文件)
下面將貼一些代碼片段。
博主是打開相機拍一張照片進行掃描實現OCR識別文字。百度的API可以接受本地圖片的路徑,或者網絡上的圖片URL也可以進行OCR文字掃描。
我用到了百度提供的UI,在SDK里面導入到項目里面就可以了。
拍照之后獲取照片的保存路徑。
核心代碼在這里!!
請求百度文字識別API,進行圖片OCR識別。我用的是xutils3.0請求的網絡。可以使用HTTPConnection發起get請求。
/** * 請求百度API接口,進行獲取數據 * * @param filePath */ private void checkData(String filePath) { try { //把圖片文件轉換為字節數組 byte[] imgData = FileUtil.readFileByBytes(filePath); //對字節數組進行Base64編碼 String imgStr = Base64Util.encode(imgData); final String params = URLEncoder.encode("image", "UTF-8") + "=" + URLEncoder.encode(imgStr, "UTF-8"); RequestParams entiry = new RequestParams(ConstantValue.BAIDU_TOKEN_URL); x.http().get(entiry, new Callback.CommonCallback<String>() { @Override public void onSuccess(final String result) { Gson gson = new Gson(); TokenInfo tokenInfo = gson.fromJson(result, TokenInfo.class); final String access_token = tokenInfo.getAccess_token(); new Thread() { public void run() { // public static final String BAIDU_TOKEN_URL = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=" + 你在百度控制台創建的AK+ "&client_secret=" + 你在百度控制台創建的SK; String resultStr = HttpUtil.post(ConstantValue.BAIDU_INTER_URL, access_token, params); Log.e("MainActivity", "MainActivity onSuccess()" + resultStr); Message msg = Message.obtain(); msg.obj = resultStr; msg.what = PRESER_IMG_OK; handler.sendMessage(msg); } }.start(); } @Override public void onError(Throwable ex, boolean isOnCallback) { } @Override public void onCancelled(CancelledException cex) { } @Override public void onFinished() { } }); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
解析數據,官方返回的是一個json串。所以我們進行解析數據
FileUtil和HttpUtils
public static File getSaveFile(Context context) { File file = new File(context.getFilesDir(), "pic.jpg"); return file; } /** * 根據文件路徑讀取byte[] 數組 */ 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(); } } } } /** * http 工具類 */ public class HttpUtil { public static String post(String requestUrl, String accessToken, String params) { try { String generalUrl = requestUrl + "?access_token=" + accessToken; URL url = new URL(generalUrl); // 打開和URL之間的連接 HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); // 設置通用的請求屬性 connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); connection.setRequestProperty("Connection", "Keep-Alive"); connection.setUseCaches(false); connection.setDoOutput(true); connection.setDoInput(true); // 得到請求的輸出流對象 DataOutputStream out = new DataOutputStream(connection.getOutputStream()); out.writeBytes(params); out.flush(); out.close(); // 建立實際的連接 connection.connect(); // 獲取所有響應頭字段 Map<String, List<String>> headers = connection.getHeaderFields(); // 遍歷所有的響應頭字段 for (String key : headers.keySet()) { System.out.println(key + "--->" + headers.get(key)); } // 定義 BufferedReader輸入流來讀取URL的響應 BufferedReader in = null; if (requestUrl.contains("nlp")) in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "GBK")); else in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8")); String result = ""; String getLine; while ((getLine = in.readLine()) != null) { result += getLine; } in.close(); System.out.println("result:" + result); return result; }catch (Exception e){ throw new RuntimeException(e); } } }
Base64Util
這樣就可以實現了。
https://download.csdn.net/download/pyfysf/10406761
有問題可以加博主QQ哦。337081267