|
|
@@ -3,16 +3,16 @@ package com.izouma.yags.utils;
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.github.kevinsawicki.http.HttpRequest;
|
|
|
-import org.apache.commons.io.FileUtils;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
import javax.imageio.ImageIO;
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
-import java.io.File;
|
|
|
import java.io.IOException;
|
|
|
import java.net.URL;
|
|
|
import java.time.LocalDateTime;
|
|
|
import java.util.Base64;
|
|
|
|
|
|
+@Slf4j
|
|
|
public class RecognizeUtil {
|
|
|
private static String accessToken = null;
|
|
|
private static LocalDateTime expireAt = null;
|
|
|
@@ -34,16 +34,20 @@ public class RecognizeUtil {
|
|
|
}
|
|
|
JSONObject body = new JSONObject();
|
|
|
body.put("image", image);
|
|
|
- return JSON.parseObject(HttpRequest.post("https://aip.baidubce.com/rpc/2.0/ai_custom_bml/v1/classification/game1?access_token=" + accessToken)
|
|
|
+ HttpRequest request = HttpRequest.post("https://aip.baidubce.com/rpc/2.0/ai_custom_bml/v1/classification/game1?access_token=" + accessToken)
|
|
|
.header("Content-Type", "application/json")
|
|
|
- .send(body.toJSONString())
|
|
|
- .body());
|
|
|
+ .send(body.toJSONString());
|
|
|
+ int code = request.code();
|
|
|
+ String res = request.body();
|
|
|
+ log.info("recognize code ={}, result={}", code, res);
|
|
|
+ return JSON.parseObject(res);
|
|
|
}
|
|
|
|
|
|
public static void main(String[] args) throws IOException {
|
|
|
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
|
|
ImageIO.write(ImageIO.read(new URL("https://cdn.raex.vip/image/2022-08-08-14-51-42lexfxYDl.jpg")), "jpg", os);
|
|
|
- JSONObject jsonObject = recognize(Base64.getEncoder().encodeToString(os.toByteArray()));;
|
|
|
+ JSONObject jsonObject = recognize(Base64.getEncoder().encodeToString(os.toByteArray()));
|
|
|
+ ;
|
|
|
System.out.println(JSON.toJSONString(jsonObject, true));
|
|
|
}
|
|
|
}
|