Exemplo n.º 1
0
  /**
   * Decode the data within the viewfinder rectangle, and time how long it took. For efficiency,
   * reuse the same reader objects from one decode to the next.
   *
   * @param data The YUV preview frame.
   * @param width The width of the preview frame.
   * @param height The height of the preview frame.
   */
  private void decode(byte[] data, int width, int height) {
    long start = System.currentTimeMillis();
    Result rawResult = null;
    PlanarYUVLuminanceSource source =
        activity.getCameraManager().buildLuminanceSource(data, width, height);
    if (source != null) {
      BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
      try {
        rawResult = multiFormatReader.decodeWithState(bitmap);
      } catch (ReaderException re) {
        // continue
      } finally {
        multiFormatReader.reset();
      }
    }

    Handler handler = activity.getHandler();
    if (rawResult != null) {
      // Don't log the barcode contents for security.
      long end = System.currentTimeMillis();
      Log.d(TAG, "Found barcode in " + (end - start) + " ms");
      if (handler != null) {
        Message message = Message.obtain(handler, R.id.decode_succeeded, rawResult);
        Bundle bundle = new Bundle();
        bundleThumbnail(source, bundle);
        message.setData(bundle);
        message.sendToTarget();
      }
    } else {
      if (handler != null) {
        Message message = Message.obtain(handler, R.id.decode_failed);
        message.sendToTarget();
      }
    }
  }
Exemplo n.º 2
0
  /**
   * Decode the data within the viewfinder rectangle, and time how long it took. For efficiency,
   * reuse the same reader objects from one decode to the next.
   *
   * @param data The YUV preview frame.
   * @param width The width of the preview frame.
   * @param height The height of the preview frame.
   */
  private void decode(byte[] data, int width, int height) {
    long start = System.currentTimeMillis();
    Result rawResult = null;
    PlanarYUVLuminanceSource source = CameraManager.get().buildLuminanceSource(data, width, height);
    BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
    try {
      rawResult = multiFormatReader.decodeWithState(bitmap);
    } catch (ReaderException re) {
      // continue
    } finally {
      multiFormatReader.reset();
    }

    if (rawResult != null) {
      // Don't log the barcode contents for security.
      long end = System.currentTimeMillis();
      Log.d(TAG, "Found barcode in " + (end - start) + " ms");
      Message message =
          Message.obtain(activity.getHandler(), R.id.zxinglib_decode_succeeded, rawResult);
      Bundle bundle = new Bundle();
      bundle.putParcelable(DecodeThread.BARCODE_BITMAP, source.renderCroppedGreyscaleBitmap());
      message.setData(bundle);
      message.sendToTarget();
    } else {
      Message message = Message.obtain(activity.getHandler(), R.id.zxinglib_decode_failed);
      message.sendToTarget();
    }
  }
Exemplo n.º 3
0
  /**
   * Decode the data within the viewfinder rectangle, and time how long it took. For efficiency,
   * reuse the same reader objects from one decode to the next.
   *
   * @param data The YUV preview frame.
   * @param width The width of the preview frame.
   * @param height The height of the preview frame.
   */
  private void decode(byte[] data, int width, int height) {
    long start = System.currentTimeMillis();
    Result rawResult = null;
    // PlanarYUVLuminanceSource source =
    // activity.getCameraManager().buildLuminanceSource(data, width,
    // height);
    // ------------------------------------

    byte[] rotatedData = new byte[data.length];
    for (int y = 0; y < height; y++) {
      for (int x = 0; x < width; x++)
        rotatedData[x * height + height - y - 1] = data[x + y * width];
    }
    int tmp = width; // Here we are swapping, that's the difference to #11
    width = height;
    height = tmp;

    PlanarYUVLuminanceSource source =
        activity.getCameraManager().buildLuminanceSource(rotatedData, width, height);

    // ------------------------------------
    if (source != null) {
      BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
      try {
        rawResult = multiFormatReader.decodeWithState(bitmap);
      } catch (ReaderException re) {
        // continue
      } finally {
        multiFormatReader.reset();
      }
    }

    Handler handler = activity.getHandler();
    if (rawResult != null) {
      // Don't log the barcode contents for security.
      long end = System.currentTimeMillis();
      Log.d(TAG, "Found barcode in " + (end - start) + " ms");
      if (handler != null) {
        Message message = Message.obtain(handler, R.id.decode_succeeded, rawResult);
        Bundle bundle = new Bundle();
        Bitmap grayscaleBitmap = toBitmap(source, source.renderCroppedGreyscaleBitmap());
        bundle.putParcelable(DecodeThread.BARCODE_BITMAP, grayscaleBitmap);
        message.setData(bundle);
        message.sendToTarget();
      }
    } else {
      if (handler != null) {
        Message message = Message.obtain(handler, R.id.decode_failed);
        message.sendToTarget();
      }
    }
  }
