百度图片文字识别
方法名:queryInfo
@Test
public void selectAllImg() throws Exception{
//读取txt中 图片的路径
File file = new File("28.txt");
String encoding = "UTF-8";
InputStreamReader read = new InputStreamReader(
new FileInputStream(file), encoding);//考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = bufferedReader.readLine();
List<String> result = Arrays.asList(lineTxt.split(","));
List<String> strings1 = new ArrayList<>();
for (int i = 3000; i < 3999; i++) {
List<String> strings = new ArrayList<>();
TLicenseVo t = tShopCheckInfoService.queryInfo(result.get(i));
if (StringUtils.isNotEmpty(t.getCompanyName())) {
strings.add(t.getCompanyName());
strings.add(result.get(i));
String aa = StringUtils.join(strings, "内集合");
strings1.add(aa);
System.out.println(strings1.toString().substring(5));
}
}
//将拼接的有效数据写入本地
writeTxt("3.txt", StringUtils.join(strings1,"外集合"));
}
//自定义一个对象接收参数 TLicenseVo
public class BaiDuAiUtil {
public static final String APP_ID = "123";
public static final String API_KEY = "123";
public static final String SECRET_KEY = "123";
private static AipOcr apiOcr;
private static String filePath = "/Users/";
static {
apiOcr = new AipOcr(APP_ID, API_KEY, SECRET_KEY);
}
public static TLicenseVo parseLicence(String path) {
// path = chanPath(path);
TLicenseVo vo = new TLicenseVo();
JSONObject res2 = apiOcr.basicGeneral(path, new HashMap<String, String>());
try {
JSONArray res = (org.json.JSONArray) res2.get("words_result");
Map<String,Object> result = new HashMap<>();
// StringBuffer buffer = new StringBuffer();
if (res.length() > 0) {
// buffer.append("开始输出照片");
String aa =res.toString();
System.out.print(aa);
vo.setCompanyName(aa);
// for (int key = 0; key < res.length(); key++) {
// result.put("code","0");
// JSONObject object = (JSONObject) res.get(key);
// String value = object.getString("words");
// buffer.append(object.toString());
// if(value.startsWith("法定代表人")){
// vo.setName(value.substring(5,value.length()));
// }
// else if(value.trim().startsWith("公司名称")){
// vo.setCompanyName(value.substring(4,value.length()));
// }
// else if(value.trim().startsWith("名称")){
// vo.setCompanyName(value.substring(2,value.length()));
// }else if(value.trim().startsWith("称")){
// vo.setCompanyName(value.substring(1,value.length()));
// }
// else if(value.trim().startsWith("地址")){
// vo.setAddr(value.substring(2,value.length()));
// }
// else if(value.trim().startsWith("址")){
// vo.setAddr(value.substring(1,value.length()));
// }
else if(value.startsWith("统一社会")){
vo.setCreditCode(value.substring(8,value.length()));
}
else if(value.startsWith("社会信用代码")){
vo.setCreditCode(value.substring(6,value.length()));
}
// else if(value.startsWith("经营者")){
// vo.setManager(value.substring(3,value.length()));
// }
// }
// System.out.print(buffer.toString());
}
} catch (JSONException e) {
e.printStackTrace();
}
// File file = new File(path);
// try {
// if (file.exists()) {
// file.delete();
// }
// } catch (Exception e) {
// System.out.println("删除文件"+path+"出错!");
// e.printStackTrace();
// }
return vo;
}
private static String chanPath(String path){
URL url = null;
String toPatch = filePath + "wx" + path.substring(path.lastIndexOf("/"));
// String toPatch = "D:/wx"+ path.substring(path.lastIndexOf("/"));
try {
url = new URL(path);
File toPic = new File(toPatch) ;
Thumbnails.of(url).size(2000,2000).outputQuality(0.25f).toFile(toPic);
} catch (Exception e) {
e.printStackTrace();
}
return toPatch ;
}
//判断上传的是不是营业执照
public static boolean checkLicence(String path,String company,String name,String credit){
boolean flag = false;
TLicenseVo licence = parseLicence(path);
if(path == null || company == null || name == null || credit == null){
return false;
}
// if(company.equals(licence.getCompany())&&name.equals(licence.getName())&&credit.equals(licence.getCreditNo())){
// flag = true;
// }
return flag;
}
public static void main(String[] args) {
String path = "/6/image.jpg" ;
System.out.println(path.substring(path.lastIndexOf("/")));
}
}