今天做了什么:

完成了调用baiduapi进行图像识别的功能

package com.hui.testend.recongnize;

//import static com.hui.testend.Sample.HTTP_CLIENT;

import com.baidu.aip.util.Base64Util;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.net.URLEncoder;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class altest {
public static final String APP_ID = "679**673";
public static final String API_KEY = "KFP***iZ";
public static final String SECRET_KEY = "edCx3x0*****5X3f";
static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().build();
public MyObject getdata(String img){
String url = "https://aip.baidubce.com/rest/2.0/image-classify/v2/advanced_general";
try {
// 本地文件路径
String filePath = img;
byte[] imgData = FileUtil.readFileByBytes(filePath);
String imgStr = Base64Util.encode(imgData);
String imgParam = URLEncoder.encode(imgStr, "UTF-8");

String param = "image=" + imgParam;
// 注意这里仅为了简化编码每一次请求都去获取access_token,线上环境access_token有过期时间, 客户端可自行缓存,过期后重新获取。
//String accessToken ="24.5***b396.2592000.1717769126.282335-67914673" ;
String accessToken=getAccessToken();
String result = HttpUtil.post(url, accessToken, param);
Gson gson=new Gson();
MyObject a=gson.fromJson(result,MyObject.class);
System.out.println(a.getResult());
for (MyObject.ResultItem b:a.getResult()
) {
System.out.println(b.getKeyword());
}
System.out.println(result);

return a;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public MyObject getdata1(byte[] img){
String url = "https://aip.baid****sify/v2/advanced_general";
try {
// 本地文件路径

byte[] imgData =img;
String imgStr = Base64Util.encode(imgData);
String imgParam = URLEncoder.encode(imgStr, "UTF-8");

String param = "image=" + imgParam;
// 注意这里仅为了简化编码每一次请求都去获取access_token,线上环境access_token有过期时间, 客户端可自行缓存,过期后重新获取。
//String accessToken ="24.5b4e**.2592000.1717769126.282335-67914673" ;
String accessToken=getAccessToken();
String result = HttpUtil.post(url, accessToken, param);
Gson gson=new Gson();
MyObject a=gson.fromJson(result,MyObject.class);
System.out.println(a.getResult());
for (MyObject.ResultItem b:a.getResult()) {
System.out.println(b.getKeyword());
}
System.out.println(result);

return a;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
static String getAccessToken() throws IOException, JSONException {
MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
RequestBody body = RequestBody.create(mediaType, "grant_type=client_credentials&client_id=" + API_KEY
+ "&client_secret=" + SECRET_KEY);
Request request = new Request.Builder()
.url("https://aip.baidubce.com/oauth/2.0/token")
.method("POST", body)
.addHeader("Content-Type", "application/x-www-form-urlencoded")
.build();
Response response = HTTP_CLIENT.newCall(request).execute();
return new JSONObject(response.body().string()).getString("access_token");
}
}

遇到了什么困难:

 

使用百度识图的sdk进行创建项目时发现给出的方法我无法调用,通过给出的文件代码才可以.

明天准备做什么:

准备继续完成团队作业

代码量  100行