说明:学习黑马头条day4内容时,按视频教程开通阿里云云盾内容安全产品试用页面 出现提示:当前的账户还未完成企业认证,请认证后再开通服务 。
https://blog.csdn.net/weixin_47314924/article/details/131966117 博客提供了解决办法。
但测试图片审核时出现了以下错误提示:
com.aliyun.tea.TeaException: code: 400, 图片无法下载,请检查链接是否可访问和本地网络情况 - 非上海OSS图片链接请参考:https://help.aliyun.com/document_detail/155645.html。URL中不支持空格和中文字符,如包含请通过urlEncode编码之后再传入。
错误原因
这是因为上述博客提供的 具有图片审核功能的GreenTextScan代码 是文件在同地域OSS条件下,不能审核非上海OOS图片。
解决办法
于是通过查官方文档对上述博客内容中的 GreenTextScan代码作了修改,代码如下:
package com.heima.common.aliyun;
import com.alibaba.fastjson.JSON;
import com.aliyun.imageaudit20191230.models.ScanImageAdvanceRequest;
import com.aliyun.imageaudit20191230.models.ScanImageResponse;
import com.aliyun.imageaudit20191230.models.ScanImageResponseBody;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.io.InputStream;
import java.net.URL;
import java.util.*;
@Getter
@Setter
@Component
@ConfigurationProperties(prefix = "aliyun")
public class GreenImageScan {
private String accessKeyId;
private String secret;
private String scenes;
public Map imageScan(List<String> imageList) throws Exception {
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
.setAccessKeyId(accessKeyId)
.setAccessKeySecret(secret);
// 访问的域名
config.endpoint = "imageaudit.cn-shanghai.aliyuncs.com";
com.aliyun.imageaudit20191230.Client client = new com.aliyun.imageaudit20191230.Client(config);
List<ScanImageAdvanceRequest.ScanImageAdvanceRequestTask> taskList = new ArrayList<>();
for (String img: imageList) {
URL url = new URL(img);
InputStream inputStream = url.openConnection().getInputStream();
ScanImageAdvanceRequest.ScanImageAdvanceRequestTask task = new ScanImageAdvanceRequest.ScanImageAdvanceRequestTask();
task.setImageURLObject(inputStream);
task.setDataId(UUID.randomUUID().toString());
task.setImageTimeMillisecond(1L);
task.setInterval(1);
task.setMaxFrames(1);
taskList.add(task);
}
//场景
List<String> sceneList = new ArrayList<>();
sceneList.add(scenes);
ScanImageAdvanceRequest scanImageAdvanceRequest = new ScanImageAdvanceRequest()
.setTask(taskList)
.setScene(sceneList);
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
Map<String, String> resultMap = null;
try {
ScanImageResponse scanImageResponse = client.scanImageAdvance(scanImageAdvanceRequest, runtime);
resultMap = new HashMap<>();
if (scanImageResponse.getStatusCode() == 200) {
List<ScanImageResponseBody.ScanImageResponseBodyDataResultsSubResults> subResults = scanImageResponse.body.data.results.get(0).getSubResults();
ListIterator<ScanImageResponseBody.ScanImageResponseBodyDataResultsSubResults> listIterator = subResults.listIterator();
while (listIterator.hasNext()) {
ScanImageResponseBody.ScanImageResponseBodyDataResultsSubResults item = listIterator.next();
resultMap.put("scene", item.scene);
resultMap.put("suggestion", item.suggestion);
resultMap.put("label", item.label);
}
} else {
/* *
* 表明请求整体处理失败,原因视具体的情况详细分析
*/
System.out.println("the whole image scan request failed. response:" + JSON.toJSONString(scanImageResponse));
return null;
}
} catch (com.aliyun.tea.TeaException teaException) {
// 获取整体报错信息
System.out.println(com.aliyun.teautil.Common.toJSONString(teaException));
// 获取单个字段
System.out.println(teaException.getCode());
}
return resultMap;
}
}
再进行测试结果如下:
测试代码
package com.heima.wemedia;
import com.heima.common.aliyun.GreenImageScan;
import com.heima.common.aliyun.GreenTextScan;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@SpringBootTest(classes = WemediaApplication.class)
@RunWith(SpringRunner.class)
public class AliyunTest {
@Autowired
private GreenTextScan greenTextScan;
@Autowired
private GreenImageScan greenImageScan;
@Test
public void testScanText() throws Exception {
Map map = greenTextScan.greeTextScan("王天刚去饭店吃饭后发现自己的车子被刮了,破口大骂是哪个傻逼干的?");
System.out.println(map);
}
@Test
public void testScanImage() throws Exception {
List<String> list=new ArrayList<>();
list.add("http://192.168.170.130:9000/leadnews/2023/08/16/29fcfbc4475c4978bb80759c73f8a3ba.jpeg");
Map map = greenImageScan.imageScan(list);
System.out.println(map);
}
}
运行结果
问题解决~