需求是公司要给后台发布的海报,每个人看到的时候吧自己的小程序二维码拼接上去
上代码
获取小程序二维码
String url="https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=ACCESS_TOKEN";
Map<String,Object> param=new HashMap<String, Object>();
if(sysEmployee==null){
param.put("scene","0");
}else{
带上自己的参数
param.put("scene",""+sysEmployee.getEmployeeId());
}
param.put("page","pages/home/home");
param.put("width","350px");
//param.put("auto_color","false");
//param.put("lineColor","{"r":0,"g":0,"b":0}");
//param.put("is_hyaline",true);是否透明,建议还是白底,不然拼接上很有可能识别不了
url=url.replace("ACCESS_TOKEN", accessToken);
byte[] result =HttpUtil.postReturnBytes(url, null, JSONObject.toJSONString(param),null);
return result;
java代码拼接图片
response.setContentType("text/html; charset=UTF-8");//设置返回数据的格式
response.setContentType("image/jpeg");
//获取图片的url
OutputStream os = response.getOutputStream();
try {
byte[] bigByte= bigByte;
byte[] smallByte=smallByte;
ByteArrayInputStream bigIn = new ByteArrayInputStream(bigByte); //将大图片作为输入流;
ByteArrayInputStream smallIn = new ByteArrayInputStream(smallByte); //将二维码作为输入流;
BufferedImage big = ImageIO.read(bigIn);
BufferedImage small = ImageIO.read(smallIn);
//不重新绘制,会报invalid argument to native writeimage,重新绘制后就可以了
BufferedImage tag;
tag = new BufferedImage(big.getWidth(), big.getHeight(), BufferedImage.TYPE_INT_BGR);
Graphics gg = tag.getGraphics();
gg.drawImage(big, 0, 0, null); // 绘制缩小后的图
gg.dispose();
big = tag;
//开始拼接图片
Graphics g = big.getGraphics();
int x = big.getWidth()-big.getWidth()*postersAndShareImg.getPasiShareX()/100;
int y = big.getHeight()-big.getHeight()*postersAndShareImg.getPasiShareY()/100;
g.drawImage(small, x, y, 50, 50, null);
g.dispose();
ImageIO.write(big, "jpeg", os);
} catch (Exception e) {
e.printStackTrace();
}
小程序端的点击图片,预览功能,可以长按发送给朋友或者保存到相册
previewImageAction:function(e){
var url=e.currentTarget.dataset.url;
wx.previewImage({
urls: [url],
complete: (res) => {},
current: 'current',
fail: (res) => {},
success: (res) => {},
})
}