import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.aliyuncs.DefaultAcsClient; import com.aliyuncs.IAcsClient; import com.aliyuncs.exceptions.ClientException; import com.aliyuncs.exceptions.ServerException; import com.aliyuncs.green.model.v20180509.ImageSyncScanRequest; import com.aliyuncs.http.FormatType; import com.aliyuncs.http.HttpResponse; import com.aliyuncs.profile.DefaultProfile; import com.aliyuncs.profile.IClientProfile; import java.util.*; /** * Created by liuhai.lh on 2017/2/17. * 图片同步检测接口 * @author liuhai.lh * @date 2017/02/17 */ public class ImageSyncScanRequestSample extends BaseSample { public static void main(String[] args) throws Exception { //请替换成你自己的accessKeyId、accessKeySecret IClientProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret); DefaultProfile.addEndpoint(getEndPointName(), regionId, "Green", getDomain()); IAcsClient client = new DefaultAcsClient(profile); ImageSyncScanRequest imageSyncScanRequest = new ImageSyncScanRequest(); imageSyncScanRequest.setAcceptFormat(FormatType.JSON); // 指定api返回格式 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", "http://f.hiphotos.baidu.com/image/pic/item/aa18972bd40735fa13899ac392510fb30f24084b.jpg"); task.put("time", new Date()); tasks.add(task); JSONObject data = new JSONObject(); /** * porn: 色情 * terrorism: 暴恐 * qrcode: 二维码 * ad: 图片广告 * ocr: 文字识别 */ data.put("scenes", Arrays.asList("porn","terrorism")); 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")); System.out.println(JSON.toJSONString(scrResponse, true)); 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"); //根据scene和suggetion做相关的处理 //do something System.out.println("args = [" + scene + "]"); System.out.println("args = [" + suggestion + "]"); } }else{ System.out.println("task process fail:" + ((JSONObject)taskResult).getInteger("code")); } } } else { System.out.println("detect not success. code:" + scrResponse.getInteger("code")); } } else { System.out.println("response not success. status:" + httpResponse.getStatus()); } } catch (ServerException e) { e.printStackTrace(); } catch (ClientException e) { e.printStackTrace(); } catch (Exception e){ e.printStackTrace(); } } }