生成小程序二维码

生成小程序二维码

引入依赖
<dependency>
	<groupId>com.twelvemonkeys.imageio</groupId>
	<artifactId>imageio-jpeg</artifactId>
	<version>3.0-rc5</version>
</dependency>

接口C:普通方形二维码

//接口A:生成普通菊花小程序带参数二维码,适用于需要的码数量较少的业务场景	数量限制是10万个
//接口C:普通方形二维码,适用于需要的码数量较少的业务场景,接口C和接口A的二维码总数量是10万个
//获取token
https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=appKey&secret=secretKey

String result = HttpClient4.doGet("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=appKey&secret=secretKey");
Map map = (Map) JSON.parse(result);
String access_token = StringUtils.getStringByObj(map.get("access_token"));
String url ="https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=";
Map<String, Object> createMap = new HashMap<>();
createMap.put("path", "pages/clodRecord/index/index?traceCode="+URLEncoder.encode(record.getQrCode()));//你二维码中跳向的地址
createMap.put("width", "430");//图片大小
Object json = JSON.toJSON(createMap);
String  res = this.httpPostWithJSON(url + access_token, json.toString(),URLEncoder.encode(record.getQrCode()));
//二维码添加logo和文本
String result1 = this.changeImage(res,record.getProcName() + " NO." + record.getProductionBach());
//给前台返回base64格式图片进行图片展示
renderJson(ErrCode.retData(ErrCode.SUCCESS, "data:image/png;base64,"+result1));


public String httpPostWithJSON(String url, String json,String id)
		throws Exception {
	DefaultHttpClient httpClient = new DefaultHttpClient();
	HttpPost httpPost = new HttpPost(url);
	httpPost.addHeader(HTTP.CONTENT_TYPE, "application/json");
	StringEntity se = new StringEntity(json);
	se.setContentType("application/json");
	se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"UTF-8"));
	httpPost.setEntity(se);
	HttpResponse response = httpClient.execute(httpPost);
	String uploadSysUrl = super.getRequest().getServletContext().getRealPath("")+"/tempQrCode/";
	if (response != null) {
		HttpEntity resEntity = response.getEntity();
		if (resEntity != null) {
			InputStream instreams = resEntity.getContent();
			System.out.println("uploadSysUrl="+uploadSysUrl);
			File saveFile = new File(uploadSysUrl+id+".png");
			// 判断这个文件(saveFile)是否存在
			if (!saveFile.getParentFile().exists()) {
				// 如果不存在就创建这个文件夹
				saveFile.getParentFile().mkdirs();
			}
			saveToImgByInputStream(instreams, uploadSysUrl, id+".png");
		}
	}
	httpPost.abort();
	return uploadSysUrl+id+".png";
}
/* @param instreams 二进制流
	 * @param imgPath 图片的保存路径
	 * @param imgName 图片的名称
	 * @return
	 *      1:保存正常
	 *      0:保存失败
	 */
public static int saveToImgByInputStream(InputStream instreams,String imgPath,String imgName){
	int stateInt = 1;
	if(instreams != null){
		try {
			File file=new File(imgPath+imgName);//可以是任何图片格式.jpg,.png等
			FileOutputStream fos=new FileOutputStream(file);
			byte[] b = new byte[1024];
			int nRead = 0;
			while ((nRead = instreams.read(b)) != -1) {
				fos.write(b, 0, nRead);
			}
			fos.flush();
			fos.close();
		} catch (Exception e) {
			stateInt = 0;
			e.printStackTrace();
		} finally {
			try {
				instreams.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	return stateInt;
}
/**
 * 二维码添加logo和文本
 * @param imageurl
 * @param text
 * @return
 */
public String changeImage(String imageurl,String text ){
	try {
		String logoPath = super.getRequest().getServletContext().getRealPath("/img") + "\\icon.png";
		InputStream imagein = new FileInputStream(logoPath);
		InputStream imagein2 = new FileInputStream(imageurl);
		BufferedImage image = ImageIO.read(imagein);
		System.out.println("logoPath="+image.getHeight()+"==||=="+image.getWidth());
		BufferedImage image2 = ImageIO.read(imagein2);
		System.out.println("imageurl="+image2.getHeight()+"==||=="+image2.getWidth());
		Graphics g = image2.getGraphics();
		g.drawImage(image, image2.getWidth()/2-60,image2.getHeight()/2-90,new Double(image2.getWidth()/4).intValue(),new Double(image2.getHeight()/4.5).intValue(),null);
		OutputStream outImage = new FileOutputStream(imageurl);
		JPEGImageEncoder enc = JPEGCodec.createJPEGEncoder(outImage);
		enc.encode(image2);
		BufferedImage bimg=ImageIO.read(new FileInputStream(imageurl));
		image2 = this.withLogo(bimg, text);
		String base64Data = new String(Base64.getEncoder().encode(QRCodeUtils.getByte(image2)));
		imagein.close();
		imagein2.close();
		outImage.close();
		return base64Data;
	} catch (Exception e) {
		e.printStackTrace();
	}
	return "";
}

接口B 生成的二维码

//接口B  生成的二维码  样式为菊花图
Map<String, Object> paramMap = new HashMap<String, Object>();
paramMap.put("scene", reqJson.getString("traceCode"));
paramMap.put("scene", record.getQrCode());
paramMap.put("path", "pages/clodRecord/index/index");
paramMap.put("page", "pages/clodRecord/index/index");
paramMap.put("width", 430);
paramMap.put("auto_color", false);
CloseableHttpClient client = HttpClients.createDefault();
HttpPost post = new HttpPost("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + access_token);
post.setEntity(new StringEntity(JSON.toJSONString(paramMap), ContentType.APPLICATION_JSON));
CloseableHttpResponse response = client.execute(post);
byte[] bytes = EntityUtils.toByteArray(response.getEntity());
response.close();
String base64Data = new String(Base64.getEncoder().encode(bytes));
renderJson(ErrCode.retData(ErrCode.SUCCESS, "data:image/png;base64," + base64Data));
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值