首页 技术 正文
技术 2022年11月15日
0 收藏 611 点赞 4,288 浏览 9484 个字

二维码分为好多种,我们最常用的是qrcode类型的二维码,以下有三种生成方式以及解析方式:

附所需jar包或者js地址

第一种:依赖qrcode.jar

  1. import java.awt.Color;
  2. import java.awt.Graphics2D;
  3. import java.awt.image.BufferedImage;
  4. import java.io.File;
  5. import java.io.UnsupportedEncodingException;
  6. import javax.imageio.ImageIO;
  7. import com.swetake.util.Qrcode;
  8. /**获取qrcode jar包 https://osdn.jp/projects/qrcode
  9. * 生成qrcode二维码
  10. * @author fangyi
  11. *
  12. */
  13. public class CreateQrCode {
  14. public static void main(String[] args) throws Exception {
  15. Qrcode qrcode= new Qrcode();
  16. qrcode.setQrcodeErrorCorrect('M');//纠错等级
  17. qrcode.setQrcodeEncodeMode('B');//N代表数字 A代表字母 B代表其他
  18. qrcode.setQrcodeVersion(7);//版本
  19. String qrData ="www.baidu.com";//二维码信息内容
  20. int width=67+12*(7-1);
  21. int height=67+12*(7-1);
  22. BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  23. //画图工具
  24. Graphics2D gs= bufferedImage.createGraphics();
  25. //设置属性
  26. gs.setBackground(Color.WHITE);
  27. gs.setColor(Color.BLACK);
  28. gs.clearRect(0, 0, width, height);
  29. //填充
  30. int pixoff=2;//偏移量
  31. byte[] d = qrData.getBytes("gb2312");//编码防止有中文
  32. if (0 < d.length && d.length<120) {
  33. boolean[][] s = qrcode.calQrcode(d);
  34. for (int i = 0; i < s.length; i++) {
  35. for (int j = 0; j < s.length; j++) {
  36. if(s[j][i]){
  37. gs.fillRect(i*3+pixoff, j*3+pixoff, 3, 3);
  38. }
  39. }
  40. }
  41. }
  42. //关闭资源,并保存文件
  43. gs.dispose();
  44. bufferedImage.flush();
  45. ImageIO.write(bufferedImage, "png", new File("D:/qrcode.jpg"));//输出到指定地方
  46. }
  47. }

读取指定的二维码:

  1. import java.awt.image.BufferedImage;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import javax.imageio.ImageIO;
  5. import jp.sourceforge.qrcode.QRCodeDecoder;
  6. /**
  7. * 读取制定路径下的qrcode信息
  8. * @author fangyi
  9. *
  10. */
  11. public class ReaderQrcode {
  12. public static void main(String[] args) throws IOException {
  13. //指定的二维码文件路径
  14. File file= new File("D:/jqueryqrcode.png");
  15. BufferedImage bufferedImage = ImageIO.read(file);
  16. QRCodeDecoder qrCodeDecoder = new QRCodeDecoder();
  17. String result = new String(qrCodeDecoder.decode(new MyQRCodeImage(bufferedImage)),"gb2312");
  18. //输出结果
  19. System.out.println(result);
  20. bufferedImage.flush();
  21. }
  22. }
  23. import java.awt.image.BufferedImage;
  24. import jp.sourceforge.qrcode.data.QRCodeImage;
  25. /**
  26. * 自定义QRCodeImage类实现QRCodeImage 供读取时所用
  27. * @author fangyi
  28. *
  29. */
  30. public class MyQRCodeImage implements QRCodeImage {
  31. BufferedImage bufferedImage ;
  32. public MyQRCodeImage(BufferedImage bufferedImage) {
  33. super();
  34. this.bufferedImage = bufferedImage;
  35. }
  36. @Override
  37. public int getHeight() {
  38. return bufferedImage.getHeight();
  39. }
  40. @Override
  41. public int getPixel(int arg0, int arg1) {
  42. return bufferedImage.getRGB(arg0, arg1);
  43. }
  44. @Override
  45. public int getWidth() {
  46. return bufferedImage.getWidth();
  47. }
  48. }

第二种:创建动态的web工程 依赖jquery.qrcode.min.js   jquery.js  运行工程访问下面jsp就可以了

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7. <title>生成二维码</title>
  8. <!-- js地址https://github.com/jeromeetienne/jquery-qrcode -->
  9. <script type="text/javascript" src="<%=request.getContextPath() %>/js/jquery.min.js"></script>
  10. <script type="text/javascript" src="<%=request.getContextPath() %>/js/jquery.qrcode.min.js"></script>
  11. </head>
  12. <body>
  13. 生成二维码如下:<br><br><br>
  14. <div id="qrcode"></div>
  15. <script type="text/javascript">
  16. $('#qrcode').qrcode("www.baidu.com"); //生产二维码
  17. /* jquery('#qrcode').qrcode({width: 64,height: 64,text: "size doesn't matter"}); */
  18. </script>
  19. </body>
  20. </html>

第三种 创建Java project  依赖zxing3.2.1.jar

  1. import java.io.File;
  2. import java.io.IOException;
  3. import java.util.Hashtable;
  4. import com.google.zxing.BarcodeFormat;
  5. import com.google.zxing.EncodeHintType;
  6. import com.google.zxing.MultiFormatWriter;
  7. import com.google.zxing.WriterException;
  8. import com.google.zxing.common.BitMatrix;
  9. import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
  10. /**
  11. * zxing jar包地址 https://github.com/Candy1575/QRcode
  12. * 依赖MatrixToImageWriter类
  13. * @author fangyi
  14. *
  15. */
  16. public class EncodeTest {
  17. public static void Encode_QR_CODE() throws IOException, WriterException{
  18. String contents = "ZXing 二维码内容1234!"; // 二维码内容
  19. int width = 430; // 二维码图片宽度 300
  20. int height = 430; // 二维码图片高度300
  21. String format = "gif";// 二维码的图片格式 gif
  22. Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
  23. // 指定纠错等级,纠错级别(L 7%、M 15%、Q 25%、H 30%)
  24. hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
  25. // 内容所使用字符集编码
  26. hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
  27. // hints.put(EncodeHintType.MAX_SIZE, 350);//设置图片的最大值
  28. // hints.put(EncodeHintType.MIN_SIZE, 100);//设置图片的最小值
  29. hints.put(EncodeHintType.MARGIN, 1);//设置二维码边的空度,非负数
  30. BitMatrix bitMatrix = new MultiFormatWriter().encode(contents,//要编码的内容
  31. //编码类型,目前zxing支持:Aztec 2D,CODABAR 1D format,Code 39 1D,Code 93 1D ,Code 128 1D,
  32. //Data Matrix 2D , EAN-8 1D,EAN-13 1D,ITF (Interleaved Two of Five) 1D,
  33. //MaxiCode 2D barcode,PDF417,QR Code 2D,RSS 14,RSS EXPANDED,UPC-A 1D,UPC-E 1D,UPC/EAN extension,UPC_EAN_EXTENSION
  34. BarcodeFormat.QR_CODE,
  35. width, //条形码的宽度
  36. height, //条形码的高度
  37. hints);//生成条形码时的一些配置,此项可选
  38. // 生成二维码
  39. File outputFile = new File("D:" + File.separator + "mycode.gif");//指定输出路径
  40. MatrixToImageWriter.writeToFile(bitMatrix, format, outputFile);
  41. }
  42. public static void main(String[] args) throws Exception {
  43. try {
  44. Encode_QR_CODE();
  45. } catch (Exception e) {
  46. // TODO: handle exception
  47. e.printStackTrace();
  48. }
  49. }
  50. }
  51. import java.awt.image.BufferedImage;
  52. import java.io.File;
  53. import java.io.IOException;
  54. import java.io.OutputStream;
  55. import javax.imageio.ImageIO;
  56. import com.google.zxing.common.BitMatrix;
  57. /**
  58. * 二维码的生成需要借助MatrixToImageWriter类,
  59. * 该类是由Google提供的,可以将该类直接拷贝到源码中使用,当然你也可以自己写个
  60. * 生产条形码的基类 依赖LogoConfig类
  61. */
  62. public class MatrixToImageWriter {
  63. private static final int BLACK = 0xFF000000;//用于设置图案的颜色
  64. private static final int WHITE = 0xFFFFFFFF; //用于背景色
  65. private MatrixToImageWriter() {
  66. }
  67. public static BufferedImage toBufferedImage(BitMatrix matrix) {
  68. int width = matrix.getWidth();
  69. int height = matrix.getHeight();
  70. BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  71. for (int x = 0; x < width; x++) {
  72. for (int y = 0; y < height; y++) {
  73. image.setRGB(x, y, (matrix.get(x, y) ? BLACK : WHITE));
  74. // image.setRGB(x, y, (matrix.get(x, y) ? Color.YELLOW.getRGB() : Color.CYAN.getRGB()));
  75. }
  76. }
  77. return image;
  78. }
  79. public static void writeToFile(BitMatrix matrix, String format, File file) throws IOException {
  80. BufferedImage image = toBufferedImage(matrix);
  81. //设置logo图标
  82. LogoConfig logoConfig = new LogoConfig();
  83. image = logoConfig.LogoMatrix(image);
  84. if (!ImageIO.write(image, format, file)) {
  85. throw new IOException("Could not write an image of format " + format + " to " + file);
  86. }else{
  87. System.out.println("图片生成成功!");
  88. }
  89. }
  90. public static void writeToStream(BitMatrix matrix, String format, OutputStream stream) throws IOException {
  91. BufferedImage image = toBufferedImage(matrix);
  92. //设置logo图标
  93. LogoConfig logoConfig = new LogoConfig();
  94. image = logoConfig.LogoMatrix(image);
  95. if (!ImageIO.write(image, format, stream)) {
  96. throw new IOException("Could not write an image of format " + format);
  97. }
  98. }
  99. }
  100. import java.awt.BasicStroke;
  101. import java.awt.Color;
  102. import java.awt.Graphics2D;
  103. import java.awt.geom.RoundRectangle2D;
  104. import java.awt.image.BufferedImage;
  105. import java.io.File;
  106. import java.io.IOException;
  107. import javax.imageio.ImageIO;
  108. /**
  109. * 二维码 添加自定义 logo图标 处理的方法,
  110. * 模仿微信自动生成二维码效果,有圆角边框,logo和二维码间有空白区,logo带有灰色边框
  111. * @author fangyi
  112. *
  113. */
  114. public class LogoConfig {
  115. /**
  116. * 设置 logo
  117. * @param matrixImage 源二维码图片
  118. * @return 返回带有logo的二维码图片
  119. * @throws IOException
  120. * @author Administrator sangwenhao
  121. */
  122. public BufferedImage LogoMatrix(BufferedImage matrixImage) throws IOException{
  123. /**
  124. * 读取二维码图片,并构建绘图对象
  125. */
  126. Graphics2D g2 = matrixImage.createGraphics();
  127. int matrixWidth = matrixImage.getWidth();
  128. int matrixHeigh = matrixImage.getHeight();
  129. /**
  130. * 读取Logo图片
  131. */
  132. BufferedImage logo = ImageIO.read(new File("C:\\Users\\fangyi\\Pictures\\code-wallpaper-18.png"));
  133. //开始绘制图片
  134. g2.drawImage(logo,matrixWidth/5*2,matrixHeigh/5*2, matrixWidth/5, matrixHeigh/5, null);//绘制
  135. BasicStroke stroke = new BasicStroke(5,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);
  136. g2.setStroke(stroke);// 设置笔画对象
  137. //指定弧度的圆角矩形
  138. RoundRectangle2D.Float round = new RoundRectangle2D.Float(matrixWidth/5*2, matrixHeigh/5*2, matrixWidth/5, matrixHeigh/5,20,20);
  139. g2.setColor(Color.white);
  140. g2.draw(round);// 绘制圆弧矩形
  141. //设置logo 有一道灰色边框
  142. BasicStroke stroke2 = new BasicStroke(1,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);
  143. g2.setStroke(stroke2);// 设置笔画对象
  144. RoundRectangle2D.Float round2 = new RoundRectangle2D.Float(matrixWidth/5*2+2, matrixHeigh/5*2+2, matrixWidth/5-4, matrixHeigh/5-4,20,20);
  145. g2.setColor(new Color(128,128,128));
  146. g2.draw(round2);// 绘制圆弧矩形
  147. g2.dispose();
  148. matrixImage.flush() ;
  149. return matrixImage ;
  150. }
  151. }

