|
- package com.iformall.utils;
-
- import com.alibaba.fastjson.JSON;
- import com.alibaba.fastjson.JSONObject;
- import com.iformall.common.ErrorCode;
- import com.iformall.common.ResultData;
- import com.iformall.sm.AiBaiduCheckResult;
- import okhttp3.*;
- import org.springframework.web.multipart.MultipartFile;
-
- import java.io.*;
- import java.net.URLEncoder;
- import java.io.IOException;
- import java.io.InputStream;
- import java.net.URL;
- import java.net.URLConnection;
-
- /**
- * 图片质量检查方法
- */
- public class BaiduImageCheckUtil {
- // 百度图片审核接口地址
- private final static String photo_check_url = "https://aip.baidubce.com/rest/2.0/solution/v1/img_censor/v2/user_defined";
- // 获取token
- private final static String auth_url = "https://aip.baidubce.com/oauth/2.0/token?";
- // 百度API Key
- private final static String apiKey = "Your API Key";
- // 百度Secret Key
- private final static String secretKey = "Your Secret Key";
- //写死的 access_token
- private final static String access_token = "24.c1c7992795301773c60c8cf01abfe759.2592000.1689236723.282335-33618414";
-
- public static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().build();
-
- public static ResultData photoCheck(MultipartFile file) {
- try {
- byte[] fileBytes = file.getBytes();
- String imgStr = Base64Util.encode(fileBytes);
- String imageUrl = URLEncoder.encode(imgStr, "UTF-8");
- String param = "image=" + imageUrl;
- // 注意这里access_token有过期时间, 客户端可自行缓存,过期后重新获取。
- // Access Token的有效期(秒为单位,有效期30天)
- // String accessToken = getAuth();
- //这里暂时写死
- String result = BaiDuHttpUtil.post(photo_check_url, access_token, param);
- System.out.println(result);
- JSONObject jsonObject = JSON.parseObject(result);
- //1:合规,2:不合规,3:疑似,4:审核失败
- Integer type = jsonObject.getInteger("conclusionType");
- if (type == 1) {
- return new ResultData();
- } else if (type == 2 || type == 3) {
- return new ResultData(ErrorCode.SYS_SERVER_ERROR.getCode(),"图片不合规");
- } else if (type == 4) {
- return new ResultData(ErrorCode.SYS_SERVER_ERROR.getCode(),"图片审核失败");
- } else {
- return new ResultData(ErrorCode.SYS_SERVER_ERROR.getCode(),"图片不合规");
- }
- } catch (Exception e) {
- e.printStackTrace();
- return new ResultData(ErrorCode.SYS_SERVER_ERROR.getCode(),"图片审核失败");
- }
- }
-
- /**
- * 将阿里云图片地址转为字节码
- */
- public static String aliyunImageToByteArray(String imageUrl) throws IOException {
- InputStream inputStream = null;
- try {
- // 创建URL对象
- URL url = new URL(imageUrl);
- // 打开连接
- URLConnection conn = url.openConnection();
- // 获取输入流
- inputStream = conn.getInputStream();
- // 将输入流转换为字节数组
- ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
- byte[] buffer = new byte[4096];
- int bytesRead;
- while ((bytesRead = inputStream.read(buffer)) != -1) {
- byteStream.write(buffer, 0, bytesRead);
- }
- inputStream.close();
- byte[] byteArray = byteStream.toByteArray();
- String imgStr = Base64Util.encode(byteArray);
- return URLEncoder.encode(imgStr, "UTF-8");
- } catch (IOException e) {
- e.printStackTrace();
- if (inputStream != null) {
- inputStream.close();
- }
- }
- return null;
- }
-
- /**
- * 获取API访问权限token,该token有一定的有效期,需要自行管理,当失效时需重新获取。
- * Access Token的有效期(秒为单位,有效期30天)
- */
- public static String getAuth() throws IOException {
- MediaType mediaType = MediaType.parse("application/json");
- RequestBody body = RequestBody.create(mediaType, "");
- Request request = new Request.Builder()
- .url(auth_url + "client_id=" + apiKey + "&client_secret= " + secretKey + "&grant_type=client_credentials")
- .method("POST", body)
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json")
- .build();
- Response response = HTTP_CLIENT.newCall(request).execute();
- System.out.println(response.body().string());
- return response.body().string();
- }
- }
|