您的位置:首页 > 移动开发 > 微信开发

微信开发总结 五

2015-08-03 13:41 357 查看
1.二维码

public class QRCode {
private String ticket;
private int expireSeconds;
private String url;


2.二维码常量

public class QRCodeConstant {
/**创建二维码 POST*/
public static final String QRCODE_CREATE_URL="https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=ACCESS_TOKEN";

/**通过ticket获取二维码 GET*/
public static final String QRCODE_TICKET_URL="https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=TICKET";

}


3.工具类

public class QRCodeUtil {
/**
* 创建临时二维码
* @author wuyw
* 2015-7-30下午12:30:44
* @param accessToken
* @param expireSeconds
* @param sceneId 场景值ID,临时二维码时为32位非0整型,永久二维码时最大值为100000(目前参数只支持1--100000)
* @return
*/
public static QRCode createTemporaryQRCode(String accessToken, int expireSeconds, int sceneId) {
QRCode code = new QRCode();
String requestUrl = QRCodeConstant.QRCODE_CREATE_URL.replace("ACCESS_TOKEN", accessToken);
String jsonMsg = "{\"expire_seconds\": %d, \"action_name\": \"QR_SCENE\", \"action_info\": {\"scene\": {\"scene_id\": %d}}";

JSONObject jsonObject = TokenUtil.httpsRequest(requestUrl, BaseConstant.TOKEN_URL_POST, String.format(jsonMsg, expireSeconds, sceneId));

if (null != jsonObject) {
code.setTicket(jsonObject.getString("ticket"));
code.setExpireSeconds(jsonObject.getInt("expire_seconds"));
code.setUrl(jsonObject.getString("url"));
}

return code;
}

/**
* 创建永久二维码
* @author wuyw
* 2015-7-30下午12:31:14
* @param accessToken
* @param sceneId
* @return
*/
public static QRCode createPermanentQRCode(String accessToken, int sceneId) {
QRCode code = new QRCode();
String requestUrl = QRCodeConstant.QRCODE_CREATE_URL.replace("ACCESS_TOKEN", accessToken);
String jsonMsg = "{\"action_name\": \"QR_LIMIT_SCENE\", \"action_info\": {\"scene\": {\"scene_id\": %d}}";

JSONObject jsonObject = TokenUtil.httpsRequest(requestUrl, BaseConstant.TOKEN_URL_POST, String.format(jsonMsg, sceneId));

if (null != jsonObject) {
code.setTicket(jsonObject.getString("ticket"));
code.setExpireSeconds(jsonObject.getInt("expire_seconds"));
code.setUrl(jsonObject.getString("url"));
}

return code;
}

/**
* 根据ticket获取二维码图片
* @author wuyw
* 2015-7-30下午12:32:47
* @param accessToken
* @param ticket
* @return
*/
public static String getQRCode(String ticket, String savePath) {
String filePath = null;
String requestUrl = QRCodeConstant.QRCODE_TICKET_URL.replace("TICKET", SignUtil.urlEncodeUTF8(ticket));

try {
URL url = new URL(requestUrl);
HttpsURLConnection connection = (HttpsURLConnection) url
.openConnection();
connection.setDoInput(true);
connection.setRequestMethod(BaseConstant.TOKEN_URL_GET);
if (!savePath.endsWith("/")) {
savePath += "/";
}
filePath = savePath + ticket + ".jpg";
BufferedInputStream bis = new BufferedInputStream(
connection.getInputStream());
FileOutputStream fos = new FileOutputStream(new File(filePath));
byte[] buf = new byte[2048];
int size = 0;
while ((size = bis.read(buf)) != -1) {
fos.write(buf, 0, size);
}
fos.close();
bis.close();
connection.disconnect();
} catch (Exception e) {
e.printStackTrace();
}

return filePath;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: