package com.utils;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONObject;
import com.baidu.aip.ocr.AipOcr;
import com.baidu.aip.imageclassify.AipImageClassify;
/**
* 类说明 :
*/
public class BaiduUtil {
/**
* 根据经纬度获得省市区信息
* @param lon 纬度
* @param lat 经度
* @param coordtype 经纬度坐标系
* @return
*/
public static Map<String, String> getCityByLonLat(String key, String lng, String lat) {
String location = lat + "," + lng;
try {
//拼装url
String url = "http://api.map.baidu.com/reverse_geocoding/v3/?ak="+key+"&output=json&coordtype=wgs84ll&location="+location;
String result = HttpClientUtils.doGet(url);
JSONObject o = new JSONObject(result);
Map<String, String> area = new HashMap<>();
area.put("province", o.getJSONObject("result").getJSONObject("addressComponent").getString("province"));
area.put("city", o.getJSONObject("result").getJSONObject("addressComponent").getString("city"));
area.put("district", o.getJSONObject("result").getJSONObject("addressComponent").getString("district"));
area.put("street", o.getJSONObject("result").getJSONObject("addressComponent").getString("street"));
return area;
}catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 获取API访问token
* 该token有一定的有效期,需要自行管理,当失效时需重新获取.
* @param ak - 百度云官网获取的 API Key
* @param sk - 百度云官网获取的 Securet Key
* @return assess_token
*/
public static String getAuth(String ak, String sk) {
// 获取token地址
String authHost = "https://aip.baidubce.com/oauth/2.0/token?";
String getAccessTokenUrl = authHost
// 1. grant_type为固定参数
+ "grant_type=client_credentials"
// 2. 官网获取的 API Key
+ "&client_id=" + ak
// 3. 官网获取的 Secret Key
+ "&client_secret=" + sk;
try {
URL realUrl = new URL(getAccessTokenUrl);
// 打开和URL之间的连接
HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();
connection.setRequestMethod("GET");
connection.connect();
// 获取所有响应头字段
Map<String, List<String>> map = connection.getHeaderFields();
// 遍历所有的响应头字段
for (String key : map.keySet()) {
System.err.println(key + "--->" + map.get(key));
}
// 定义 BufferedReader输入流来读取URL的响应
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String result = "";
String line;
while ((line = in.readLine()) != null) {
result += line;
}
/**
* 返回结果示例
*/
System.err.println("result:" + result);
org.json.JSONObject jsonObject = new org.json.JSONObject(result);
String access_token = jsonObject.getString("access_token");
return access_token;
} catch (Exception e) {
System.err.printf("获取token失败!");
e.printStackTrace(System.err);
}
return null;
}
//设置APPID/AK/SK
public static final String APP_ID = "29917330";
public static final String API_KEY = "yMnHy1guHZRzGhXB7BILdktB";
public static final String SECRET_KEY = "1oHG8X0yizyZmIwj3bZygG470b648iE1";
private static AipOcr ocrClient = null;
/**
* 识别图片上的文本内容,转成文字字符串返回
* @param imagePath 图片文件的路径
*/
public static String generalString(String imagePath, boolean isNewline){
try{
HashMap<String, String> options = new HashMap<String, String>();
options.put("language_type", "CHN_ENG"); //CHN_ENG:中英文混合, ENG:英文
options.put("detect_direction", "true"); //是否检测图像朝向,默认不检测,即:false
options.put("detect_language", "true"); //是否检测语言,默认不检测。
options.put("probability", "false"); //是否返回识别结果中每一行的置信度
//通用文字识别
if(ocrClient==null) {
ocrClient = new AipOcr(APP_ID, API_KEY, SECRET_KEY);
ocrClient.setConnectionTimeoutInMillis(5000);
ocrClient.setSocketTimeoutInMillis(60000);
}
JSONObject jsonObject = ocrClient.basicAccurateGeneral(imagePath, options);
String result = mergeString(jsonObject, isNewline);
return result;
}catch(Exception ex){
ex.printStackTrace();
}
return null;
}
private static String mergeString(JSONObject jsonObject, boolean isNewline){
if(jsonObject == null){
return "";
}
if(jsonObject.has("words_result") && jsonObject.has("words_result_num")){
int wordsResultNum = jsonObject.getInt("words_result_num");
if(wordsResultNum > 0){
StringBuilder sb = new StringBuilder();
JSONArray jsonArray = jsonObject.getJSONArray("words_result");
int len = jsonArray.length();
for(int i=0;i<len;i++) {
JSONObject obj = (JSONObject)jsonArray.get(i);
if(isNewline){
sb.append(obj.get("words") + "\n");
}else{
sb.append(obj.get("words"));
}
}
return sb.toString();
}
}else{
return jsonObject.toString();
}
return null;
}
public static JSONObject animalDetect(String imgPath) {
//初始化
AipImageClassify aic = new AipImageClassify(APP_ID, API_KEY, SECRET_KEY);
//返回JSON格式的数据
HashMap<String, String> params = new HashMap<String, String>();
params.put("baike_num", "1");
JSONObject res = aic.animalDetect(imgPath, params);
System.out.println(res.toString(2));
return res;
}
public static JSONObject dishDetect(String imgPath) {
//初始化
AipImageClassify aic = new AipImageClassify(APP_ID, API_KEY, SECRET_KEY);
//返回JSON格式的数据
HashMap<String, String> params = new HashMap<String, String>();
params.put("baike_num", "1");
JSONObject res = aic.dishDetect(imgPath, params);
System.out.println(res.toString(2));
return res;
}
public static JSONObject plantDetect(String imgPath) {
//初始化
AipImageClassify aic = new AipImageClassify(APP_ID, API_KEY, SECRET_KEY);
//返回JSON格式的数据
HashMap<String, String> params = new HashMap<String, String>();
params.put("baike_num", "1");
JSONObject res = aic.plantDetect(imgPath, params);
System.out.println(res.toString(2));
return res;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
基于springboot的企业固定资产管理系统(源码+数据库).zip (548个子文件)
update-password.vue.bak 3KB
main.js.bak 2KB
IndexMain.vue.bak 2KB
IndexAsideStatic.vue.bak 2KB
BreadCrumbs.vue.bak 2KB
IndexHeader.vue.bak 2KB
3-build.bat 15B
2-run.bat 14B
1-install.bat 12B
YonghuController.class 8KB
CommonController.class 8KB
BaiduUtil.class 7KB
MPUtil.class 7KB
UsersController.class 6KB
ZichanlingyongController.class 6KB
ZichanguihaiController.class 6KB
ZichanleibieController.class 5KB
ZichanxinxiController.class 5KB
FileController.class 5KB
ZichanlingyongEntity.class 5KB
ZichanxinxiEntity.class 5KB
ZichanguihaiEntity.class 5KB
TokenServiceImpl.class 5KB
ConfigController.class 4KB
AuthorizationInterceptor.class 4KB
ZichanlingyongServiceImpl.class 4KB
ZichanleibieServiceImpl.class 4KB
ZichanguihaiServiceImpl.class 4KB
ZichanxinxiServiceImpl.class 4KB
YonghuEntity.class 4KB
YonghuServiceImpl.class 4KB
CommonUtil.class 3KB
Query.class 3KB
UsersServiceImpl.class 3KB
PageUtils.class 3KB
TokenEntity.class 3KB
ZichanlingyongModel.class 3KB
ZichanlingyongVO.class 3KB
ZichanxinxiModel.class 3KB
ZichanguihaiModel.class 3KB
ZichanxinxiVO.class 3KB
ZichanguihaiVO.class 3KB
ZichanleibieEntity.class 2KB
CommonServiceImpl.class 2KB
InterceptorConfig.class 2KB
R.class 2KB
UsersEntity.class 2KB
SpringContextUtils.class 2KB
ValidatorUtils.class 2KB
ZichanlingyongService.class 2KB
ConfigServiceImpl.class 2KB
ZichanleibieService.class 2KB
ZichanguihaiService.class 2KB
ZichanxinxiService.class 2KB
ZichanlingyongDao.class 2KB
YonghuModel.class 2KB
ZichanleibieDao.class 2KB
ZichanguihaiDao.class 2KB
YonghuVO.class 2KB
ZichanxinxiDao.class 2KB
YonghuService.class 2KB
YonghuDao.class 2KB
HttpClientUtils.class 2KB
EIException.class 1KB
JQPageInfo.class 1KB
SpringbootSchemaApplication.class 1KB
ConfigEntity.class 1KB
SQLFilter.class 1KB
TokenService.class 1KB
ZichanlingyongView.class 1KB
ZichanguihaiView.class 1KB
ZichanleibieView.class 1KB
ZichanxinxiView.class 1KB
YonghuView.class 1KB
CommonDao.class 1KB
CommonService.class 1KB
UsersDao.class 969B
TokenDao.class 969B
FileUtil.class 964B
UsersService.class 952B
MybatisPlusConfig.class 669B
ConfigService.class 589B
SpringbootSchemaApplicationTests.class 541B
MD5Util.class 516B
IgnoreAuth.class 428B
ZichanleibieModel.class 402B
APPLoginUser.class 395B
LoginUser.class 389B
ZichanleibieVO.class 387B
AlipayConfig.class 283B
ConfigDao.class 269B
.classpath 2KB
mvnw.cmd 7KB
canvas-bg-1.css 391B
canvas-bg-2.css 83B
canvas-bg-5.css 61B
canvas-bg-4.css 61B
canvas-bg-3.css 61B
.factorypath 15KB
.gitignore 184B
共 548 条
- 1
- 2
- 3
- 4
- 5
- 6
资源评论
等天晴i
- 粉丝: 5496
- 资源: 10万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功