>>分享Java Web开发技术,并且对孙卫琴的《Tomcat与Java Web开发技术详解》提供技术支持 书籍支持  卫琴直播  品书摘要  在线测试  资源下载  联系我们
发表一个新主题 开启一个新投票 回复文章 您是本文章第 25414 个阅读者 刷新本主题
 * 贴子主题:  Java二维码生成-谷歌(Google.zxing)开源二维码生成的范例及介绍 回复文章 点赞(0)  收藏  
作者:javathinker    发表时间:2020-03-30 14:21:34     消息  查看  搜索  好友  复制  引用

      
            这里 我们使用  比特矩阵(位矩阵)的QR码编码在缓冲图片上画出二维码

      范例有以下传入 参数

                 OutputStream outputStream, 要存储的文件

                  String content, 携带信息的内容

                 int qrCodeSize, 图片大小

                 String imageFormat 编码

             步骤:

             1.设置二维码的纠错级别参数
      // 设置二维码纠错级别MAP

       Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap =  
                                      new Hashtable<EncodeHintType, ErrorCorrectionLevel> ();

       hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);  //  矫错级别

                  2.创建比特矩阵

1 QRCodeWriter qrCodeWriter =  new  QRCodeWriter();

2  // 创建比特矩阵(位矩阵)的QR码编码的字符串

3 BitMatrix byteMatrix =  
                        qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, qrCodeSize, qrCodeSize, hintMap);

4  //  使BufferedImage勾画QRCode (matrixWidth 是行二维码像素点)

5  int matrixWidth = byteMatrix.getWidth();

               3.开始在缓冲图片中画二维码

  1              BufferedImage image =  new BufferedImage(matrixWidth, matrixWidth , BufferedImage.TYPE_INT_RGB);  

  2              image.createGraphics();  

  3             Graphics2D graphics =  (Graphics2D) image.getGraphics();  

  4              graphics.setColor(Color.WHITE);  

  5             graphics.fillRect(0, 0 , matrixWidth, matrixWidth);  

  6              //  使用比特矩阵画并保存图像

  7              graphics.setColor(Color.BLACK);  

  8              for ( int i = 0; i < matrixWidth; i++ ){

  9                  for ( int j = 0; j < matrixWidth; j++ ){

10                      if  (byteMatrix.get(i, j)){

11                         graphics.fillRect(i, j, 1, 1 );  

12                      }

13                  }

14              }

15             ImageIO.write(image, imageFormat, outputStream);  

                二维码生成的工具类代码:

1  package  。。。;

  2

  3  import  java.awt.Color;

  4  import  java.awt.Graphics2D;

  5  import  java.awt.image.BufferedImage;

  6  import  java.io.File;

  7  import  java.io.FileInputStream;

  8  import  java.io.FileOutputStream;

  9  import  java.io.IOException;

10  import  java.io.InputStream;

11  import  java.io.OutputStream;

12  import  java.util.Hashtable;

13

14  import  javax.imageio.ImageIO;

15

16  import  com.google.zxing.BarcodeFormat;

17  import  com.google.zxing.BinaryBitmap;

18  import  com.google.zxing.EncodeHintType;

19  import  com.google.zxing.LuminanceSource;

20  import  com.google.zxing.ReaderException;

21  import  com.google.zxing.Result;

22  import  com.google.zxing.WriterException;

23  import  com.google.zxing.client.j2se.BufferedImageLuminanceSource;

24  import  com.google.zxing.common.BitMatrix;

25  import  com.google.zxing.common.HybridBinarizer;

26  import  com.google.zxing.qrcode.QRCodeReader;

27  import  com.google.zxing.qrcode.QRCodeWriter;

28  import  com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

29

30  /**

31   * 二维码生成和读的工具类

32   *

33   */


34  public  class  QrCodeCreateUtil {

35    

36      /**

37       * 生成包含字符串信息的二维码图片

38       *  @param  outputStream 文件输出流路径

39       *  @param  content 二维码携带信息

40       *  @param  qrCodeSize 二维码图片大小

41       *  @param  imageFormat 二维码的格式

42       *  @throws  WriterException

43       *  @throws  IOException

44       */


45      public  static  boolean createQrCode(OutputStream outputStream, String content,  int qrCodeSize, String imageFormat)  throws  WriterException, IOException{  

46              // 设置二维码纠错级别MAP

47             Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap =  new Hashtable<EncodeHintType, ErrorCorrectionLevel> ();  

48             hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);   //  矫错级别  

49             QRCodeWriter qrCodeWriter =  new  QRCodeWriter();  

50              // 创建比特矩阵(位矩阵)的QR码编码的字符串  

51             BitMatrix byteMatrix =  qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, qrCodeSize, qrCodeSize, hintMap);  

