后台服务
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

114 行
4.7 KiB

  1. package com.iformall.utils;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.iformall.common.ErrorCode;
  5. import com.iformall.common.ResultData;
  6. import com.iformall.sm.AiBaiduCheckResult;
  7. import okhttp3.*;
  8. import org.springframework.web.multipart.MultipartFile;
  9. import java.io.*;
  10. import java.net.URLEncoder;
  11. import java.io.IOException;
  12. import java.io.InputStream;
  13. import java.net.URL;
  14. import java.net.URLConnection;
  15. /**
  16. * 图片质量检查方法
  17. */
  18. public class BaiduImageCheckUtil {
  19. // 百度图片审核接口地址
  20. private final static String photo_check_url = "https://aip.baidubce.com/rest/2.0/solution/v1/img_censor/v2/user_defined";
  21. // 获取token
  22. private final static String auth_url = "https://aip.baidubce.com/oauth/2.0/token?";
  23. // 百度API Key
  24. private final static String apiKey = "Your API Key";
  25. // 百度Secret Key
  26. private final static String secretKey = "Your Secret Key";
  27. //写死的 access_token
  28. private final static String access_token = "24.c1c7992795301773c60c8cf01abfe759.2592000.1689236723.282335-33618414";
  29. public static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().build();
  30. public static ResultData photoCheck(MultipartFile file) {
  31. try {
  32. byte[] fileBytes = file.getBytes();
  33. String imgStr = Base64Util.encode(fileBytes);
  34. String imageUrl = URLEncoder.encode(imgStr, "UTF-8");
  35. String param = "image=" + imageUrl;
  36. // 注意这里access_token有过期时间, 客户端可自行缓存,过期后重新获取。
  37. // Access Token的有效期(秒为单位,有效期30天)
  38. // String accessToken = getAuth();
  39. //这里暂时写死
  40. String result = BaiDuHttpUtil.post(photo_check_url, access_token, param);
  41. System.out.println(result);
  42. JSONObject jsonObject = JSON.parseObject(result);
  43. //1:合规,2:不合规,3:疑似,4:审核失败
  44. Integer type = jsonObject.getInteger("conclusionType");
  45. if (type == 1) {
  46. return new ResultData();
  47. } else if (type == 2 || type == 3) {
  48. return new ResultData(ErrorCode.SYS_SERVER_ERROR.getCode(),"图片不合规");
  49. } else if (type == 4) {
  50. return new ResultData(ErrorCode.SYS_SERVER_ERROR.getCode(),"图片审核失败");
  51. } else {
  52. return new ResultData(ErrorCode.SYS_SERVER_ERROR.getCode(),"图片不合规");
  53. }
  54. } catch (Exception e) {
  55. e.printStackTrace();
  56. return new ResultData(ErrorCode.SYS_SERVER_ERROR.getCode(),"图片审核失败");
  57. }
  58. }
  59. /**
  60. * 将阿里云图片地址转为字节码
  61. */
  62. public static String aliyunImageToByteArray(String imageUrl) throws IOException {
  63. InputStream inputStream = null;
  64. try {
  65. // 创建URL对象
  66. URL url = new URL(imageUrl);
  67. // 打开连接
  68. URLConnection conn = url.openConnection();
  69. // 获取输入流
  70. inputStream = conn.getInputStream();
  71. // 将输入流转换为字节数组
  72. ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
  73. byte[] buffer = new byte[4096];
  74. int bytesRead;
  75. while ((bytesRead = inputStream.read(buffer)) != -1) {
  76. byteStream.write(buffer, 0, bytesRead);
  77. }
  78. inputStream.close();
  79. byte[] byteArray = byteStream.toByteArray();
  80. String imgStr = Base64Util.encode(byteArray);
  81. return URLEncoder.encode(imgStr, "UTF-8");
  82. } catch (IOException e) {
  83. e.printStackTrace();
  84. if (inputStream != null) {
  85. inputStream.close();
  86. }
  87. }
  88. return null;
  89. }
  90. /**
  91. * 获取API访问权限token,该token有一定的有效期,需要自行管理,当失效时需重新获取。
  92. * Access Token的有效期(秒为单位,有效期30天)
  93. */
  94. public static String getAuth() throws IOException {
  95. MediaType mediaType = MediaType.parse("application/json");
  96. RequestBody body = RequestBody.create(mediaType, "");
  97. Request request = new Request.Builder()
  98. .url(auth_url + "client_id=" + apiKey + "&client_secret= " + secretKey + "&grant_type=client_credentials")
  99. .method("POST", body)
  100. .addHeader("Content-Type", "application/json")
  101. .addHeader("Accept", "application/json")
  102. .build();
  103. Response response = HTTP_CLIENT.newCall(request).execute();
  104. System.out.println(response.body().string());
  105. return response.body().string();
  106. }
  107. }