Android ZXing二维码、条形码的生成,扫一扫demo

雨点打透心脏的1/2处 2021-09-14 13:26 1048阅读 0赞

目录

导语

一、ZXing简介

二、ZXing方法类源码

BarcodeFormat:条形码格式类

EncodeHintType:编码显示风格

三、ZXing使用实战小案例

生成二维码

生成条形码

四、二维码扫一扫


导语

自从微信、支付宝推出扫一扫功能后,条形码、二维码的使用场景越来越丰富,扫码使用场景:

  • 信息获取(名片、地图、WIFI密码、资料)
  • 网站跳转(跳转到微博、手机网站、网站)
  • 广告推送(用户扫码,直接浏览商家推送的视频、音频广告)
  • 手机电商(用户扫码、手机直接购物下单)
  • 防伪溯源(用户扫码、即可查看生产地;同时可以获取最终消费地)
  • 优惠促销(用户扫码,下载电子优惠券,抽奖)
  • 会员管理(用户手机上获取电子会员信息、VIP服务)
  • 手机支付(扫描商品二维码,通过银行或第三方支付提供的手机端通道完成支付
  • 登陆管理(不需输入账户,密码,扫码登陆)

时代的脚步下,今天我们出行、骑行、购物,去商场和店铺都是支付扫码了。所以我们赶快一起学习一下吧!

小知识:浅谈二维码的生成和识别原理

一、ZXing简介

ZXing(Zebra Crossing)是Google开源的用于生成和解析多种格式1D/2D条形码的JAVA类库。

jar包下载: http://download.csdn.net/download/csdn_aiyang/10190680

官网地址: http://code.google.com/p/zxing/

GitHub地址: ZXing-GitHub项目地址

二、ZXing方法类源码

BarcodeFormat:条形码格式类

  1. package com.google.zxing;
  2. public enum BarcodeFormat {
  3. AZTEC,
  4. CODABAR,// 可表示数字0 - 9,字符$、+、 -、还有只能用作起始/终止符的a,b,c d四个字符,可变长度,没有校验位
  5. CODE_39,
  6. CODE_93,
  7. CODE_128,// 条形码,表示高密度数据, 字符串可变长,符号内含校验码
  8. DATA_MATRIX,
  9. EAN_8,
  10. EAN_13,// 条形码,13位纯数字
  11. ITF,
  12. MAXICODE,
  13. PDF_417,// 二维码
  14. QR_CODE,// 二维码
  15. RSS_14,
  16. RSS_EXPANDED,
  17. UPC_A,// 统一产品代码A:12位数字,最后一位为校验位
  18. UPC_E,// 统一产品代码E:7位数字,最后一位为校验位
  19. UPC_EAN_EXTENSION;
  20. private BarcodeFormat() {
  21. }
  22. }

EncodeHintType:编码显示风格

  1. package com.google.zxing;
  2. public enum EncodeHintType {
  3. ERROR_CORRECTION,//错误修正
  4. CHARACTER_SET,//编码格式
  5. MARGIN,//外边距
  6. PDF417_COMPACT,
  7. PDF417_COMPACTION,
  8. PDF417_DIMENSIONS;
  9. private EncodeHintType() {
  10. }
  11. }

三、ZXing使用实战小案例

生成二维码

  1. private void initialLayout() {
  2. ImageView imageQRCode = (ImageView) findViewById(R.id.imageQRCode);
  3. String contentQRCode = "二维码字符串";
  4. try {
  5. // 根据字符串生成二维码图片并显示在界面上,第二个参数为图片的大小(310*310)
  6. Bitmap bitmapQRCode = createQRCode(contentQRCode, 310);
  7. imageQRCode.setImageBitmap(bitmapQRCode);
  8. } catch (WriterException e) {
  9. e.printStackTrace();
  10. }
  11. }
  12. private static final int BLACK = 0xff000000; //二维码颜色
  13. public static Bitmap createQRCode(String str, int widthAndHeight)
  14. throws WriterException {
  15. Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
  16. hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
  17. BitMatrix matrix = new MultiFormatWriter().encode(str,
  18. BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight);
  19. int width = matrix.getWidth();
  20. int height = matrix.getHeight();
  21. int[] pixels = new int[width * height];
  22. for (int y = 0; y < height; y++) {
  23. for (int x = 0; x < width; x++) {
  24. if (matrix.get(x, y)) {
  25. pixels[y * width + x] = BLACK;
  26. }
  27. }
  28. }
  29. Bitmap bitmap = Bitmap.createBitmap(width, height,
  30. Bitmap.Config.ARGB_8888);
  31. bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
  32. return bitmap;
  33. }

SouthEast

二维码 · 功能升级!

如果想在中心放上小图标,如图效果:

SouthEast 1 (在带入zxing.jar后,代码可直接复制到项目中使用。)

  1. /**
  2. * 生成二维码图片
  3. *
  4. * @param codeUrl
  5. * @param icon
  6. */
  7. private void showQREncode(String codeUrl, Bitmap icon) {
  8. if (null != codeUrl && !codeUrl.equals(""))
  9. try {
  10. Bitmap bitmap = createBitmap(new String(codeUrl.getBytes(), "UTF-8"), icon);
  11. mQrcodeImg.setImageBitmap(bitmap);
  12. mQRCodeShow = true;
  13. } catch (Exception e) {
  14. e.printStackTrace();
  15. }
  16. } else {
  17. CommonTools.showShortToast(mContext, "请输入二维码信息!");
  18. }
  19. }
  20. /**
  21. * 生成二维码 中间插入小图片
  22. */
  23. private Bitmap createBitmap(String str, Bitmap icon) throws WriterException {
  24. icon = zoomBitmap(icon, IMAGE_HALFWIDTH);
  25. Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
  26. hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
  27. hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
  28. hints.put(EncodeHintType.MARGIN, 1);
  29. // 生成二维矩阵,编码时指定大小,不要生成了图片以后再进行缩放,这样会模糊导致识别失败
  30. BitMatrix matrix = new MultiFormatWriter().encode(str, BarcodeFormat.QR_CODE, 380, 380, hints);
  31. int width = matrix.getWidth();
  32. int height = matrix.getHeight();
  33. // 二维矩阵转为一维像素数组,也就是一直横着排了
  34. int halfW = width / 2;
  35. int halfH = height / 2;
  36. int[] pixels = new int[width * height];
  37. for (int y = 0; y < height; y++) {
  38. for (int x = 0; x < width; x++) {
  39. if (x > halfW - IMAGE_HALFWIDTH && x < halfW + IMAGE_HALFWIDTH && y > halfH - IMAGE_HALFWIDTH && y < halfH + IMAGE_HALFWIDTH) {
  40. pixels[y * width + x] = icon.getPixel(x - halfW + IMAGE_HALFWIDTH, y - halfH + IMAGE_HALFWIDTH);
  41. } else {
  42. if (matrix.get(x, y)) {
  43. pixels[y * width + x] = 0xff000000;
  44. } else { // 无信息设置像素点为白色
  45. pixels[y * width + x] = 0xffffffff;
  46. }
  47. }
  48. }
  49. }
  50. Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
  51. // 通过像素数组生成bitmap
  52. bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
  53. return bitmap;
  54. }
  55. /**
  56. * 缩放图片
  57. */
  58. private Bitmap zoomBitmap(Bitmap icon, int h) {
  59. // 缩放图片
  60. Matrix m = new Matrix();
  61. float sx = (float) 2 * h / icon.getWidth();
  62. float sy = (float) 2 * h / icon.getHeight();
  63. m.setScale(sx, sy);
  64. // 重新构造一个图片
  65. return Bitmap.createBitmap(icon, 0, 0, icon.getWidth(), icon.getHeight(), m, false);
  66. }

