阿里云内容管理图片校验简单使用
发布日期:2021-05-06 19:32:59 浏览次数:21 分类:精选文章

本文共 5591 字,大约阅读时间需要 18 分钟。

阿里云图片校验的实现过程

在阿里云中部署图片校验服务是一个高效的解决方案。以下是具体的实现步骤和技术细节。

首先,您需要拥有一个阿里云帐号。接下来,请按照以下步骤配置阿里云开发环境:

  • 创建阿里云开发者账号:登录阿里云官网,注册并创建一个新的开发者账号。

  • 获取必要的配置信息:在账户设置中,创建一个子账号,获取accessKeyId和accessKeySecret。这两个密钥用于身份认证,确保您的代码能够正常与阿里云服务进行交互。

  • 开通阿里云服务:进入“内容管理服务”官网,选择适合您需求的套餐。阿里云提供免费试用,建议新手选择30天内免费使用的选项。

  • 配置访问权限:在阿里云控制台中,进入“访问控制”页面,将权限设置为“AliyunYundunGreenWebFullAccess”。这将允许您的应用程序访问绿网服务。

  • 接下来,准备好开发环境:

  • 创建一个Maven项目:使用Eclipse或IntelliJ IDEA创建一个新的Maven项目。

  • 配置配置文件:创建一个名为config.properties的文件,填写以下内容:

    • accessKeyId=xxxx(阿里云提供的API密钥)
    • accessKeySecret=xxx(对应的密钥)
    • regionId=cn-beijing(选择中国北京的区域)
  • 添加依赖项:在项目的pom.xml中,添加阿里云SDK的依赖。以下是示例代码:

    com.aliyun
    aliyun-java-sdk-core
    3.5.0
    com.aliyun
    aliyun-java-sdk-green
    3.5.0
  • 导入SDK示例代码:将阿里云提供的SDK示例代码导入到您的项目中。以下是一个简化的代码示例:

  • package com.hqjl.communityserv.imageCheck;
    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.JSONObject;
    import com.aliyuncs.DefaultAcsClient;
    import com.aliyuncs.exceptions.ClientException;
    import com.aliyuncs.exceptions.ServerException;
    import com.aliyuncs.http.FormatType;
    import com.aliyuncs.http.HttpResponse;
    import com.aliyuncs.profile.DefaultProfile;
    import com.aliyuncs.profile.IClientProfile;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.stereotype.Component;
    import java.util.Map;
    import java.util.List;
    @Component
    public class ImageSyncScanRequest extends BaseRequest {
    private final Logger LOG = LoggerFactory.getLogger(ImageSyncScanRequest.class);
    private final String AD = "ad";
    private final String PASS = "pass";
    public Boolean checkPic(String url) throws Exception {
    IClientProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
    DefaultProfile.addEndpoint(getEndPointName(), regionId, "Green", getDomain());
    IAcsClient client = new DefaultAcsClient(profile);
    com.aliyuncs.green.model.v20180509.ImageSyncScanRequest imageSyncScanRequest = new com.aliyuncs.green.model.v20180509.ImageSyncScanRequest();
    imageSyncScanRequest.setAcceptFormat(FormatType.JSON);
    imageSyncScanRequest.setMethod(com.aliyuncs.http.MethodType.POST);
    imageSyncScanRequest.setEncoding("utf-8");
    imageSyncScanRequest.setRegionId(regionId);
    List
    > tasks = new ArrayList<>();
    Map
    task = new LinkedHashMap<>();
    task.put("dataId", UUID.randomUUID().toString());
    task.put("url", url);
    task.put("time", new Date());
    tasks.add(task);
    JSONObject data = new JSONObject();
    data.put("scenes", Arrays.asList(AD));
    data.put("tasks", tasks);
    imageSyncScanRequest.setHttpContent(data.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON);
    imageSyncScanRequest.setConnectTimeout(3000);
    imageSyncScanRequest.setReadTimeout(10000);
    try {
    HttpResponse httpResponse = client.doAction(imageSyncScanRequest);
    if (httpResponse.isSuccess()) {
    JSONObject scrResponse = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8"));
    if (200 == scrResponse.getInteger("code")) {
    JSONArray taskResults = scrResponse.getJSONArray("data");
    for (Object taskResult : taskResults) {
    if (200 == ((JSONObject) taskResult).getInteger("code")) {
    JSONArray sceneResults = ((JSONObject) taskResult).getJSONArray("results");
    for (Object sceneResult : sceneResults) {
    String scene = ((JSONObject) sceneResult).getString("scene");
    String suggestion = ((JSONObject) sceneResult).getString("suggestion");
    if (!suggestion.equals(PASS)) {
    return false;
    }
    }
    } else {
    LOG.error("task process fail:" + ((JSONObject) taskResult).getInteger("code"));
    LOG.error("msg=" + scrResponse.getString("msg"));
    return false;
    }
    }
    } else {
    LOG.error("detect not success. code:" + scrResponse.getInteger("code"));
    LOG.error("msg=" + scrResponse.getString("msg"));
    return false;
    }
    } else {
    LOG.error("response not success. status:" + httpResponse.getStatus());
    LOG.error(httpResponse.toString());
    return false;
    }
    } catch (ServerException e) {
    LOG.error(e.getMessage(), e);
    return false;
    } catch (ClientException e) {
    LOG.error(e.getMessage(), e);
    return false;
    } catch (Exception e) {
    LOG.error(e.getMessage(), e);
    return false;
    }
    return true;
    }
    }

    以上代码示例展示了如何将图片通过阿里云绿网服务进行校验。关键点包括:

    -图片校验的主要场景包括色情内容、暴恐内容、二维码识别、广告检测和文本识别等。

    -通过设置 scenes 参数为 "ad",可以对图片进行广告内容检测。

    • task 参数设置为包含图片的唯一标识符和访问时间,确保校验结果与图片生命周期一致。

    -返回结果根据 suggestion 字段判断是否通过校验,建议设置为 "pass" 表示通过。

    在实际应用中,建议:

    • 确保传入的 URL 是完整的 URL 地址。

    • 根据图片的实际存储路径调整 URL 参数。

    • 建议结合图片的存储时间(如图片上传时间)作为 task 中的 time 参数。

    • 根据实际需求调整 regions 参数,支持的区域包括 cn-shanghai、cn-beijing 等。

    通过以上配置和代码示例,您可以轻松实现阿里云绿网服务的图片校验功能。

    上一篇:Ehcache 基本使用 以及 基于RMI集群搭建
    下一篇:springboot利用切面记录在线人数

    发表评论

    最新留言

    做的很好,不错不错
    [***.243.131.199]2025年04月30日 09时46分20秒

    关于作者

        喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
    -- 愿君每日到此一游!

    推荐文章