MyGreenImageScan

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 com.heima.file.service.FileStorageService;
import com.sun.xml.internal.messaging.saaj.util.ByteInputStream;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.*;

@Getter
@Setter
@Component
@ConfigurationProperties(prefix = "aliyun")
public class MyGreenImageScan {

    private String accessKeyId;
    private String secret;
    private String scenes;
    @Autowired
    FileStorageService fileStorageService;

    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) {
            ScanImageAdvanceRequest.ScanImageAdvanceRequestTask task = new ScanImageAdvanceRequest.ScanImageAdvanceRequestTask();
            byte[] bytes = fileStorageService.downLoadFile(img);
            ByteInputStream byteInputStream = new ByteInputStream(bytes,bytes.length);
            task.setImageURLObject(byteInputStream);
            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);
        sceneList.add("logo");
        sceneList.add("porn");
        
        ScanImageAdvanceRequest scanImageAdvanceRequest=new ScanImageAdvanceRequest();
        scanImageAdvanceRequest.setTask(taskList);
        scanImageAdvanceRequest.setScene(sceneList);
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        try {
            ScanImageResponse scanImageResponse = client.scanImageAdvance(scanImageAdvanceRequest, runtime);
            Map<String, String> 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();
                    System.out.println("scene = [" + item.scene + "]");
                    System.out.println("suggestion = [" + item.suggestion + "]");
                    System.out.println("label = [" + item.label + "]");

                    if (!item.suggestion.equals("pass")) {
                        resultMap.put("suggestion", item.suggestion);
                        resultMap.put("label", item.label);
                        return resultMap;
                    }
                }
                resultMap.put("suggestion", "pass");

            } 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 null;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值