Home > Back-end >  Springboot API implementation project called baidu picture character recognition
Springboot API implementation project called baidu picture character recognition

Time:03-27

Preparations for the :
Need to get the corresponding project before using API_KEY and SECRET_KEY, these parameters must be used when using the API, is used to generate access_token.
How to obtain these parameters?
http://ai.baidu.com/tech/ocr/general
After landing to create an application



Click the create application, fill in the corresponding information. Click on the application list can get API_KEY and SECRET_KEY


Download the Java SDK, and introducing the jar package project



call
implementation, obtain access_token
 package com. Baidu. Ai. Aip. The auth. 

The import org. Json. JSONObject;

Import the Java. IO. BufferedReader;
Import the Java. IO. InputStreamReader;
The import java.net.HttpURLConnection;
The import java.net.URL;
import java.util.List;
import java.util.Map;

/* *
* access token class
*/
Public class AuthService {

/* *
* get access token
* @ return return example:
* {
* "access_token" : "24.460 da4889caad24cccdb1fea17221975 2592000.1491995545.282335-1234567",
* "expires_in" : 2592000
*}
*/
Public static String getAuth () {
//website to get the API Key update registration for you
String clientId="baidu cloud applications AK";
//website to obtain the Secret Key to update for you register
String clientSecret="baidu cloud applications SK";
Return getAuth (clientId, clientSecret);
}

/* *
* get access token API
* the token has certain validity, the need to manage, when failure to obtain again.
* @ param ak - baidu cloud's official website for the API Key
* @ param sk - baidu cloud's official website to obtain Securet Key
* @ return assess_token example:
"*" 24.460 da4889caad24cccdb1fea17221975 2592000.1491995545.282335-1234567
*/
Public static String getAuth String ak, String (sk) {
//access token address
String authHost="https://aip.baidubce.com/oauth/2.0/token?" ;
String getAccessTokenUrl=authHost
//1. Grant_type for fixed parameter
+ "grant_type=client_credentials"
//2. The official website to get the API Key
+ "& amp; Client_id="+ ak
//3. The website to get the Secret Key of
+ "& amp; The client_secret="+ sk.
Try {
RealUrl URL=new URL (getAccessTokenUrl);
//the connection between the open and URL
HttpURLConnection connection=(HttpURLConnection) realUrl. OpenConnection ();
Connection. SetRequestMethod (" GET ");
connection.connect();
//get all the response header fields
Map The map=connection. GetHeaderFields ();
//to iterate through all the response header fields
For (String key: map. KeySet ()) {
System. Err. Println (key + "-- -- -- & gt;" + map. Get (key));
}
//define BufferedReader the response of the input stream to read the URL
BufferedReader in=new BufferedReader (new InputStreamReader (connection. The getInputStream ()));
The String result="";
The String line;
While ((line=in readLine ())!=null) {
The result +=line;
}
/* *
* returns the sample
*/
System. Err. Println (" result: "+ result);
JSONObject JSONObject=new JSONObject (result);
String access_token=jsonObject.getString("access_token");
Return access_token.
} the catch (Exception e) {
Failed to get token System. Err. Printf ("!" );
E.p rintStackTrace System. (err);
}
return null;
}

}



write into base64 before the picture into urlencode tools
 package com. Baidu. Ai. Aip. The auth. 

The import sun. Misc. BASE64Encoder;

Import the Java. IO. FileInputStream;
import java.io.IOException;
Import the Java. IO. InputStream;
The import java.net.URLEncoder;

/* *
* image into base64 UrlEncode again after the results
*/

Public class BaseImg64 {
/* *
* local photo into Base64 string
*/
Public static String getImageStrFromPath (String imgPath) {
InputStream in;
Byte [] data=https://bbs.csdn.net/topics/null;
//read image byte array
Try {
In=new FileInputStream (imgPath);
Data=https://bbs.csdn.net/topics/new byte [in the available ()];
In the read (data);
In the close ();
} the catch (IOException e) {
e.printStackTrace();
}
//to byte array Base64 encoding
BASE64Encoder encoder=new BASE64Encoder ();
//return Base64 encoded and then URLEncode the byte array of string
Return URLEncoder. Encode (encoder. Encode (data));
}
}


write calls baidu API interface method, to obtain recognition results
 
Package com. Baidu. Ai. Aip. Auth.

The import org. Apache. HTTP. HttpResponse;
The import org. Apache. HTTP. Client. HttpClient;
The import org. Apache. HTTP. Client. The methods. The HttpPost;
The import org. Apache. HTTP. Entity. StringEntity;
The import org. Apache. HTTP. Impl. Client. DefaultHttpClient;
The import org. Apache. HTTP. Util. EntityUtils;

Import the Java. IO. The File;
import java.io.IOException;
The import java.net.URI;
The import java.net.URISyntaxException;

/* *
* image character recognition
*/

Public class Check {
Private static final String POST_URL="HTTP://https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic? Access_token="+ AuthService getAuth ();

/* *
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related