微信图片检测

2020-03-29  本文已影响0人  孙瑞锴

1.借鉴

微信开发者文档

2. 开始

请求方法

public WxAccessToken getWxAccessToken() throws Exception {
    String resposne = restTemplate.getForObject(String.format(weixinConfig.getAccessTokenUrl(), weixinConfig.getAppId(), weixinConfig.getAppSecret()), String.class);
    log.info(String.format("[获取accessToken结果]:%s", resposne));

    JSONObject jsonObject = JSON.parseObject(resposne);
    WxAccessToken wxAccessToken = new WxAccessToken();
    wxAccessToken.setAccessToken(jsonObject.getString("access_token"));
    wxAccessToken.setExpires(jsonObject.getIntValue("expires_in"));
    return wxAccessToken;
}


 public Result2<Boolean, String> imageCheck() {
    try
    {
        WxAccessToken wxAccessToken = getWxAccessToken();

        final String filePath = "/xxx/xxx/xxx";
        final String fileName = "12345.jpg";

        //设置请求头
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("multipart/form-data");
        headers.setContentType(type);

        //读取网络图片
//      UrlResource urlResource = new UrlResource("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1585216274927&di=8056bd399b43592731dda0e7f7128769&imgtype=0&src=http%3A%2F%2Fgz.feixin.10086.cn%2FPublic%2FUploads%2Fuser%2F5%2F7%2F13%2F3%2F1160265713%2Fimgs%2F5618ae06645f0.jpg");
            
        // 读取本地图片
        FileSystemResource fileSystemResource = new FileSystemResource(filePath + File.separator + fileName);
        MultiValueMap<String, Object> form = new LinkedMultiValueMap<>();
        // 设置本地图片
        form.add("media", fileSystemResource);
        //设置网络图片
//       form.add("media", urlResource);
        HttpEntity<MultiValueMap<String, Object>> files = new HttpEntity<>(form, headers);

        String s = restTemplate.postForObject(String.format(weixinConfig.getImageCheckUrl(), wxAccessToken.getAccessToken()), files, String.class);
        log.info("图片检测结果:" + s);
        JSONObject jsonObject = JSON.parseObject(s);
        if (jsonObject.getIntValue("errcode") == 0) {
            return Result2.of(true, "内容正常");
        }
        else {
            return Result2.of(false, jsonObject.getString("errMsg"));
        }
    } catch (Exception e)
    {
        e.printStackTrace();
    }

    return Result2.of(false, "检测失败");
}

WxAccessToken.class

@Data
public class WxAccessToken
{
    private String accessToken;
    // 单位秒
    private Integer expires;
}

Result2.class

@Data
public class Result2<R1, R2>
{
    private R1 r1;
    private R2 r2;

    public Result2<R1, R2> of(R1 r1, R2 r2) {
        this.r1 = r1;
        this.r2 = r2;
        return this;
    }
}

WeixinConfig.class

@Configuration
@Data
@ConfigurationProperties("weixin")
public class WeixinConfig {
    /**
     * 图片检测
     */
    private String imageCheckUrl;
}

WxAccessToken.class

@Data
public class WxAccessToken
{
    private String accessToken;
    private Integer expires;
}

yml配置

weixin:
  app-id: 1234567
  app-secret: 7654321
  auth-url: https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code
  access-token-url: https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s
  image-check-url: https://api.weixin.qq.com/wxa/img_sec_check?access_token=%s

3.大功告成

虽然图片的识别结果不尽如人意

上一篇下一篇

猜你喜欢

热点阅读