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 ();
}