View Javadoc
1   package com.foxinmy.weixin4j.wxa.api;
2   
3   import java.awt.Image;
4   import java.awt.image.BufferedImage;
5   import java.io.ByteArrayInputStream;
6   import java.io.ByteArrayOutputStream;
7   import java.io.IOException;
8   import java.io.InputStream;
9   import java.util.HashMap;
10  import java.util.Map;
11  import java.util.Properties;
12  
13  import javax.imageio.ImageIO;
14  
15  import com.foxinmy.weixin4j.exception.WeixinException;
16  import com.foxinmy.weixin4j.http.apache.content.InputStreamBody;
17  import com.foxinmy.weixin4j.http.apache.mime.FormBodyPart;
18  import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
19  import com.foxinmy.weixin4j.token.TokenManager;
20  
21  /**
22   * 违法违规内容检查。
23   *
24   * @since 1.9
25   */
26  public class SecCheckApi extends TokenManagerApi {
27  
28  	public SecCheckApi(TokenManager tokenManager) {
29  		super(tokenManager);
30  	}
31  
32  	public SecCheckApi(TokenManager tokenManager, Properties properties) {
33  		super(tokenManager, properties);
34  	}
35  
36  	private InputStream scale(InputStream inputStream, int maxWidth, int maxHeight) throws IOException {
37  		final BufferedImage srcBufferedImage = ImageIO.read(inputStream);
38  
39  		final int srcWidth = srcBufferedImage.getWidth();
40  		final int srcHeight = srcBufferedImage.getHeight();
41  		final float scale = Math.min((float) maxWidth / (float) srcWidth, (float) maxHeight / (float) srcHeight);
42  
43  		final BufferedImage scaledBufferedImage;
44  		if (scale < 1F) {
45  			final int width = (int) (srcWidth * scale);
46  			final int height = (int) (srcHeight * scale);
47  			final Image scaledImage = srcBufferedImage.getScaledInstance(width, height, Image.SCALE_DEFAULT);
48  
49  			scaledBufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
50  			scaledBufferedImage.getGraphics().drawImage(scaledImage, 0, 0 , null);
51  		} else {
52  			scaledBufferedImage = srcBufferedImage;
53  		}
54  
55  		final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
56  		ImageIO.write(scaledBufferedImage, "png", outputStream);
57  		return new ByteArrayInputStream(outputStream.toByteArray());
58  	}
59  
60  	/**
61  	 * 校验一张图片是否含有违法违规内容。
62  	 *
63  	 * @param inputStream the image input stream.
64  	 * @throws WeixinException indicates getting access token failed, or the content is risky.
65  	 * @see <a href="https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/sec-check/security.imgSecCheck.html">校验一张图片是否含有违法违规内容</a>
66  	 */
67  	public void imgSecCheck(InputStream inputStream) throws WeixinException {
68  		final String imgSecCheckUri = this.getAccessTokenRequestUri("wxa_img_sec_check");
69  		final InputStreamBody inputStreamBody = new InputStreamBody(inputStream, "media");
70  		final FormBodyPart formBodyPart = new FormBodyPart("media", inputStreamBody);
71  		final WeixinResponse response = weixinExecutor.post(imgSecCheckUri, formBodyPart);
72  		final WxaApiResult r = response.getAsObject(WxaApiResult.TYPE_REFERENCE);
73  		r.checkErrCode();
74  	}
75  
76  	public void imgSecCheck(InputStream inputStream, int maxWidth, int maxHeight) throws WeixinException {
77  		try {
78  			this.imgSecCheck(this.scale(inputStream, maxWidth, maxHeight));
79  		} catch (IOException e) {
80  			throw new WeixinException(e);
81  		}
82  	}
83  
84  	/**
85  	 * 检查一段文本是否含有违法违规内容。
86  	 *
87  	 * @param content 要检测的文本内容,长度不超过 500KB,编码格式为 UTF-8。
88  	 * @throws WeixinException indicates getting access token failed, or the content is risky.
89  	 * @see <a href="https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/sec-check/security.msgSecCheck.html">检查一段文本是否含有违法违规内容</a>
90  	 */
91  	public void msgSecCheck(String content) throws WeixinException {
92  		final Map<String, String> params = new HashMap<String, String>(1);
93  		params.put("content", content);
94  		final WxaApiResult r = this.post("wxa_msg_sec_check", params, WxaApiResult.TYPE_REFERENCE);
95  		r.checkErrCode();
96  	}
97  
98  	/**
99  	 * 异步校验图片/音频是否含有违法违规内容。
100 	 *
101 	 * @param mediaUrl 要检测的多媒体 URL
102 	 * @param mediaType {@code 1}: 音频; {@code 2}: 图片
103 	 * @return 任务 ID,用于匹配异步推送结果
104 	 * @throws WeixinException indicates getting access token failed.
105 	 * @see <a href="https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/sec-check/security.mediaCheckAsync.html">security.mediaCheckAsync</a>
106 	 */
107 	public String mediaCheckAsync(String mediaUrl, int mediaType) throws WeixinException {
108 		final Map<String, Object> params = new HashMap<String, Object>(2);
109 		params.put("media_url", mediaUrl);
110 		params.put("media_type", mediaType);
111 		final WxaApiAsyncResult r = this.post("wxa_media_check_async", params, WxaApiAsyncResult.TYPE_REFERENCE);
112 		r.checkErrCode();
113 		return r.getTraceId();
114 	}
115 
116 }