当前位置:   article > 正文

Android 调用UVCCamera 采集UVC摄像头数据,并调用zxing 扫码库扫码_android uvc摄像头回显数据处理

android uvc摄像头回显数据处理

zxing 提供的sample都是原生camera采集摄像头数据,而android上使用UVCCamera采集摄像头的情况比较多,那么怎么在uvccamera采集到摄像头数据,并调用zxing扫码呢。直接上代码。

DecodeHandle.java

  1. package com.baidu.idl.face.main.decode;
  2. import android.content.Context;
  3. import android.graphics.Bitmap;
  4. import android.os.Bundle;
  5. import android.os.Handler;
  6. import android.os.HandlerThread;
  7. import android.os.Looper;
  8. import android.os.Message;
  9. import android.text.TextUtils;
  10. import android.util.Log;
  11. import com.google.zxing.BarcodeFormat;
  12. import com.google.zxing.BinaryBitmap;
  13. import com.google.zxing.DecodeHintType;
  14. import com.google.zxing.MultiFormatReader;
  15. import com.google.zxing.PlanarYUVLuminanceSource;
  16. import com.google.zxing.ReaderException;
  17. import com.google.zxing.Result;
  18. import com.google.zxing.common.HybridBinarizer;
  19. import java.util.Arrays;
  20. import java.util.Collection;
  21. import java.util.Map;
  22. import java.util.EnumMap;
  23. import java.util.EnumSet;
  24. public class DecodeHandler {
  25. private final DecodeListener mListener;
  26. private boolean isAvailable = true;
  27. private static final int DECODE_BITMAP = 0;
  28. private static final int DECODE_YUV = 1;
  29. //private QrDecoder mDecoder;
  30. private DecodeThread mDecodThread;
  31. private int mTimeout;
  32. Context mContext;
  33. private int mWidth;
  34. private int mHeight;
  35. private final Map<DecodeHintType, Object> hints;
  36. private Collection<BarcodeFormat> decodeFormats;
  37. private final MultiFormatReader multiFormatReader;
  38. public DecodeHandler(Context context, DecodeListener listener,final int width, final int height) {
  39. mWidth = width;
  40. mHeight = height;
  41. mListener = listener;
  42. mContext = context;
  43. //mDecoder = new QrDecoder(context);
  44. HandlerThread thread = new HandlerThread("qr_decode_thread");
  45. thread.start();
  46. mDecodThread = new DecodeThread(thread.getLooper());
  47. //扫码初始化
  48. multiFormatReader = new MultiFormatReader();
  49. decodeFormats = EnumSet.of(BarcodeFormat.QR_CODE, BarcodeFormat.CODE_128);
  50. hints = new EnumMap<>(DecodeHintType.class);
  51. hints.put(DecodeHintType.POSSIBLE_FORMATS, decodeFormats);
  52. String characterSet = "utf-8";
  53. if (characterSet != null) {
  54. hints.put(DecodeHintType.CHARACTER_SET, characterSet);
  55. }
  56. hints.put(DecodeHintType.TRY_HARDER, true);
  57. multiFormatReader.setHints(hints);
  58. }
  59. public void release() {
  60. setAvailable(false);
  61. mDecodThread.getLooper().quit();
  62. }
  63. public void pushYuv(byte[] yuv) {
  64. Log.d("MainActivity","pushYuv()");
  65. if (isAvailable()) {
  66. setAvailable(false);
  67. mDecodThread.obtainMessage(DECODE_YUV, yuv).sendToTarget();
  68. }
  69. }
  70. private boolean isAvailable() {
  71. return isAvailable;
  72. }
  73. private void setAvailable(boolean b) {
  74. isAvailable = b;
  75. }
  76. private class DecodeThread extends Handler {
  77. DecodeThread(Looper looper) {
  78. super(looper);
  79. }
  80. @Override
  81. public void handleMessage(Message msg) {
  82. super.handleMessage(msg);
  83. switch (msg.what) {
  84. case DECODE_YUV:
  85. {
  86. decode((byte[])msg.obj,mWidth,mHeight);
  87. setAvailable(true);
  88. }
  89. break;
  90. }
  91. }
  92. }
  93. /**
  94. * @param data A preview frame.
  95. * @param width The width of the image.
  96. * @param height The height of the image.
  97. * @return A PlanarYUVLuminanceSource instance.
  98. */
  99. public PlanarYUVLuminanceSource buildLuminanceSource(byte[] data, int width, int height) {
  100. return new PlanarYUVLuminanceSource(data, width, height, 0, 0,
  101. width, height, false);
  102. }
  103. /**
  104. * @param data The YUV preview frame.
  105. * @param width The width of the preview frame.
  106. * @param height The height of the preview frame.
  107. */
  108. private void decode(byte[] data, int width, int height) {
  109. long start = System.currentTimeMillis();
  110. Result rawResult = null;
  111. PlanarYUVLuminanceSource source = buildLuminanceSource(data, width, height);
  112. if (source != null) {
  113. BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
  114. try {
  115. rawResult = multiFormatReader.decodeWithState(bitmap);
  116. BarcodeFormat format = rawResult.getBarcodeFormat();
  117. //类型
  118. String type = format.toString();
  119. String strScan = rawResult.getText();
  120. String content = new String(strScan.trim());
  121. if (!TextUtils.isEmpty(content)) {
  122. mListener.onDecodeSuccess(content);
  123. }
  124. } catch (ReaderException re) {
  125. // continue
  126. mListener.onDecodeFailed(-1);
  127. } finally {
  128. multiFormatReader.reset();
  129. }
  130. }
  131. }
  132. }

 

DecodeListener.java

  1. package com.baidu.idl.face.main.decode;
  2. public interface DecodeListener {
  3. public void onDecodeSuccess(final String content);
  4. public void onDecodeFailed(final int error);
  5. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Li_阴宅/article/detail/1017183
推荐阅读
相关标签
  

闽ICP备14008679号