zoukankan      html  css  js  c++  java
  • Qrcode 二维码


    1. package cn.com.do1.wechat.common;
    2. import java.awt.Color;
    3. import java.awt.Graphics2D;
    4. import java.awt.image.BufferedImage;
    5. import java.io.File;
    6. import javax.imageio.ImageIO;
    7. import javax.servlet.http.HttpServletResponse;
    8. import com.swetake.util.Qrcode;
    9. /*
    10. * java生成二维码工具类扫一扫
    11. */
    12. public class QrcodeUtil {
    13. /**
    14. * 生成二维码图像
    15. *
    16. * @author sss
    17. * @param content
    18. * 需要生成二维码的文字或者其他内容
    19. * 生成二维码图像保存的地址
    20. * @return
    21. */
    22. public static void QrcodeImg(String content, HttpServletResponse response) {
    23. int width = 160;
    24. int height = 160;
    25. // 实例化Qrcode
    26. try {
    27. Qrcode qrcode = new Qrcode();
    28. // 设置二维码排错率,可以选项L(%7),M(%15),Q(%25),H(%30)
    29. qrcode.setQrcodeErrorCorrect('M');
    30. qrcode.setQrcodeEncodeMode('B');
    31. // 设置二维码的尺寸,取值范围(1-40)
    32. qrcode.setQrcodeVersion(8);
    33. // 设置图片尺寸
    34. BufferedImage bufImg = new BufferedImage(width, height,
    35. BufferedImage.TYPE_INT_RGB);
    36. // 绘制二维码图片
    37. Graphics2D gs = bufImg.createGraphics();
    38. // 设置二维码的背景颜色
    39. gs.setBackground(Color.WHITE);
    40. // 创建二维码的矩形区域
    41. gs.clearRect(0, 0, width, height);
    42. // 设置二维码的颜色
    43. gs.setColor(Color.BLACK);
    44. //设置偏移量 不设置可能导致解析出错
    45. int pixoff = 2;
    46. // 获取内容的字节数组,设置编码集
    47. byte[] contentBytes = new byte[2048];
    48. contentBytes = content.getBytes("utf-8");
    49. if (contentBytes.length > 0 && contentBytes.length < 1000) {
    50. boolean[][] codeOut = qrcode.calQrcode(contentBytes);
    51. for (int i = 0; i < codeOut.length; i++) {
    52. for (int j = 0; j < codeOut.length; j++) {
    53. if (codeOut[j][i]) {
    54. gs.fillRect(j * 3 + pixoff, i * 3 + pixoff, 3, 3);
    55. }
    56. }
    57. }
    58. } else {
    59. throw new RuntimeException("输入的内容超出了二维码的最大限制值");
    60. }
    61. gs.dispose();
    62. bufImg.flush();
    63. //生成二维码QRCode图片 (以流的方式输出。)
    64. ImageIO.write(bufImg, "jpg", response.getOutputStream());
    65. // // 生成二维码图片
    66. // File imgFile = new File(imgPath);
    67. // ImageIO.write(bufImg, "png", imgFile);
    68. // System.out.println("恭喜您,二维码生成成功");
    69. } catch (Exception e) {
    70. e.printStackTrace();
    71. }
    72. }
    73. }

    注:加大二维码的图片Vesion可以容纳更多的字节 

                    int width = 160;
    int height = 160;
    // 实例化Qrcode
    try {
    Qrcode qrcode = new Qrcode();
    // 设置二维码排错率,可以选项L(%7),M(%15),Q(%25),H(%30)
    qrcode.setQrcodeErrorCorrect('M');
    qrcode.setQrcodeEncodeMode('B');
    // 设置二维码的尺寸,取值范围(1-40)
    qrcode.setQrcodeVersion(8);

  • 相关阅读:
    Dart语言学习笔记(5)
    使用 Dart 调用 REST API
    JSON数据的解析和生成(Dart)
    趣味编程:静夜思(Dart版)
    正则表达式(Dart)
    Dart语言学习笔记(4)
    Dart语言学习笔记(3)
    C++11特性之右值引用
    各大编程字体比较
    优先队列的应用 C++实现
  • 原文地址:https://www.cnblogs.com/signheart/p/6597967.html
Copyright © 2011-2022 走看看