读取二维码信息

  1. import java.awt.image.BufferedImage;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6. import javax.imageio.ImageIO;
  7. import com.google.zxing.Binarizer;
  8. import com.google.zxing.BinaryBitmap;
  9. import com.google.zxing.EncodeHintType;
  10. import com.google.zxing.LuminanceSource;
  11. import com.google.zxing.MultiFormatReader;
  12. import com.google.zxing.NotFoundException;
  13. import com.google.zxing.Result;
  14. import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
  15. import com.google.zxing.common.BitArray;
  16. import com.google.zxing.common.BitMatrix;
  17. import com.google.zxing.common.HybridBinarizer;
  18. /**
  19. * 读取zxing类型下的二维码信息
  20. * @author fangyi
  21. *
  22. */
  23. public class ReadQrCode_Zxing {
  24. @SuppressWarnings("unchecked")
  25. public static void main(String[] args) {
  26. try {
  27. MultiFormatReader formatReader = new MultiFormatReader();
  28. //读取指定的二维码文件
  29. File file= new File("D:\\mycode.gif");
  30. BufferedImage bufferedImage =ImageIO.read(file);
  31. BinaryBitmap binaryBitmap= new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(bufferedImage)));
  32. //定义二维码参数
  33. Map hints= new HashMap<>();
  34. hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
  35. Result result = formatReader.decode(binaryBitmap, hints);
  36. //输出相关的二维码信息
  37. System.out.println("解析结果"+result.toString());
  38. System.out.println("二维码格式类型"+result.getBarcodeFormat());
  39. System.out.println("二维码文本内容"+result.getText());
  40. bufferedImage.flush();
  41. } catch (NotFoundException e) {
  42. e.printStackTrace();
  43. } catch (IOException e) {
  44. e.printStackTrace();
  45. }
  46. }
  47. }

用二维码进行添加好友获取好友信息是用到VCard

微信扫码登陆网页原理:通过堵塞等待的长连接,近乎实时的获得信息。 对于验证过程,Open API 一般是通过授权令牌(Token)来解决的,原理是当用户通过授权后,分配一个限定条件下的令牌(如限制本机访问、限制授权有效时间、限制同时登录设备数等),使获得授权的用户仅在有限的前提下能访问相关服务。 像计算机休眠后曾做的授权就自动收回了,这样就有效的避免了在别人电脑上(尤其是网吧)打开,但忘记关闭或退出这类安全问题了。

实现可借鉴已有博客:https://www.cnblogs.com/zijun/p/3580125.html

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,111
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,584
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,431
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,203
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,838
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,922