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
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
64
65
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
88
89
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
102
103
104
105
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 }