生成条形码

  1. /**
  2. * 转换条形码
  3. *
  4. * @param str 条形码的字符串
  5. * @return
  6. * @throws WriterException
  7. */
  8. private Bitmap BarcodeFormatCode(String str) {
  9. int width = 800;
  10. int height = 160;
  11. BarcodeFormat barcodeFormat = BarcodeFormat.CODE_128;
  12. BitMatrix matrix = null;
  13. try {
  14. matrix = new MultiFormatWriter().encode(str, barcodeFormat, width, height, null);
  15. return bitMatrix2Bitmap(matrix);
  16. } catch (WriterException e) {
  17. e.printStackTrace();
  18. }
  19. return null;
  20. }
  21. private Bitmap bitMatrix2Bitmap(BitMatrix matrix) {
  22. int w = matrix.getWidth();
  23. int h = matrix.getHeight();
  24. int[] rawData = new int[w * h];
  25. for (int i = 0; i < w; i++) {
  26. for (int j = 0; j < h; j++) {
  27. int color = Color.WHITE;
  28. if (matrix.get(i, j)) {
  29. // 有内容的部分,颜色设置为黑色,可以自己修改成其他颜色
  30. color = Color.BLACK;
  31. }
  32. rawData[i + (j * w)] = color;
  33. }
  34. }
  35. Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
  36. bitmap.setPixels(rawData, 0, w, 0, 0, w, h);
  37. return bitmap;
  38. }

" class="reference-link">SouthEast 2

四、二维码扫一扫

zxing.CaptureActivity中handleDecode处理解析好的数据即可

  1. /**
  2. * A valid barcode has been found, so give an indication of success and show
  3. * the results.
  4. *
  5. * @param rawResult The contents of the barcode.
  6. * @param scaleFactor amount by which thumbnail was scaled
  7. * @param barcode A greyscale bitmap of the camera data which was decoded.
  8. */
  9. public void handleDecode(final Result rawResult, Bitmap barcode,
  10. float scaleFactor) {
  11. inactivityTimer.onActivity();
  12. boolean fromLiveScan = barcode != null;
  13. if (fromLiveScan) {
  14. beepManager.playBeepSoundAndVibrate();
  15. }
  16. finish();
  17. String scanResult = rawResult.getText();
  18. if (null != scanResult && scanResult.trim().length() > 0) {
  19. Intent intentBind = new Intent(CaptureActivity.this,
  20. BindActivity.class);
  21. intentBind.putExtra("physicalId", scanResult);
  22. startActivity(intentBind);
  23. }
  24. switch (source) {
  25. case NATIVE_APP_INTENT:
  26. case PRODUCT_SEARCH_LINK:
  27. break;
  28. case ZXING_LINK:
  29. break;
  30. case NONE:
  31. break;
  32. }
  33. }

二维码扫一扫,demo下载地址:

https://download.csdn.net/download/csdn_aiyang/12084640

发表评论

表情:
评论列表 (有 0 条评论,1048人围观)

还没有评论,来说两句吧...

相关阅读