Exemplo n.º 4
0
 private static String decodeQrcode(String filePath) {
   String content = "";
   try {
     BufferedImage image = ImageIO.read(new File(filePath));
     MultiFormatReader reader = new MultiFormatReader();
     LuminanceSource source = new BufferedImageLuminanceSource(image);
     BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
     Result result = reader.decode(bitmap, getDecodeHints());
     content = result.getText();
   } catch (Exception e) {
     e.printStackTrace();
   }
   return content;
 }
  /**
   * Decode the data within the viewfinder rectangle, and time how long it took. For efficiency,
   * reuse the same reader objects from one decode to the next.
   *
   * @param data The YUV preview frame.
   * @param width The width of the preview frame.
   * @param height The height of the preview frame.
   */
  private void decode(byte[] data, int width, int height) {
    Size size = activity.getCameraManager().getPreviewSize();

    // 这里需要将获取的data翻转一下,因为相机默认拿的的横屏的数据
    byte[] rotatedData = new byte[data.length];
    for (int y = 0; y < size.height; y++) {
      for (int x = 0; x < size.width; x++)
        rotatedData[x * size.height + size.height - y - 1] = data[x + y * size.width];
    }

    // 宽高也要调整
    int tmp = size.width;
    size.width = size.height;
    size.height = tmp;

    Result rawResult = null;
    PlanarYUVLuminanceSource source = buildLuminanceSource(rotatedData, size.width, size.height);
    if (source != null) {
      BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
      try {
        rawResult = multiFormatReader.decodeWithState(bitmap);
      } catch (ReaderException re) {
        // continue
      } finally {
        multiFormatReader.reset();
      }
    }

    Handler handler = activity.getHandler();
    if (rawResult != null) {
      // Don't log the barcode contents for security.
      if (handler != null) {
        Message message = Message.obtain(handler, R.id.decode_succeeded, rawResult);
        Bundle bundle = new Bundle();
        bundleThumbnail(source, bundle);
        message.setData(bundle);
        message.sendToTarget();
      }
    } else {
      if (handler != null) {
        Message message = Message.obtain(handler, R.id.decode_failed);
        message.sendToTarget();
      }
    }
  }
Exemplo n.º 6
0
  public ZXDecoder() {
    reader = new MultiFormatReader();

    hints.put(DecodeHintType.POSSIBLE_FORMATS, EnumSet.allOf(BarcodeFormat.class));
    hints.put(DecodeHintType.CHARACTER_SET, "utf-8");
    hints.put(DecodeHintType.TRY_HARDER, true);

    reader.setHints(hints);
  }
Exemplo n.º 7
0
 public Result getRawResult(Bitmap bitmap) {
   if (bitmap == null) {
     return null;
   }
   try {
     return multiFormatReader.decodeWithState(
         new BinaryBitmap(new HybridBinarizer(new BitmapLuminanceSource(bitmap))));
   } catch (NotFoundException e) {
     e.printStackTrace();
   }
   return null;
 }
