微信小程序> 小程序怎么分享二维码-微信小程序生成分享带参数二维码图片并添加文字功能-小程序二维码

小程序怎么分享二维码-微信小程序生成分享带参数二维码图片并添加文字功能-小程序二维码

浏览量:2102 时间: 来源:专业的程序员

1.笔者最近接到一个新的任务,不是很难的功能,就是之前没有接触过,后端生成带参数的小程序二维码图片,并在图片下面添加一些文字。想在将代码分享给大家,期望可以给大家提供帮助。

2.一、首先生成小程序的分享二维码有三种方式

接口A:适用于需要的码数量较少的业务场景生成小程序码,可接受path参数较长,生成个数受限。接口B:适用于需要的码数量极多的业务场景生成小程序码,可接受页面参数较短,生成个数不受限。接口C:适用于需要的码数量较少的业务场景生成二维码,可接受path参数较长,生成个数受限。

3.具体的情况请参照https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/qr-code.html

4.二、笔者使用第一种方式来生成分享二维码

5.1、第一步,获取access_token

privateStringgetAccessToken(){Stringresult=null;try{//自己的appid和secretresult=HttpUtil.get("https://api.weixin.qq.com/cgi-bin/token"+"?grant_type=client_credential&appid=yourappId&secret=yoursecret",null,null);}catch(Exceptione){e.printStackTrace();}JSONObjectjsonObject=JSON.parseObject(result);result=jsonObject.getString("access_token");System.out.println(result);returnresult;}

6.2、第二步,请求分享二维码的二进制流

StringURL="https://api.weixin.qq.com/wxa/getwxacode?access_token="+accessToken;JSONObjectjsonObject=newJSONObject();jsonObject.put("path","pages/lsjindex/lsjindex");InputStreaminputStream=null;try{inputStream=HttpUtil.postStream(URL,jsonObject.toJSONString(),null);}catch(Exceptione){e.printStackTrace();}BufferedImagebi=ImageIO.read(inputStream);

7.3、拼接文字

publicBufferedImagecreateNewPic(Stringtitle,BufferedImagebi){BufferedImageimage=newBufferedImage(500,550,BufferedImage.TYPE_INT_RGB);//设置图片的背景色Graphics2Dmain=image.createGraphics();main.setColor(Color.white);main.fillRect(0,0,500,550);//***********************插入中间广告图GraphicsmainPic=image.getGraphics();if(logo!=null){mainPic.drawImage(logo,40,40,400,400,null);mainPic.dispose();}//***********************页面底部文字GraphicstitleG=image.createGraphics();//设置区域颜色//titleG.setColor(Color.white);//填充区域并确定区域大小位置//titleG.fillRect(450,50,450,50);//设置字体颜色,先设置颜色,再填充内容titleG.setColor(Color.BLACK);//设置字体FonttitleFont=newFont("宋体",Font.BOLD,14);titleG.setFont(titleFont);titleG.drawString(title,200,500);returnimage;}

8.4、最后生成图片并保存

FiletargetFile=newFile("1.png");ImageIO.write(image,"png",targetFile);

9.完成的代码如下:

publicclassSmallAppTest{publicstaticvoidmain(String[]args)throwsIOException{SmallAppTesttest=newSmallAppTest();//获得access_tokenStringaccessToken=test.getAccessToken();//获得小程序二维码//POSThttps://api.weixin.qq.com/wxa/getwxacode?access_token=ACCESS_TOKENStringURL="https://api.weixin.qq.com/wxa/getwxacode?access_token="+accessToken;JSONObjectjsonObject=newJSONObject();jsonObject.put("path","pages/lsjindex/lsjindex");InputStreaminputStream=null;try{inputStream=HttpUtil.postStream(URL,jsonObject.toJSONString(),null);}catch(Exceptione){e.printStackTrace();}BufferedImagebi=ImageIO.read(inputStream);BufferedImageimage=test.createNewPic("测试",bi);//加文字FiletargetFile=newFile("1.png");ImageIO.write(image,"png",targetFile);//关闭流inputStream.close();}publicBufferedImagecreateNewPic(Stringtitle,BufferedImagelogo){BufferedImageimage=newBufferedImage(500,550,BufferedImage.TYPE_INT_RGB);//设置图片的背景色Graphics2Dmain=image.createGraphics();main.setColor(Color.white);main.fillRect(0,0,500,550);//***********************插入中间广告图GraphicsmainPic=image.getGraphics();if(logo!=null){mainPic.drawImage(logo,40,40,400,400,null);mainPic.dispose();}//***********************页面头部GraphicstitleG=image.createGraphics();//设置区域颜色//titleG.setColor(Color.white);//填充区域并确定区域大小位置//titleG.fillRect(450,50,450,50);//设置字体颜色,先设置颜色,再填充内容titleG.setColor(Color.BLACK);//设置字体FonttitleFont=newFont("宋体",Font.BOLD,14);titleG.setFont(titleFont);titleG.drawString(title,200,500);returnimage;}privateStringgetAccessToken(){Stringresult=null;try{result=HttpUtil.get("https://api.weixin.qq.com/cgi-bin/token"+"?grant_type=client_credential&appid=appId&secret=secret",null,null);}catch(Exceptione){e.printStackTrace();}JSONObjectjsonObject=JSON.parseObject(result);result=jsonObject.getString("access_token");System.out.println(result);returnresult;}}

10.用到的工具类:Http工具类

11.https://download.csdn.net/download/money9sun/10973850

12.结果图

13.

14.推荐:史上最全的java开发工具类地址:https://github.com/EricLoveMia/JavaTools

版权声明

即速应用倡导尊重与保护知识产权。如发现本站文章存在版权问题,烦请提供版权疑问、身份证明、版权证明、联系方式等发邮件至197452366@qq.com ,我们将及时处理。本站文章仅作分享交流用途,作者观点不等同于即速应用观点。用户与作者的任何交易与本站无关,请知悉。

产品经理

手机 : 13312967497

擅长 : 小程序流量变现

扫码领取礼包

最新资讯

热门模板

  • 头条
  • 搜狐
  • 微博
  • 百家
  • 一点资讯
  • 知乎