Java tutorial
package com.huawei.ais.demo; import java.io.File; import java.io.IOException; import java.net.URISyntaxException; import java.util.ArrayList; import org.apache.commons.codec.binary.Base64; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.apache.http.Header; import org.apache.http.HttpResponse; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.apache.http.entity.mime.HttpMultipartMode; import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.entity.mime.content.FileBody; import org.apache.http.message.BasicHeader; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.huawei.ais.sdk.util.HttpClientUtils; /** * Token???, ?? * http://support.huaweicloud.com/api-ocr/ocr_03_0005.html */ public class TokenDemo { /** * Token??Token * ???: * http://support.huaweicloud.com/api-ocr/ocr_03_0005.html * * @param username ?? * @param passwd ? * @param domainName ?? * @param projectName ?? * @return JSON */ private static String requestBody(String username, String passwd, String domainName, String projectName) { JSONObject auth = new JSONObject(); JSONObject identity = new JSONObject(); JSONArray methods = new JSONArray(); methods.add("password"); identity.put("methods", methods); JSONObject password = new JSONObject(); JSONObject user = new JSONObject(); user.put("name", username); user.put("password", passwd); JSONObject domain = new JSONObject(); domain.put("name", username); user.put("domain", domain); password.put("user", user); identity.put("password", password); JSONObject scope = new JSONObject(); JSONObject scopeProject = new JSONObject(); scopeProject.put("name", projectName); JSONObject projectDomain = new JSONObject(); projectDomain.put("name", domainName); scopeProject.put("domain", projectDomain); scope.put("project", scopeProject); auth.put("identity", identity); auth.put("scope", scope); JSONObject params = new JSONObject(); params.put("auth", auth); return params.toJSONString(); } /** * ?Token? ??HTTPHeader???Token * ???: X-Subject-Token * * @param username ?? * @param password ? * @param regionName ???? * @return ?Token * @throws URISyntaxException * @throws UnsupportedOperationException * @throws IOException */ private static String getToken(String username, String password, String regionName) throws URISyntaxException, UnsupportedOperationException, IOException { // 1.?Token?? String requestBody = requestBody(username, password, username, regionName); String url = "https://iam.cn-north-1.myhuaweicloud.com/v3/auth/tokens"; Header[] headers = new Header[] { new BasicHeader("Content-Type", ContentType.APPLICATION_JSON.toString()) }; StringEntity stringEntity = new StringEntity(requestBody, "utf-8"); // 2.IAM??, POST??Token value HttpResponse response = HttpClientUtils.post(url, headers, stringEntity); Header[] xst = response.getHeaders("X-Subject-Token"); return xst[0].getValue(); } /** * Base64???Token??? * @param token token? * @param formFile * @throws IOException */ public static void requestOcrCustomsFormEnBase64(String token, String formFile) { // 1.????? String url = "https://ais.cn-north-1.myhuaweicloud.com/v1.0/ocr/action/ocr_form"; Header[] headers = new Header[] { new BasicHeader("X-Auth-Token", token) }; try { MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create(); multipartEntityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); FileBody fileBody = new FileBody(new File(formFile), ContentType.create("image/jpeg", "utf-8")); multipartEntityBuilder.addPart("file", fileBody); // 2.????, POST?? HttpResponse response = HttpClientUtils.post(url, headers, multipartEntityBuilder.build()); System.out.println(response); String content = IOUtils.toString(response.getEntity().getContent()); System.out.println(content); } catch (Exception e) { e.printStackTrace(); } } /** * ?Base64???Token??? * @param token token? * @param formFile * @throws IOException */ public static void requestOcrVatInvoiceBase64(String token, String formFile) { // 1.???? String url = "https://ais.cn-north-1.myhuaweicloud.com/v1.0/ocr/vat-invoice"; Header[] headers = new Header[] { new BasicHeader("X-Auth-Token", token), new BasicHeader("Content-Type", ContentType.APPLICATION_JSON.toString()) }; try { byte[] fileData = FileUtils.readFileToByteArray(new File(formFile)); String fileBase64Str = Base64.encodeBase64String(fileData); JSONObject json = new JSONObject(); json.put("image", fileBase64Str); StringEntity stringEntity = new StringEntity(json.toJSONString(), "utf-8"); // 2.???, POST?? HttpResponse response = HttpClientUtils.post(url, headers, stringEntity); System.out.println(response); String content = IOUtils.toString(response.getEntity().getContent()); System.out.println(content); } catch (Exception e) { e.printStackTrace(); } } /** * ?Base64???Token??? * @param token token? * @param formFile * @throws IOException */ public static void requestOcrIDCardBase64(String token, String formFile) { // 1.???? String url = "https://ais.cn-north-1.myhuaweicloud.com/v1.0/ocr/id-card"; Header[] headers = new Header[] { new BasicHeader("X-Auth-Token", token), new BasicHeader("Content-Type", ContentType.APPLICATION_JSON.toString()) }; try { byte[] fileData = FileUtils.readFileToByteArray(new File(formFile)); String fileBase64Str = Base64.encodeBase64String(fileData); JSONObject json = new JSONObject(); json.put("image", fileBase64Str); // // ????????? // "side"?"front", "back"""?""?? // json.put("side", "front"); StringEntity stringEntity = new StringEntity(json.toJSONString(), "utf-8"); // 2.???, POST?? HttpResponse response = HttpClientUtils.post(url, headers, stringEntity); System.out.println(response); String content = IOUtils.toString(response.getEntity().getContent()); System.out.println(content); } catch (Exception e) { e.printStackTrace(); } } /** * ?Base64???Token??? * @param token token? * @param formFile * @throws IOException */ public static void requestOcrDriverLicenseBase64(String token, String formFile) { // 1.???? String url = "https://ais.cn-north-1.myhuaweicloud.com/v1.0/ocr/driver-license"; Header[] headers = new Header[] { new BasicHeader("X-Auth-Token", token), new BasicHeader("Content-Type", ContentType.APPLICATION_JSON.toString()) }; try { byte[] fileData = FileUtils.readFileToByteArray(new File(formFile)); String fileBase64Str = Base64.encodeBase64String(fileData); JSONObject json = new JSONObject(); json.put("image", fileBase64Str); StringEntity stringEntity = new StringEntity(json.toJSONString(), "utf-8"); // 2.???, POST?? HttpResponse response = HttpClientUtils.post(url, headers, stringEntity); System.out.println(response); String content = IOUtils.toString(response.getEntity().getContent()); System.out.println(content); } catch (Exception e) { e.printStackTrace(); } } /** * ?Base64???Token??? * @param token token? * @param formFile * @throws IOException */ public static void requestOcrVehicleLicenseBase64(String token, String formFile) { // 1.???? String url = "https://ais.cn-north-1.myhuaweicloud.com/v1.0/ocr/vehicle-license"; Header[] headers = new Header[] { new BasicHeader("X-Auth-Token", token), new BasicHeader("Content-Type", ContentType.APPLICATION_JSON.toString()) }; try { byte[] fileData = FileUtils.readFileToByteArray(new File(formFile)); String fileBase64Str = Base64.encodeBase64String(fileData); JSONObject json = new JSONObject(); json.put("image", fileBase64Str); StringEntity stringEntity = new StringEntity(json.toJSONString(), "utf-8"); // 2.???, POST?? HttpResponse response = HttpClientUtils.post(url, headers, stringEntity); System.out.println(response); String content = IOUtils.toString(response.getEntity().getContent()); System.out.println(content); } catch (Exception e) { e.printStackTrace(); } } /** * Base64???Token??? * @param token token? * @param formFile * @throws IOException */ public static void requestOcrGeneralTableBase64(String token, String formFile) { // 1.??? String url = "https://ais.cn-north-1.myhuaweicloud.com/v1.0/ocr/general-table"; Header[] headers = new Header[] { new BasicHeader("X-Auth-Token", token), new BasicHeader("Content-Type", ContentType.APPLICATION_JSON.toString()) }; try { byte[] fileData = FileUtils.readFileToByteArray(new File(formFile)); String fileBase64Str = Base64.encodeBase64String(fileData); JSONObject json = new JSONObject(); json.put("image", fileBase64Str); StringEntity stringEntity = new StringEntity(json.toJSONString(), "utf-8"); // 2.??, POST?? HttpResponse response = HttpClientUtils.post(url, headers, stringEntity); System.out.println(response); String content = IOUtils.toString(response.getEntity().getContent()); System.out.println(content); } catch (Exception e) { e.printStackTrace(); } } /** * Base64???Token??? * @param token token? * @param formFile * @throws IOException */ public static void requestOcrHandwritingBase64(String token, String formFile) { // 1.??? String url = "https://ais.cn-north-1.myhuaweicloud.com/v1.0/ocr/handwriting"; Header[] headers = new Header[] { new BasicHeader("X-Auth-Token", token), new BasicHeader("Content-Type", ContentType.APPLICATION_JSON.toString()) }; try { byte[] fileData = FileUtils.readFileToByteArray(new File(formFile)); String fileBase64Str = Base64.encodeBase64String(fileData); JSONObject json = new JSONObject(); json.put("image", fileBase64Str); // // 1.a ???????? // detect_direction?:true, false // json.put("detect_direction", true); // // 1.b ???????? ??? // text_type?:"digit", "upper_letter" ?? // ArrayList<String> textType = new ArrayList<String>(); textType.add("digit"); textType.add("upper_letter"); json.put("text_type", textType); StringEntity stringEntity = new StringEntity(json.toJSONString(), "utf-8"); // 2.??, POST?? HttpResponse response = HttpClientUtils.post(url, headers, stringEntity); System.out.println(response); String content = IOUtils.toString(response.getEntity().getContent()); System.out.println(content); } catch (Exception e) { e.printStackTrace(); } } /** * ?Base64???Token??? * @param token token? * @param formFile * @throws IOException */ public static void requestOcrMvsInvoiceBase64(String token, String formFile) { // 1.???? String url = "https://ais.cn-north-1.myhuaweicloud.com/v1.0/ocr/mvs-invoice"; Header[] headers = new Header[] { new BasicHeader("X-Auth-Token", token), new BasicHeader("Content-Type", ContentType.APPLICATION_JSON.toString()) }; try { byte[] fileData = FileUtils.readFileToByteArray(new File(formFile)); String fileBase64Str = Base64.encodeBase64String(fileData); JSONObject json = new JSONObject(); json.put("image", fileBase64Str); StringEntity stringEntity = new StringEntity(json.toJSONString(), "utf-8"); // 2.???, POST?? HttpResponse response = HttpClientUtils.post(url, headers, stringEntity); System.out.println(response); String content = IOUtils.toString(response.getEntity().getContent()); System.out.println(content); } catch (Exception e) { e.printStackTrace(); } } /** * Base64???Token??? * @param token token? * @param formFile * @throws IOException */ public static void requestOcrGeneralTextBase64(String token, String formFile) { // 1.??? String url = "https://ais.cn-north-1.myhuaweicloud.com/v1.0/ocr/general-text"; Header[] headers = new Header[] { new BasicHeader("X-Auth-Token", token), new BasicHeader("Content-Type", ContentType.APPLICATION_JSON.toString()) }; try { byte[] fileData = FileUtils.readFileToByteArray(new File(formFile)); String fileBase64Str = Base64.encodeBase64String(fileData); JSONObject json = new JSONObject(); json.put("image", fileBase64Str); // // 1.a ???????? // detect_direction?:true, false // json.put("detect_direction", false); StringEntity stringEntity = new StringEntity(json.toJSONString(), "utf-8"); // 2.??, POST?? HttpResponse response = HttpClientUtils.post(url, headers, stringEntity); System.out.println(response); String content = IOUtils.toString(response.getEntity().getContent()); System.out.println(content); } catch (Exception e) { e.printStackTrace(); } } /** * ? */ public static void main(String[] args) throws URISyntaxException, UnsupportedOperationException, IOException { String username = "n6cc9c0a531613917943"; // ?? String password = "Lpking12#"; // ??? String regionName = "cn-north-1"; // ????: http://developer.huaweicloud.com/endpoint.html String token = getToken(username, password, regionName); System.out.println(token); // ???requestOcrVatInvoiceBase64 //requestOcrVatInvoiceBase64(token, "data/vat-invoice-demo.jpg"); // ????requestOcrCustomsFormEnBase64 //requestOcrCustomsFormEnBase64(token, "data/customs-form-en-demo.jpg"); // ???requestOcrIDCardBase64 //requestOcrIDCardBase64(token, "data/id-card-demo.png"); // ???requestOcrDriverLicenseBase64 //requestOcrDriverLicenseBase64(token, "data/driver-license-demo.png"); // ???requestOcrVehicleLicenseBase64 //requestOcrVehicleLicenseBase64(token, "data/vehicle-license-demo.png"); // ??requestOcrGeneralTableBase64 //requestOcrGeneralTableBase64(token, "data/general-table-demo.png"); // ??requestOcrHandwritingBase64 //requestOcrHandwritingBase64(token, "data/handwriting-demo.jpg"); // ???requestOcrMvsInvoiceBase64 //requestOcrMvsInvoiceBase64(token, "data/mvs-invoice-demo.jpg"); // ??requestOcrGeneralTextBase64 //requestOcrGeneralTextBase64(token, "data/general-text-demo.jpg"); } }