package ocr;
import java.io.IOException;
import java.lang.reflect.Modifier;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.net.URLEncoder;
import java.util.HashMap;
import org.json.JSONObject;
import com.baidu.aip.ocr.AipOcr;
import com.baidu.aip.util.Base64Util;
public class OcrTest
{
public static final String APP_ID = "C123456";
public static final String APP_KEY = "";
public static final String SECRET_KEY = "";
public static void main(String[] args)
{
System.setProperty("proxySet", "true");
System.setProperty("http.proxyHost", "10.244");
System.setProperty("http.proxyPort", "3128");
System.setProperty("https.proxyHost", "10.244");
System.setProperty("https.proxyPort", "3128");
BasicAuthenticator auth = new BasicAuthenticator("账号", "密码");
Authenticator.setDefault(auth);
System.out.print(System.getProperty("os.name"));
String filePath = "F:\\Workspace\\springboot\\src\\test\\java\\ocr\\888.jpg";
String access_token = "";
String otherHost = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic";
// String otherHost = "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard";
try
{
byte[] imgData = FileUtil.readFileByBytes(filePath);
String imgStr = Base64Util.encode(imgData);
String prams = URLEncoder.encode("image","UTF-8")+"="+URLEncoder.encode(imgStr,"UTF-8");
String result = HttpUtil.post(otherHost, access_token, prams);
System.out.println("识别文字:"+result);
} catch (Exception e)
{
e.printStackTrace();
}
}
public static String getAuth(String ak,String sk){
String authHost = "https://aip.baidubce.com/oauth/2.0/token?";
String mustParam = "grant_type=client_credentials";
return authHost+mustParam+"&client_id="+ak+"&client_secret="+sk;
}
}
class BasicAuthenticator extends Authenticator {
String userName;
String password;
public BasicAuthenticator(String userName, String password) {
this.userName = userName;
this.password = password;
}
// @Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password.toCharArray());
}
}