小程序动态生成二维码,生成image图片
2018-01-22 09:45
302 查看
前端:
<imagesrc="{{img_usrl}}"style="width:100%;height:104px;"bindlongtap="saveImg"></image>
js部分:在onLoad中请求
//我的userid值为:239a3c37-3c2e-4a9d-be74-557638b23b63
this.setData({img_usrl:getApp().getBaseUrl()+"/icon/"+userid});
java后台:
工具QRCodeUtli类:
pom文件需要引用的包:
<imagesrc="{{img_usrl}}"style="width:100%;height:104px;"bindlongtap="saveImg"></image>
js部分:在onLoad中请求
//我的userid值为:239a3c37-3c2e-4a9d-be74-557638b23b63
this.setData({img_usrl:getApp().getBaseUrl()+"/icon/"+userid});
java后台:
@RequestMapping("icon/{cateogry}") publicvoidgetQrcodeImg(@PathVariable("cateogry")Stringcateogry, HttpServletRequestrequest, HttpServletResponseresponse){ try{ //cateogry的值为前端请求的值:239a3c37-3c2e-4a9d-be74-557638b23b63 Stringurl="固定写死的url路径?id参数="+cateogry; BufferedImageimage=QRCodeUtli.getqrcode(url); response.setContentType("image/png"); OutputStreamstream=response.getOutputStream(); ByteArrayOutputStreamout=newByteArrayOutputStream(); ImageIO.write(image,"png",out); stream.write(out.toByteArray()); stream.flush(); stream.close(); }catch(Exceptione){ e.printStackTrace(); } }
工具QRCodeUtli类:
packagecom.early.api.util; importjava.awt.image.BufferedImage; importjava.util.HashMap; importjava.util.Map; importcom.google.zxing.BarcodeFormat; importcom.google.zxing.EncodeHintType; importcom.google.zxing.MultiFormatWriter; importcom.google.zxing.WriterException; importcom.google.zxing.common.BitMatrix; publicclassQRCodeUtli{ privatestaticfinalintBLACK=0xFF000000; privatestaticfinalintWHITE=0xFFFFFFFF; publicstaticBufferedImagegetqrcode(Stringcode){ intwidth=400;//图像宽度 intheight=400;//图像高度 Map<EncodeHintType,Object>hints=newHashMap<EncodeHintType,Object>(); hints.put(EncodeHintType.MARGIN,0);//设置白边宽度,取值0~4 hints.put(EncodeHintType.CHARACTER_SET,"UTF-8"); try{ BitMatrixbitMatrix=newMultiFormatWriter().encode(code, BarcodeFormat.QR_CODE,width,height,hints); BufferedImageimage=toBufferedImage(bitMatrix); returnimage; }catch(WriterExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); } returnnull; } publicstaticBufferedImagetoBufferedImage(BitMatrixmatrix){ intwidth=matrix.getWidth(); intheight=matrix.getHeight(); BufferedImageimage=newBufferedImage(width,height, BufferedImage.TYPE_INT_RGB); for(intx=0;x<width;x++){ for(inty=0;y<height;y++){ image.setRGB(x,y,matrix.get(x,y)?BLACK:WHITE); } } returnimage; } }
pom文件需要引用的包:
<dependency> <groupId>com.google.zxing</groupId> <artifactId>core</artifactId> <version>3.3.1</version> </dependency>
相关文章推荐
- 小程序从本地相册选择图片或使用相机拍照wx.chooseImage上传后预览并可以识别图中二维码
- javascript动态生成网址二维码图片
- 实战 HTTP 处理程序(HTTP Handler) -- 动态生成图片
- .NET开发微信小程序-生成二维码/上传图片到服务器
- 微信小程序动态生成二维码
- php小程序二维码获取生成图片分享
- 微信小程序将字符串生成二维码图片的操作方法
- 动态生成二维码图片后通过js(JavaScript)或jq实现网页图片转base64格式下载
- JavaScript动态生成二维码图片
- 生成二维码图片 MatrixToImageWriter .java
- js动态生成二维码图片
- PHP版 微信小程序生成带参数小程序二维码图片 php接口+小程序demo
- 实战 HTTP 处理程序(HTTP Handler) (3) -- 动态生成图片
- iOS根据字符使用滤镜动态生成二维码 重绘图片增加二维码的清晰度
- Handler一般处理程序 -- 动态生成图片 (转载)
- C#利用QRCode动态生成自定义二维码图片
- js动态生成二维码图片
- 微信小程序动态生成二维码的实现代码
- 芝麻动态码-小程序动态二维码生成
- 实战 HTTP 处理程序(HTTP Handler) (3) -- 动态生成图片