Java id card recognition interface call example

Source: Internet
Author: User
Tags http post json

This java article demonstrates the basic aggregate data identification interface. The basic http post request uploads images and receives JSON data for processing.

You need

Https://www.juhe.cn/docs/api/id/153

Apply for an appkey for business card recognition

1. List of supported document types

Request address: http://api2.juheapi.com/cardrecon/supportlist? Key = the appkey you applied

This interface can be obtained through GET requests. java Network requests include HttpClient SDKs and HttpURLConnection related packages. HttpClient is used here and the pilot package is required, if maven is used, it is easier to directly put:

<Dependency> <groupid> org. apache. httpcomponents </groupid>
<Artifactid> httpmime </artifactid>
<Version> 4.3.6 </version>
</Dependency>
<Dependency>
<Groupid> org. apache. httpcomponents </groupid>
<Artifactid> httpclient </artifactid>
<Version> 4.4.1 </version>
</Dependency>

Copy to configuration file pom. xml

The request code is as follows:

Public static String get () throws IOException {
// Create an HttpClient object
CloseableHttpClient httpClient = HttpClients. createDefault ();
CloseableHttpResponse response = null;
String result = null;
Try {
HttpGet httpGet = new HttpGet ("http://api2.juheapi.com/cardrecon/supportlist? Key = "+ appKey );
// Execute the network request
Response = httpClient.exe cute (httpGet );
// Obtain the Request Entity
HttpEntity resEntity = response. getEntity ();
If (resEntity! = Null ){
// ConverStreamToString is one of the following methods: convert the byte stream of network requests into a UTF-8 string
Result = ConvertStreamToString (resEntity. getContent (), "UTF-8 ");
            }
EntityUtils. consume (resEntity );
} Catch (Exception e ){
} Finally {
// Close the request
Response. close ();
HttpClient. close ();
        }
// The JSON data is parsed by a third party using the JSON jar package.
Return result;
    }

2. Document image recognition

Request address: http://api2.juheapi.com/cardrecon/upload

// This method is the File type that contains the local image information in the parameters uploaded by the POST request.
Public static String post (String type, File file) throws Exception {
CloseableHttpClient httpClient = HttpClients. createDefault ();
CloseableHttpResponse response = null;
String result = null;
// HttpClient request settings. You do not need to configure them. Use the default parameters. Here, the connection and duration are set (in milliseconds)
RequestConfig config = RequestConfig. custom (). setConnectTimeout (30000). setSocketTimeout (30000). build ();
Try {
HttpPost httppost = new HttpPost ("http://api2.juheapi.com/cardrecon/upload ");
// FileBody encapsulate File-type parameters
FileBody bin = new FileBody (file );
// StringBody encapsulate String-type parameters
StringBody keyBody = new StringBody (key, ContentType. TEXT_PLAIN );
StringBody typeBody = new StringBody (type, ContentType. TEXT_PLAIN );
// Add part to pass in the parameter and specify the parameter name
HttpEntity reqEntity = MultipartEntityBuilder. create ()
. AddPart ("pic", bin). addPart ("key", keyBody)
. AddPart ("cardType", typeBody). build ();
Httppost. setEntity (reqEntity );
Httppost. setConfig (config );
// Execute the network request and return the result
Response = httpClient.exe cute (httppost );
HttpEntity resEntity = response. getEntity ();
If (resEntity! = Null ){
Result = ConvertStreamToString (resEntity. getContent (), "UTF-8 ");
            }
EntityUtils. consume (resEntity );
} Finally {
Response. close ();
HttpClient. close ();
        }
// The JSON data is parsed by a third party using the JSON jar package.
Return result;
    }

// This method converts the transmitted bytes into corresponding strings and returns them. This method is generally used in network requests.
Public static String ConvertStreamToString (InputStream is, String charset)
Throws Exception {
StringBuilder sb = new StringBuilder ();
Try (InputStreamReader inputStreamReader = new InputStreamReader (is, charset )){
Try (BufferedReader reader = new BufferedReader (inputStreamReader )){
String line = null;
While (line = reader. readLine ())! = Null ){
Sb. append (line). append ("\ r \ n ");
                }
            }
        }
Return sb. toString ();
    }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.