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

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

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

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)文章

  • Mybatis的詳細(xì)使用教程

    Mybatis的詳細(xì)使用教程

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

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

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

    maven多模塊打包注意事項(xiàng)詳解

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

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

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

    El表達(dá)式使用問(wèn)題javax.el.ELException:Failed to parse the expression

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

    Java異常ClassCastException的解決

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

    java“與”符號(hào)寫(xiě)法與用法

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

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

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

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

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

    SpringBoot中MyBatis-Plus 查詢(xún)時(shí)排除某些字段的操作方法

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

最新評(píng)論

洛南县| 林口县| 平潭县| 兰西县| 额济纳旗| 荆门市| 桑植县| 南丰县| 西峡县| 广德县| 温州市| 普安县| 招远市| 全椒县| 翼城县| 舒城县| 横峰县| 龙门县| 廉江市| 玉屏| 牙克石市| 古丈县| 衡水市| 封丘县| 三江| 公安县| 道孚县| 昌吉市| 金寨县| 三明市| 荆州市| 慈溪市| 满洲里市| 南乐县| 乐清市| 新沂市| 贵溪市| 彭山县| 禄丰县| 大同县| 财经|