Exemplo n.º 8
0
  /**
   * Decode the data within the viewfinder rectangle, and time how long it took. For efficiency,
   * reuse the same reader objects from one decode to the next.
   *
   * @param data The YUV preview frame.
   * @param width The width of the preview frame.
   * @param height The height of the preview frame.
   */
  private void decode(byte[] data, int width, int height) {
    long start = System.currentTimeMillis();
    Result rawResult = null;

    // XXX modify here
    byte[] rotatedData = new byte[data.length];
    for (int y = 0; y < height; y++) {
      for (int x = 0; x < width; x++)
        rotatedData[x * height + height - y - 1] = data[x + y * width];
    }
    int tmp = width; // Here we are swapping, that's the difference to #11
    width = height;
    height = tmp;

    PlanarYUVLuminanceSource source =
        CameraManager.get().buildLuminanceSource(rotatedData, width, height);
    BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
    try {
      rawResult = multiFormatReader.decodeWithState(bitmap);
    } catch (ReaderException re) {
      // continue
    } finally {
      multiFormatReader.reset();
    }

    if (rawResult != null) {
      long end = System.currentTimeMillis();
      Log.d(TAG, "Found barcode (" + (end - start) + " ms):\n" + rawResult.toString());
      Message message =
          Message.obtain(activity.getHandler(), R.id.zandroid_zxing_decode_succeeded, rawResult);
      Bundle bundle = new Bundle();
      bundle.putParcelable(DecodeThread.BARCODE_BITMAP, source.renderCroppedGreyscaleBitmap());
      message.setData(bundle);
      // Log.d(TAG, "Sending decode succeeded message...");
      message.sendToTarget();
    } else {
      Message message = Message.obtain(activity.getHandler(), R.id.zandroid_zxing_decode_failed);
      message.sendToTarget();
    }
  }
Exemplo n.º 9
0
  public String decode(final byte[] image, final int width, final int height) {
    Result result = null;

    try {
      BinaryBitmap bitmap =
          new BinaryBitmap(
              new HybridBinarizer(
                  new PlanarRotatedYUVLuminanceSource(
                      image, width, height, 0, 0, width, height, true)));
      result = reader.decodeWithState(bitmap);
    } catch (Throwable re) {
      re.printStackTrace();
    } finally {
      reader.reset();
    }

    if (result != null) {
      return result.getText();
    }

    return null;
  }
Exemplo n.º 10
0
 public DecoderBitmap(Context context) {
   multiFormatReader = new MultiFormatReader();
   Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(2);
   Vector<BarcodeFormat> decodeFormats = new Vector<BarcodeFormat>();
   if (decodeFormats == null || decodeFormats.isEmpty()) {
     decodeFormats = new Vector<BarcodeFormat>();
     decodeFormats.addAll(DecodeFormatManager.getBarCodeFormats());
     decodeFormats.addAll(DecodeFormatManager.getQrCodeFormats());
     decodeFormats.addAll(EnumSet.of(BarcodeFormat.AZTEC));
     decodeFormats.addAll(EnumSet.of(BarcodeFormat.PDF_417));
   }
   hints.put(DecodeHintType.POSSIBLE_FORMATS, decodeFormats);
   hints.put(DecodeHintType.CHARACTER_SET, "UTF8");
   multiFormatReader.setHints(hints);
 }
Exemplo n.º 11
0
  public BitmapDecoder(Context context) {

    multiFormatReader = new MultiFormatReader();

    // 解码的参数
    Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(2);
    // 可以解析的编码类型
    Vector<BarcodeFormat> decodeFormats = new Vector<BarcodeFormat>();
    if (decodeFormats == null || decodeFormats.isEmpty()) {
      decodeFormats = new Vector<BarcodeFormat>();

      // 这里设置可扫描的类型,我这里选择了都支持
      decodeFormats.addAll(DecodeFormatManager.ONE_D_FORMATS);
      decodeFormats.addAll(DecodeFormatManager.QR_CODE_FORMATS);
      decodeFormats.addAll(DecodeFormatManager.DATA_MATRIX_FORMATS);
    }
    hints.put(DecodeHintType.POSSIBLE_FORMATS, decodeFormats);

    // 设置继续的字符编码格式为UTF8
    hints.put(DecodeHintType.CHARACTER_SET, "UTF8");

    // 设置解析配置参数
    multiFormatReader.setHints(hints);
  }
Exemplo n.º 12
0
 DecodeHandler(CaptureActivity activity, Hashtable<DecodeHintType, Object> hints) {
   multiFormatReader = new MultiFormatReader();
   multiFormatReader.setHints(hints);
   this.activity = activity;
 }
 DecodeHandler(BarCodeScannerFragment fragment, Map<DecodeHintType, Object> hints) {
   multiFormatReader = new MultiFormatReader();
   multiFormatReader.setHints(hints);
   this.fragment = fragment;
 }
Exemplo n.º 14
0
 DecodeHandler(BarCodeFragment activity, Map<DecodeHintType, Object> hints) {
   multiFormatReader = new MultiFormatReader();
   multiFormatReader.setHints(hints);
   this.activity = activity;
 }