小程序码生成随记

1.查看小程序api生成二维码,小程序码(有a,b,c等多种方式)

2.通过百度查找java小程序码搜索

3.搭建测试环境测试demo案例

注意事项:上述涉及到的appid和秘钥是小程序的非公众号的

接口B:适用于需要的码数量极多,或仅临时使用的业务场景

接口地址:

https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=ACCESS_TOKEN

获取 access_token 详见文档

POST 参数说明

参数类型默认值说明
sceneString最大32个可见字符,只支持数字,大小写英文以及部分特殊字符:!#$&\'()*+,/:;=?@-._~,其它字符请自行编码为合法字符(因不支持%,中文无法使用 urlencode 处理,请使用其他编码方式)
pageString必须是已经发布的小程序存在的页面(否则报错),例如 "pages/index/index" ,根路径前不要填加\'/\',不能携带参数(参数请放在scene字段里),如果不填写这个字段,默认跳主页面
widthInt430二维码的宽度
auto_colorBoolfalse自动配置线条颜色,如果颜色依然是黑色,则说明不建议配置主色调
line_colorObject{"r":"0","g":"0","b":"0"}auto_color 为 false 时生效,使用 rgb 设置颜色 例如 {"r":"xxx","g":"xxx","b":"xxx"}

注意:通过该接口生成的小程序码,永久有效,数量暂无限制。用户扫描该码进入小程序后,开发者需在对应页面获取的码中 scene 字段的值,再做处理逻辑。使用如下代码可以获取到二维码中的 scene 字段的值。调试阶段可以使用开发工具的普通编译下拉(添加编译模式)自定义参数 scene=xxxx 进行模拟,开发工具模拟时的 scene 的参数值需要进行 urlencode

// 这是首页的 js
Page({
  onLoad: function(options) {
    // options 中的 scene 需要使用 decodeURIComponent 才能获取到生成二维码时传入的 scene
    var scene = decodeURIComponent(options.scene)
  }
})


java源码(main方法生成小程序码)


package com.zw.xcx;

import org.springframework.http.HttpEntity;

import org.springframework.http.HttpMethod;

import org.springframework.http.ResponseEntity;

import org.springframework.util.LinkedMultiValueMap;

import org.springframework.util.MultiValueMap;

import org.springframework.web.client.RestTemplate;

import java.util.HashMap;

import java.util.Map;

import net.sf.json.JSONObject;

import redis.clients.jedis.Jedis;

import java.io.ByteArrayInputStream;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

public class TestPost {

public static void main(String[] args) throws Exception {

// TODO 自动生成的方法存根

GetUrl();

Jedis jedis = new Jedis("localhost");

String access_token = jedis.get("access_token");

System.out.println("access_token is:"+access_token);

getminiqrQr("0736",access_token);

}

public static void GetUrl() throws ClientProtocolException,

IOException {

HttpGet httpGet = new HttpGet(

"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="

+ Xcx.APPID + "&secret="

+ Xcx.SECRET );

HttpClient httpClient = HttpClients.createDefault();

HttpResponse res = httpClient.execute(httpGet);

org.apache.http.HttpEntity entity = res.getEntity();

String result = EntityUtils.toString(entity, "UTF-8");

JSONObject jsons = JSONObject.fromObject(result);

String expires_in = jsons.getString("expires_in");

//缓存

if(Integer.parseInt(expires_in)==7200){

//ok

String access_token = jsons.getString("access_token");

Jedis cache = new Jedis("127.0.0.1", 6379);

System.out.println("access_token:"+access_token);

Pipeline pipeline = cache.pipelined();

pipeline.set("access_token", access_token);

pipeline.expire("access_token", 7200);

pipeline.sync();

}else{

System.out.println("出错获取token失败!");

}

}

public static String GetPostUrl(String access_token,String id) throws Exception {

System.out.println(access_token);

System.out.println(id);

String url ="https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=";

Map<String, Object> map = new HashMap<String, Object>();

map.put("scene", "0736");//你二维码中跳向的地址

map.put("page", "pages/index/index");//你二维码中跳向的地址

map.put("width", "430");//图片大小

JSONObject json = JSONObject.fromObject(map);

System.out.println(json);

return null;

}

public static Map getminiqrQr(String sceneStr, String accessToken) {

RestTemplate rest = new RestTemplate();

InputStream inputStream = null;

OutputStream outputStream = null;

try {

String url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+accessToken;

Map<String,Object> param = new HashMap<>();

param.put("scene", sceneStr);

param.put("page", "pages/index/index");

param.put("width", 430);

param.put("auto_color", false);

Map<String,Object> line_color = new HashMap<>();

line_color.put("r", 0);

line_color.put("g", 0);

line_color.put("b", 0);

param.put("line_color", line_color);

MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();

HttpEntity requestEntity = new HttpEntity(param, headers);

ResponseEntity<byte[]> entity = rest.exchange(url, HttpMethod.POST, requestEntity, byte[].class, new Object[0]);

byte[] result = entity.getBody();

inputStream = new ByteArrayInputStream(result);

File file = new File("D://erweima/"+sceneStr+".png");

if (!file.exists()){

file.createNewFile();

}

outputStream = new FileOutputStream(file);

int len = 0;

byte[] buf = new byte[1024];

while ((len = inputStream.read(buf, 0, 1024)) != -1) {

outputStream.write(buf, 0, len);

}

outputStream.flush();

} catch (Exception e) {

} finally {

if(inputStream != null){

try {

inputStream.close();

} catch (IOException e) {

e.printStackTrace();

}

}

if(outputStream != null){

try {

outputStream.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

return null;

}

}




替换小程序码中间图片


package com.zw.xcx;

import java.awt.Graphics2D;

import java.awt.Image;

import java.awt.RenderingHints;

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.FileInputStream;

import javax.imageio.ImageIO;

public class TestImageChange {

public static void main(String[] args)

{

try

{

BufferedImage appletImg = ImageIO.read(new FileInputStream("D://erweima/applet.png"));

Graphics2D g2d = appletImg.createGraphics();

// 设置抗锯齿的属性

g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

BufferedImage centerImg = ImageIO.read(new File("D://erweima/center.png"));

g2d.drawImage(centerImg.getScaledInstance(centerImg.getWidth(), centerImg.getHeight(), Image.SCALE_SMOOTH), (appletImg.getWidth() - centerImg.getWidth()) / 2, (appletImg.getHeight() - centerImg.getHeight()) / 2, null);

// 关闭资源

g2d.dispose();

ImageIO.write(appletImg, "png", new File("D://erweima/newApplet.png"));

}

catch (Exception e)

{

e.printStackTrace();

}

}

}