最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

阿里通用OCR文字識別/圖像識別/圖片識別對接代碼示例(Java篇)

 更新時間:2024年12月24日 10:02:31   作者:Bug制造者——  
這篇文章主要介紹了阿里通用OCR文字識別/圖像識別/圖片識別對接(Java篇)的相關(guān)資料,文中詳細介紹了包括開通服務(wù)、測試圖片、編寫識別代碼、處理識別結(jié)果等步驟,需要的朋友可以參考下

1:進入阿里云開通文字識別

現(xiàn)在1分錢可以購買500次識別

2:這里可以測試 放入自己的圖片URL即可

3:進入 云市場-已購買的服務(wù) 找到剛才自己買的OCR 復(fù)制appcode

4:編寫識別代碼 img我這里是傳入的URL鏈接

/**
     * OCR文字識別
     * @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的要求,定義相對應(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 "";
        }
    }

 這個是識別是會返回多個結(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 工具類

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:測試圖片

7:識別結(jié)果

當(dāng)然 你們可以 吧ret返回集合中的 結(jié)果全部拼接起來 

總結(jié)

到此這篇關(guān)于阿里通用OCR文字識別/圖像識別/圖片識別對接的文章就介紹到這了,更多相關(guān)Java版阿里通用OCR識別內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Mybatis的詳細使用教程

    Mybatis的詳細使用教程

    這篇文章主要介紹了Mybatis的詳細使用教程,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-12-12
  • java獲取各種路徑的基本方法

    java獲取各種路徑的基本方法

    這篇文章主要為大家詳細介紹了java獲取各種路徑的基本方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-10-10
  • maven多模塊打包注意事項詳解

    maven多模塊打包注意事項詳解

    這篇文章主要為大家介紹了maven多模塊打包注意事項詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-07-07
  • java json與map互相轉(zhuǎn)換的示例

    java json與map互相轉(zhuǎn)換的示例

    這篇文章主要介紹了java json與map互相轉(zhuǎn)換的示例,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下
    2020-10-10
  • El表達式使用問題javax.el.ELException:Failed to parse the expression的解決方式

    El表達式使用問題javax.el.ELException:Failed to parse the expression

    今天小編就為大家分享一篇關(guān)于Jsp El表達式使用問題javax.el.ELException:Failed to parse the expression的解決方式,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-12-12
  • Java異常ClassCastException的解決

    Java異常ClassCastException的解決

    這篇文章主要介紹了Java異常ClassCastException的解決,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • java“與”符號寫法與用法

    java“與”符號寫法與用法

    在本篇文章里小編給大家整理的是關(guān)于java“與”符號寫法與用法,對此有需要的朋友們可以學(xué)習(xí)下。
    2020-02-02
  • SpringCloud之@FeignClient()注解的使用方式

    SpringCloud之@FeignClient()注解的使用方式

    這篇文章主要介紹了SpringCloud之@FeignClient()注解的使用方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • maven創(chuàng)建spark項目的pom.xml文件配置demo

    maven創(chuàng)建spark項目的pom.xml文件配置demo

    這篇文章主要為大家介紹了maven創(chuàng)建spark項目的pom.xml文件配置demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-05-05
  • SpringBoot中MyBatis-Plus 查詢時排除某些字段的操作方法

    SpringBoot中MyBatis-Plus 查詢時排除某些字段的操作方法

    這篇文章主要介紹了SpringBoot中MyBatis-Plus 查詢時排除某些字段的操作方法,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-08-08

最新評論

清镇市| 金寨县| 富川| 扶绥县| 宜昌市| 靖边县| 临江市| 津南区| 安泽县| 卢氏县| 贺兰县| 马鞍山市| 精河县| 阳东县| 微博| 茌平县| 宾川县| 呼和浩特市| 邵阳县| 扎赉特旗| 普定县| 封开县| 玛沁县| 怀仁县| 漳州市| 三亚市| 澄江县| 霍城县| 比如县| 金阳县| 兴宁市| 漳州市| 凤城市| 盐城市| 海门市| 五寨县| 东乌珠穆沁旗| 扎兰屯市| 泸水县| 永泰县| 阜平县|