java 生成微信小程序二维码 base64 格式并携带参数 java 后端 完整代码
时间: 2024-01-12 10:05:06 浏览: 209
以下是Java生成微信小程序带参数二维码的完整代码:
```java
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.codec.binary.Base64;
import org.json.JSONObject;
public class WxMaQrcodeUtils {
private static final String API_URL = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode";
private static final String PARAM_PAGE = "page";
private static final String PARAM_SCENE = "scene";
private static final String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token";
/**
* 获取access_token
*
* @param appid 小程序appid
* @param secret 小程序secret
* @return access_token
*/
public static String getAccessToken(String appid, String secret) {
String accessToken = null;
try {
String url = ACCESS_TOKEN_URL + "?grant_type=client_credential&appid=" + appid + "&secret=" + secret;
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
int responseCode = con.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
InputStream inputStream = con.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder stringBuilder = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line);
}
bufferedReader.close();
JSONObject jsonObject = new JSONObject(stringBuilder.toString());
accessToken = jsonObject.getString("access_token");
}
} catch (Exception e) {
e.printStackTrace();
}
return accessToken;
}
/**
* 生成小程序带参数二维码
*
* @param accessToken 小程序access_token
* @param page 小程序页面路径
* @param scene 小程序页面参数
* @return base64格式的二维码图片数据
*/
public static String createQrcode(String accessToken, String page, String scene) {
String base64Image = null;
try {
Map<String, Object> params = new HashMap<>();
params.put(PARAM_PAGE, page);
params.put(PARAM_SCENE, scene);
URL obj = new URL(API_URL + "?access_token=" + accessToken);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/json");
con.setDoOutput(true);
con.getOutputStream().write(new JSONObject(params).toString().getBytes("UTF-8"));
int responseCode = con.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
InputStream inputStream = con.getInputStream();
byte[] imageBytes = new byte[inputStream.available()];
inputStream.read(imageBytes);
inputStream.close();
base64Image = Base64.encodeBase64String(imageBytes);
}
} catch (Exception e) {
e.printStackTrace();
}
return base64Image;
}
public static void main(String[] args) {
String appid = "your_appid";
String secret = "your_secret";
String accessToken = getAccessToken(appid, secret);
String page = "pages/index/index";
String scene = "key=value";
String base64Image = createQrcode(accessToken, page, URLEncoder.encode(scene));
System.out.println(base64Image);
}
}
```
注意,需要引入Apache Commons Codec库。
其中,getAccessToken方法用于获取小程序的access_token,createQrcode方法用于生成小程序带参数二维码并返回base64格式的图片数据。在main方法中调用以上两个方法即可生成小程序二维码。
阅读全文