QRCode.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package com.baoshi.swms.util;
  2. import android.graphics.Bitmap;
  3. import android.graphics.Color;
  4. import android.text.TextUtils;
  5. import androidx.annotation.ColorInt;
  6. import androidx.annotation.Nullable;
  7. import com.google.zxing.BarcodeFormat;
  8. import com.google.zxing.EncodeHintType;
  9. import com.google.zxing.WriterException;
  10. import com.google.zxing.common.BitMatrix;
  11. import com.google.zxing.common.CharacterSetECI;
  12. import com.google.zxing.qrcode.QRCodeWriter;
  13. import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
  14. import java.util.Hashtable;
  15. /**
  16. * 二维码生成工具类
  17. *
  18. * @author Zhendong Zhou
  19. * @date 2021/12/3 18:06
  20. */
  21. public class QRCode {
  22. /**
  23. * 创建二维码位图
  24. *
  25. * @param content 字符串内容(支持中文)
  26. * @param width 位图宽度(单位:px)
  27. * @param height 位图高度(单位:px)
  28. * @return
  29. */
  30. @Nullable
  31. public static Bitmap createQRCodeBitmap(String content, int width, int height){
  32. return createQRCodeBitmap(content, width, height, "utf-8", "H", "2", Color.BLACK, Color.WHITE);
  33. }
  34. /**
  35. * 创建二维码位图 (支持自定义配置和自定义样式)
  36. *
  37. * @param content 字符串内容
  38. * @param width 位图宽度,要求>=0(单位:px)
  39. * @param height 位图高度,要求>=0(单位:px)
  40. * @param character_set 字符集/字符转码格式 (支持格式:{@link CharacterSetECI })。传null时,zxing源码默认使用 "ISO-8859-1"
  41. * @param error_correction 容错级别 (支持级别:{@link ErrorCorrectionLevel })。传null时,zxing源码默认使用 "L"
  42. * @param margin 空白边距 (可修改,要求:整型且>=0), 传null时,zxing源码默认使用"4"。
  43. * @param color_black 黑色色块的自定义颜色值
  44. * @param color_white 白色色块的自定义颜色值
  45. * @return Bitmap
  46. */
  47. @Nullable
  48. public static Bitmap createQRCodeBitmap(String content, int width, int height,
  49. @Nullable String character_set, @Nullable String error_correction, @Nullable String margin,
  50. @ColorInt int color_black, @ColorInt int color_white){
  51. /** 1.参数合法性判断 */
  52. if(TextUtils.isEmpty(content)){ // 字符串内容判空
  53. return null;
  54. }
  55. if(width < 0 || height < 0){ // 宽和高都需要>=0
  56. return null;
  57. }
  58. try {
  59. /** 2.设置二维码相关配置,生成BitMatrix(位矩阵)对象 */
  60. Hashtable<EncodeHintType, String> hints = new Hashtable<>();
  61. if(!TextUtils.isEmpty(character_set)) {
  62. hints.put(EncodeHintType.CHARACTER_SET, character_set); // 字符转码格式设置
  63. }
  64. if(!TextUtils.isEmpty(error_correction)){
  65. hints.put(EncodeHintType.ERROR_CORRECTION, error_correction); // 容错级别设置
  66. }
  67. if(!TextUtils.isEmpty(margin)){
  68. hints.put(EncodeHintType.MARGIN, margin); // 空白边距设置
  69. }
  70. BitMatrix bitMatrix = new QRCodeWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
  71. /** 3.创建像素数组,并根据BitMatrix(位矩阵)对象为数组元素赋颜色值 */
  72. int[] pixels = new int[width * height];
  73. for(int y = 0; y < height; y++){
  74. for(int x = 0; x < width; x++){
  75. if(bitMatrix.get(x, y)){
  76. pixels[y * width + x] = color_black; // 黑色色块像素设置
  77. } else {
  78. pixels[y * width + x] = color_white; // 白色色块像素设置
  79. }
  80. }
  81. }
  82. /** 4.创建Bitmap对象,根据像素数组设置Bitmap每个像素点的颜色值,之后返回Bitmap对象 */
  83. Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
  84. bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
  85. return bitmap;
  86. } catch (WriterException e) {
  87. e.printStackTrace();
  88. }
  89. return null;
  90. }
  91. }