52              //  使BufferedImage勾画QRCode  (matrixWidth 是行二维码像素点)

53              int matrixWidth =  byteMatrix.getWidth();  

54             BufferedImage image =  new BufferedImage(matrixWidth-200, matrixWidth-200 , BufferedImage.TYPE_INT_RGB);  

55              image.createGraphics();  

56             Graphics2D graphics =  (Graphics2D) image.getGraphics();  

57              graphics.setColor(Color.WHITE);  

58             graphics.fillRect(0, 0 , matrixWidth, matrixWidth);  

59              //  使用比特矩阵画并保存图像

60              graphics.setColor(Color.BLACK);  

61              for ( int i = 0; i < matrixWidth; i++ ){

62                  for ( int j = 0; j < matrixWidth; j++ ){

63                      if  (byteMatrix.get(i, j)){

64                         graphics.fillRect(i-100, j-100, 1, 1 );  

65                      }

66                  }

67              }

68              return  ImageIO.write(image, imageFormat, outputStream);  

69      }  

70      

71      /**

72       * 读二维码并输出携带的信息

73       */


74      public  static  void readQrCode(InputStream inputStream)  throws  IOException{  

75          // 从输入流中获取字符串信息

76         BufferedImage image =  ImageIO.read(inputStream);  

77          // 将图像转换为二进制位图源

78         LuminanceSource source =  new  BufferedImageLuminanceSource(image);  

79         BinaryBitmap bitmap =  new BinaryBitmap( new  HybridBinarizer(source));  

80         QRCodeReader reader =  new  QRCodeReader();  

81         Result result =  null  ;  

82          try  {

83          result =  reader.decode(bitmap);  

84         }  catch  (ReaderException e) {

85              e.printStackTrace();  

86          }

87          System.out.println(result.getText());  

88      }

89      /**

90       * 测试代码

91       *  @throws  WriterException

92       */


93      public  static  void main(String[] args)  throws  IOException, WriterException {  

94        

95         createQrCode( new FileOutputStream( new File("d:\\qrcode.jpg")),
                  "WE1231238239128sASDASDSADSDWEWWREWRERWSDFDFSDSDF123123123123213123",900,"JPEG" );

96         readQrCode( new FileInputStream( new File("d:\\qrcode.jpg" )));  

97      }  

98  

99 }  

   要是文章对你有用,随手点个赞哦

                      
----------------------------
原文链接:https://www.cnblogs.com/lanxiamo/p/6293580.html

程序猿的技术大观园:www.javathinker.net



[这个贴子最后由 sunweiqin 在 2020-03-31 10:25:45 重新编辑]
  Java面向对象编程-->Swing组件(下)
  JavaWeb开发-->Web运作原理(Ⅰ)
  JSP与Hibernate开发-->映射一对多关联关系
  Java网络编程-->RMI框架
  精通Spring-->
  Vue3开发-->通过Vuex进行状态管理
  程序员:我终于知道HTTP的post和get请求方式的区别
  HTTP 状态代码所对应的消息
  Servlet 网页重定向
  Json格式实现数据传输
  详解在Linux系统中安装Tomcat
  用过滤器对返回的响应结果进行修改
  HTTP和HTTPS的请求和响应
  HTTP请求响应过程与HTTPS区别
  java生成图片验证码--封装生成图片验证码的工具类
  tomcat+nginx域名配置方法
  读《Tomcat与JavaWeb开发技术详解》的笔记
  深入解读HTTP Session
  web.xml中配置出错页面,处理异常
  JavaWeb视图层技术对比:jsp、freemarker、velocity
  在JSP中用JavaScript实现倒计时和自动跳转
  更多...
 IPIP: 已设置保密
楼主      
1页 0条记录 当前第1
发表一个新主题 开启一个新投票 回复文章


中文版权所有: JavaThinker技术网站 Copyright 2016-2026 沪ICP备16029593号-2
荟萃Java程序员智慧的结晶,分享交流Java前沿技术。  联系我们
如有技术文章涉及侵权,请与本站管理员联系。