摘要:
Java 获取微信小程序二维码(指定启动页面和动态传入参数),Java 获取微信小程序二维码(指定启动页面和动态传入参数),Java 获取微信小程序二维码(指定启动页面和动态传入参数)
代码案例:
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
import com.alibaba.fastjson.JSON;
import com.what21.server.common.integrate.wechat.WeChat;
public class WeChatApplet extends WeChat {
static Logger logger = Logger.getLogger(WeChatApplet.class);
/**
* @return
*/
public static Map<String, Object> getToken() {
Map<String, Object> data = new HashMap<String, Object>();
try {
// ======================================================================//
// 拼接调用微信的URL
// grant_type:client_credential
// appid:小程序
// secret:小程序
// ======================================================================//
StringBuilder urlSb = new StringBuilder();
urlSb.append("https://api.weixin.qq.com/cgi-bin/token");
urlSb.append("?");
urlSb.append("grant_type=%s&appid=%s&secret=%s");
String tokenUrl = String.format(urlSb.toString(), "client_credential", MIN_APPID, MIN_APPSECRET);
// ======================================================================//
// 执行URL Get调用
// ======================================================================//
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(tokenUrl);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
String resultText = EntityUtils.toString(httpEntity, "utf-8");
// ======================================================================//
// 处理HTTP返回结果
// ======================================================================//
@SuppressWarnings("unchecked")
Map<Object, Object> resultMap = JSON.parseObject(resultText, Map.class);
if (resultMap != null) {
if (resultMap != null && resultMap.size() > 0) {
// 合并2次调用结果
Set<Object> keySet = resultMap.keySet();
for (Object key : keySet) {
if (!data.containsKey(key)) {
data.put((String) key, resultMap.get(key));
}
}
}
}
} catch (Exception e) {
logger.error("fail to request wechat access token. [error={}]", e);
}
return data;
}
/**
* @param accessToken
* @param page
* @param scene
* @param width
* @return
*/
public static Map<String, Object> getMiniQrCode(String accessToken, String page, String scene, int width) {
Map<String, Object> data = new HashMap<String, Object>();
try {
// ======================================================================//
// 拼接调用微信的URL
// ======================================================================//
String url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + accessToken;
Map<String, Object> paramsMap = new HashMap<>();
paramsMap.put("page", page);
paramsMap.put("scene", scene);
paramsMap.put("width", width <= 0 ? 430 : width);
paramsMap.put("auto_color", false);
Map<String, Object> line_color = new HashMap<String, Object>();
line_color.put("r", 0);
line_color.put("g", 0);
line_color.put("b", 0);
paramsMap.put("line_color", line_color);
logger.info("调用生成微信URL接口传参:" + paramsMap);
// ======================================================================//
// 执行URL Post调用
// ======================================================================//
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader(HTTP.CONTENT_TYPE, "application/json");
// 必须是json模式的 post
String body = JSON.toJSONString(paramsMap);
StringEntity entity = new StringEntity(body);
entity.setContentType("image/png");
httpPost.setEntity(entity);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
// ======================================================================//
// 处理HTTP返回结果
// ======================================================================//
InputStream contentStream = httpEntity.getContent();
byte[] bytes = toByteArray(contentStream);
contentStream.read(bytes);
// 返回内容
data.put("qrLength", bytes.length);
data.put("qrBytes", bytes);
data.put("qrBytesEncoder", Base64.getEncoder().encodeToString(bytes));
} catch (Exception e) {
e.printStackTrace();
logger.error("调用生成微信小程序码URL接口异常", e);
}
return data;
}
/**
* @param input
* @return
* @throws IOException
*/
public static byte[] toByteArray(InputStream input) throws IOException {
ByteArrayOutputStream output = new ByteArrayOutputStream();
copy(input, output);
return output.toByteArray();
}
/**
* @param input
* @param output
* @return
* @throws IOException
*/
public static int copy(InputStream input, OutputStream output) throws IOException {
long count = copyLarge(input, output);
if (count > 2147483647L) {
return -1;
}
return (int) count;
}
/**
* @param input
* @param output
* @return
* @throws IOException
*/
public static long copyLarge(InputStream input, OutputStream output) throws IOException {
byte[] buffer = new byte[4096];
long count = 0L;
int n = 0;
while (-1 != (n = input.read(buffer))) {
output.write(buffer, 0, n);
count += n;
}
return count;
}
}
测试方法:
import java.io.FileOutputStream;
import java.util.Map;
public class WeChatAppletTest {
/**
* @param args
*/
public static void main(String[] args) {
String access_token = (String) WeChatApplet.getToken().get("access_token");
Map<String, Object> data = WeChatApplet.getMiniQrCode(access_token, "pages/index/index",
"btype=1&border=xxx",60);
String qrBytesEncoder = (String) data.get("qrBytesEncoder");
Integer qrLength = (Integer) data.get("qrLength");
System.out.println(qrBytesEncoder);
System.out.println(qrLength);
byte[] qrBytes = (byte[]) data.get("qrBytes");
saveToImg("d://qrcode.png",qrBytes);
}
/**
* @param imgPath
* @param bytes
* @return
*/
public static int saveToImg(String imgPath, byte[] bytes) {
int stateInt = 1;
try {
FileOutputStream fos = new FileOutputStream(imgPath);
fos.write(bytes);
fos.flush();
fos.close();
} catch (Exception e) {
stateInt = 0;
e.printStackTrace();
} finally {
}
return stateInt;
}
}
转载:http://www.what21.com/u/10004/6756200547748968305.htm
相关推荐
微信小程序 二维码生成器 (源码)微信小程序 二维码生成器 (源码)微信小程序 二维码生成器 (源码)微信小程序 二维码生成器 (源码)微信小程序 二维码生成器 (源码)微信小程序 二维码生成器 (源码)微信小程序 二维码...
getwxacodeunlimit 生成二维码 并保存在本地 。获取到access_token 可以直接使用
PHP获取微信小程序二维码 三种方式 包括带参数菊花码 A接口,生成小程序码,可接受path参数较长,生成个数受限【菊花码】 #B接口,生成小程序码,可接受页面参数较短,生成个数不受限【菊花码】。 #C接口,生成...
通过微信小程序接口,生成带参数的二维码,并下载之本地返回二维码图片地址
主要介绍了Java实现微信公众号获取临时二维码功能,结合实例形式分析了java调用微信公众号接口实现临时二维码生成功能相关操作技巧,需要的朋友可以参考下
微信小程序二维码制作,生成二维码 小程序开发 源码下载
先介绍一下项目场景,主要是通过微信小程序二维码裂变分享,每个账号有专属邀请二维码,分享出去,有新人扫码入驻,就可以得到现金奖励或红包奖励。当然,产品设计会更丰富,不止有裂变模式,还有渠道推广模式,还有...
微信小程序 小工具类 二维码生成器 (源代码+截图)微信小程序 小工具类 二维码生成器 (源代码+截图)微信小程序 小工具类 二维码生成器 (源代码+截图)微信小程序 小工具类 二维码生成器 (源代码+截图)微信小...
微信小程序源码 二维码生成器(学习版)微信小程序源码 二维码生成器(学习版)微信小程序源码 二维码生成器(学习版)微信小程序源码 二维码生成器(学习版)微信小程序源码 二维码生成器(学习版)微信小程序源码 二维码生成...
微信小程序二维码生成源码,ASP.NET C#源码,带参数的小程序二维码生成源码。
基于qrcodejs的微信小程序二维码生成库
主要介绍了微信小程序实现获取小程序码和二维码java接口开发,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
微信小程序 二维码生成器(截图+源码)微信小程序 二维码生成器(截图+源码)微信小程序 二维码生成器(截图+源码)微信小程序 二维码生成器(截图+源码)微信小程序 二维码生成器(截图+源码)微信小程序 二维码...
微信小程序开发-二维码生成器案例源码.zip
微信小程序二维码生成器 可以输入网址生成相应的二维码,用户可以分享和保存 非常实用的小工具,也可以作为插件功能使用
小程序项目 二维码生成器 《微信小程序》 软件架构 软件架构说明 原生微信小程序 基础功能 二维码生成器,文字、url等生成二维码 二维码反向解析器。 二维码反向解析出来字符串 微信名片,本地存储的一个微信...
Java实现生成带参数的微信小程序二维码图片,存储到本地。
java获取微信小程序openid,包括其他很多小程序操作,下载即可运行
微信小程序 绘制二维码 添加中间头像Logo 公众号Cydiapp
微信小程序,生成logo二维码保存图片;或者在canvas某一部位绘制二维码