阿里通用OCR文字識(shí)別/圖像識(shí)別/圖片識(shí)別對(duì)接代碼示例(Java篇)
1:進(jìn)入阿里云開(kāi)通文字識(shí)別
現(xiàn)在1分錢(qián)可以購(gòu)買(mǎi)500次識(shí)別
2:這里可以測(cè)試 放入自己的圖片URL即可
3:進(jìn)入 云市場(chǎng)-已購(gòu)買(mǎi)的服務(wù) 找到剛才自己買(mǎi)的OCR 復(fù)制appcode
4:編寫(xiě)識(shí)別代碼 img我這里是傳入的URL鏈接
/** * OCR文字識(shí)別 * @param img 圖片地址 */ public static String aliyunAnalysisPic(String img){ String host = "https://tysbgpu.market.alicloudapi.com"; String path = "/api/predict/ocr_general"; String method = "POST"; //自己的appCode String appcode = "自己上面復(fù)制的appcode"; Map<String, String> headers = new HashMap<String, String>(); //最后在header中的格式(中間是英文空格)為Authorization:APPCODE **************** headers.put("Authorization", "APPCODE " + appcode); //根據(jù)API的要求,定義相對(duì)應(yīng)的Content-Type headers.put("Content-Type", "application/json; charset=UTF-8"); Map<String, String> querys = new HashMap<String, String>(); // String bodys = "{\"img\":\"\",\"url\":\""+img+"\",\"prob\":false,\"charInfo\":false,\"rotate\":false,\"table\":false}"; String bodys = "{\"image\":\""+img+"\",\"configure\":{\"output_prob\":true,\"output_keypoints\":false,\"skip_detection\":false,\"without_predicting_direction\":false,\"dir_assure\":false}}"; try { HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys); System.out.println(response.toString()); //獲取response的body String entity = EntityUtils.toString(response.getEntity()); JSONObject jsonObject = new JSONObject(entity); String word = extractMaxProbWord(jsonObject); // String content = jsonObject.get("word").toString(); System.out.println(word); return word; } catch (Exception e) { e.printStackTrace(); return ""; } }
這個(gè)是識(shí)別是會(huì)返回多個(gè)結(jié)果 判斷prob精度值最大的結(jié)果 返回
public static String extractMaxProbWord(JSONObject jsonResponse) throws Exception { // JSONObject jsonObject = new JSONObject(jsonResponse); JSONArray retArray = jsonResponse.getJSONArray("ret"); double maxProb = -1; String maxProbWord = ""; for (int i = 0; i < retArray.length(); i++) { JSONObject obj = retArray.getJSONObject(i); double prob = obj.getDouble("prob"); String word = obj.getString("word"); if (prob > maxProb) { maxProb = prob; maxProbWord = word; } } return maxProbWord; }
5:HttpUtils 工具類(lèi)
package edu.controllereTest.teacherTest; import org.apache.commons.lang3.StringUtils; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpDelete; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPut; import org.apache.http.conn.ClientConnectionManager; import org.apache.http.conn.scheme.Scheme; import org.apache.http.conn.scheme.SchemeRegistry; import org.apache.http.conn.ssl.SSLSocketFactory; import org.apache.http.entity.ByteArrayEntity; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; import java.security.cert.X509Certificate; import java.util.ArrayList; import java.util.List; import java.util.Map; public class HttpUtils { /** * get * * @param host * @param path * @param method * @param headers * @param querys * @return * @throws Exception */ public static HttpResponse doGet(String host, String path, String method, Map<String, String> headers, Map<String, String> querys) throws Exception { HttpClient httpClient = wrapClient(host); HttpGet request = new HttpGet(buildUrl(host, path, querys)); for (Map.Entry<String, String> e : headers.entrySet()) { request.addHeader(e.getKey(), e.getValue()); } return httpClient.execute(request); } /** * post form * * @param host * @param path * @param method * @param headers * @param querys * @param bodys * @return * @throws Exception */ public static HttpResponse doPost(String host, String path, String method, Map<String, String> headers, Map<String, String> querys, Map<String, String> bodys) throws Exception { HttpClient httpClient = wrapClient(host); HttpPost request = new HttpPost(buildUrl(host, path, querys)); for (Map.Entry<String, String> e : headers.entrySet()) { request.addHeader(e.getKey(), e.getValue()); } if (bodys != null) { List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>(); for (String key : bodys.keySet()) { nameValuePairList.add(new BasicNameValuePair(key, bodys.get(key))); } UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nameValuePairList, "utf-8"); formEntity.setContentType("application/x-www-form-urlencoded; charset=UTF-8"); request.setEntity(formEntity); } return httpClient.execute(request); } /** * Post String * * @param host * @param path * @param method * @param headers * @param querys * @param body * @return * @throws Exception */ public static HttpResponse doPost(String host, String path, String method, Map<String, String> headers, Map<String, String> querys, String body) throws Exception { HttpClient httpClient = wrapClient(host); HttpPost request = new HttpPost(buildUrl(host, path, querys)); for (Map.Entry<String, String> e : headers.entrySet()) { request.addHeader(e.getKey(), e.getValue()); } if (StringUtils.isNotBlank(body)) { request.setEntity(new StringEntity(body, "utf-8")); } return httpClient.execute(request); } /** * Post stream * * @param host * @param path * @param method * @param headers * @param querys * @param body * @return * @throws Exception */ public static HttpResponse doPost(String host, String path, String method, Map<String, String> headers, Map<String, String> querys, byte[] body) throws Exception { HttpClient httpClient = wrapClient(host); HttpPost request = new HttpPost(buildUrl(host, path, querys)); for (Map.Entry<String, String> e : headers.entrySet()) { request.addHeader(e.getKey(), e.getValue()); } if (body != null) { request.setEntity(new ByteArrayEntity(body)); } return httpClient.execute(request); } /** * Put String * @param host * @param path * @param method * @param headers * @param querys * @param body * @return * @throws Exception */ public static HttpResponse doPut(String host, String path, String method, Map<String, String> headers, Map<String, String> querys, String body) throws Exception { HttpClient httpClient = wrapClient(host); HttpPut request = new HttpPut(buildUrl(host, path, querys)); for (Map.Entry<String, String> e : headers.entrySet()) { request.addHeader(e.getKey(), e.getValue()); } if (StringUtils.isNotBlank(body)) { request.setEntity(new StringEntity(body, "utf-8")); } return httpClient.execute(request); } /** * Put stream * @param host * @param path * @param method * @param headers * @param querys * @param body * @return * @throws Exception */ public static HttpResponse doPut(String host, String path, String method, Map<String, String> headers, Map<String, String> querys, byte[] body) throws Exception { HttpClient httpClient = wrapClient(host); HttpPut request = new HttpPut(buildUrl(host, path, querys)); for (Map.Entry<String, String> e : headers.entrySet()) { request.addHeader(e.getKey(), e.getValue()); } if (body != null) { request.setEntity(new ByteArrayEntity(body)); } return httpClient.execute(request); } /** * Delete * * @param host * @param path * @param method * @param headers * @param querys * @return * @throws Exception */ public static HttpResponse doDelete(String host, String path, String method, Map<String, String> headers, Map<String, String> querys) throws Exception { HttpClient httpClient = wrapClient(host); HttpDelete request = new HttpDelete(buildUrl(host, path, querys)); for (Map.Entry<String, String> e : headers.entrySet()) { request.addHeader(e.getKey(), e.getValue()); } return httpClient.execute(request); } private static String buildUrl(String host, String path, Map<String, String> querys) throws UnsupportedEncodingException { StringBuilder sbUrl = new StringBuilder(); sbUrl.append(host); if (!StringUtils.isBlank(path)) { sbUrl.append(path); } if (null != querys) { StringBuilder sbQuery = new StringBuilder(); for (Map.Entry<String, String> query : querys.entrySet()) { if (0 < sbQuery.length()) { sbQuery.append("&"); } if (StringUtils.isBlank(query.getKey()) && !StringUtils.isBlank(query.getValue())) { sbQuery.append(query.getValue()); } if (!StringUtils.isBlank(query.getKey())) { sbQuery.append(query.getKey()); if (!StringUtils.isBlank(query.getValue())) { sbQuery.append("="); sbQuery.append(URLEncoder.encode(query.getValue(), "utf-8")); } } } if (0 < sbQuery.length()) { sbUrl.append("?").append(sbQuery); } } return sbUrl.toString(); } private static HttpClient wrapClient(String host) { HttpClient httpClient = new DefaultHttpClient(); if (host.startsWith("https://")) { sslClient(httpClient); } return httpClient; } private static void sslClient(HttpClient httpClient) { try { SSLContext ctx = SSLContext.getInstance("TLS"); X509TrustManager tm = new X509TrustManager() { public X509Certificate[] getAcceptedIssuers() { return null; } public void checkClientTrusted(X509Certificate[] xcs, String str) { } public void checkServerTrusted(X509Certificate[] xcs, String str) { } }; ctx.init(null, new TrustManager[] { tm }, null); SSLSocketFactory ssf = new SSLSocketFactory(ctx); ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); ClientConnectionManager ccm = httpClient.getConnectionManager(); SchemeRegistry registry = ccm.getSchemeRegistry(); registry.register(new Scheme("https", 443, ssf)); } catch (KeyManagementException ex) { throw new RuntimeException(ex); } catch (NoSuchAlgorithmException ex) { throw new RuntimeException(ex); } } }
6:測(cè)試圖片
7:識(shí)別結(jié)果
當(dāng)然 你們可以 吧ret返回集合中的 結(jié)果全部拼接起來(lái)
總結(jié)
到此這篇關(guān)于阿里通用OCR文字識(shí)別/圖像識(shí)別/圖片識(shí)別對(duì)接的文章就介紹到這了,更多相關(guān)Java版阿里通用OCR識(shí)別內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
spring validation多層對(duì)象校驗(yàn)教程
這篇文章主要介紹了spring validation多層對(duì)象校驗(yàn)教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10簡(jiǎn)單了解JAVA中類(lèi)、實(shí)例與Class對(duì)象
這篇文章主要介紹了簡(jiǎn)單了解JAVA中類(lèi)、實(shí)例與Class對(duì)象,類(lèi)是面向?qū)ο缶幊陶Z(yǔ)言的一個(gè)重要概念,它是對(duì)一項(xiàng)事物的抽象概括,可以包含該事物的一些屬性定義,以及操作屬性的方法,需要的朋友可以參考下2019-06-06Java算法之最長(zhǎng)公共子序列問(wèn)題(LCS)實(shí)例分析
這篇文章主要介紹了Java算法之最長(zhǎng)公共子序列問(wèn)題(LCS),結(jié)合實(shí)例形式分析了最長(zhǎng)公共子序列的原理及問(wèn)題解決方法,需要的朋友可以參考下2017-11-11spring @Lazy延遲注入的邏輯實(shí)現(xiàn)
有時(shí)候我們會(huì)在屬性注入的時(shí)候添加@Lazy注解實(shí)現(xiàn)延遲注入,今天咱們通過(guò)閱讀源碼來(lái)分析下原因,感興趣的可以了解一下2021-08-08微信小程序 開(kāi)發(fā)中遇到問(wèn)題總結(jié)
這篇文章主要介紹了微信小程序 開(kāi)發(fā)中遇到問(wèn)題總結(jié)的相關(guān)資料,需要的朋友可以參考下2017-02-02SpringBoot實(shí)現(xiàn)接口的各種參數(shù)校驗(yàn)的示例
本文主要介紹了SpringBoot實(shí)現(xiàn)接口的各種參數(shù)校驗(yàn)的示例,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01Java零基礎(chǔ)教程之Windows下安裝 JDK的方法圖解
這篇文章主要介紹了Java零基礎(chǔ)教程之Windows下安裝 JDK的方法圖解,本文介紹的非常詳細(xì),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09IntelliJ?IDEA?2020.2?全家桶及以下版本激活工具大全【喜訊】
這篇文章主要介紹了IntelliJ?IDEA?2020.2?全家桶及以下版本激活工具大全【喜訊】,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09