Java Code Examples for com.google.zxing.DecodeHintType

The following examples show how to use com.google.zxing.DecodeHintType. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: tysq-android   Source File: ZxingUtils.java    License: GNU General Public License v3.0 6 votes vote down vote up
/**
 * 解析QR图内容
 *
 * @param imageView
 * @return
 */

public static String readImage(ImageView imageView) {
    String content = null;
    Map<DecodeHintType, String> hints = new HashMap<>();
    hints.put(DecodeHintType.CHARACTER_SET, "utf-8");

    // 获得待解析的图片
    Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
    RGBLuminanceSource source = new RGBLuminanceSource(bitmap);
    BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source));
    QRCodeReader reader = new QRCodeReader();
    try {
        Result result = reader.decode(bitmap1, hints);
        // 得到解析后的文字
        content = result.getText();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return content;
}
 
Example 2
Source Project: weex   Source File: MultiDetector.java    License: Apache License 2.0 6 votes vote down vote up
public DetectorResult[] detectMulti(Map<DecodeHintType,?> hints) throws NotFoundException {
  BitMatrix image = getImage();
  ResultPointCallback resultPointCallback =
      hints == null ? null : (ResultPointCallback) hints.get(DecodeHintType.NEED_RESULT_POINT_CALLBACK);
  MultiFinderPatternFinder finder = new MultiFinderPatternFinder(image, resultPointCallback);
  FinderPatternInfo[] infos = finder.findMulti(hints);

  if (infos.length == 0) {
    throw NotFoundException.getNotFoundInstance();
  }

  List<DetectorResult> result = new ArrayList<>();
  for (FinderPatternInfo info : infos) {
    try {
      result.add(processFinderPatternInfo(info));
    } catch (ReaderException e) {
      // ignore
    }
  }
  if (result.isEmpty()) {
    return EMPTY_DETECTOR_RESULTS;
  } else {
    return result.toArray(new DetectorResult[result.size()]);
  }
}
 
Example 3
CaptureActivityHandler(CaptureActivity activity,
                       Collection<BarcodeFormat> decodeFormats,
                       Map<DecodeHintType,?> baseHints,
                       String characterSet,
                       CameraManager cameraManager) {
  this.activity = activity;
  decodeThread = new DecodeThread(activity, decodeFormats, baseHints, characterSet,
      new ViewfinderResultPointCallback(activity.getViewfinderView()));
  decodeThread.start();
  state = State.SUCCESS;

  // Start ourselves capturing previews and decoding.
  this.cameraManager = cameraManager;
  cameraManager.startPreview();
  restartPreviewAndDecode();
}
 
Example 4
Source Project: barcodescanner-lib-aar   Source File: PDF417Reader.java    License: MIT License 6 votes vote down vote up
private static Result[] decode(BinaryBitmap image, Map<DecodeHintType, ?> hints, boolean multiple) 
    throws NotFoundException, FormatException, ChecksumException {
  List<Result> results = new ArrayList<>();
  PDF417DetectorResult detectorResult = Detector.detect(image, hints, multiple);
  for (ResultPoint[] points : detectorResult.getPoints()) {
    DecoderResult decoderResult = PDF417ScanningDecoder.decode(detectorResult.getBits(), points[4], points[5],
        points[6], points[7], getMinCodewordWidth(points), getMaxCodewordWidth(points));
    Result result = new Result(decoderResult.getText(), decoderResult.getRawBytes(), points, BarcodeFormat.PDF_417);
    result.putMetadata(ResultMetadataType.ERROR_CORRECTION_LEVEL, decoderResult.getECLevel());
    PDF417ResultMetadata pdf417ResultMetadata = (PDF417ResultMetadata) decoderResult.getOther();
    if (pdf417ResultMetadata != null) {
      result.putMetadata(ResultMetadataType.PDF417_EXTRA_METADATA, pdf417ResultMetadata);
    }
    results.add(result);
  }
  return results.toArray(new Result[results.size()]);
}
 
Example 5
Source Project: reacteu-app   Source File: DataMatrixReader.java    License: MIT License 6 votes vote down vote up
@Override
public Result decode(BinaryBitmap image, Map<DecodeHintType,?> hints)
    throws NotFoundException, ChecksumException, FormatException {
  DecoderResult decoderResult;
  ResultPoint[] points;
  if (hints != null && hints.containsKey(DecodeHintType.PURE_BARCODE)) {
    BitMatrix bits = extractPureBits(image.getBlackMatrix());
    decoderResult = decoder.decode(bits);
    points = NO_POINTS;
  } else {
    DetectorResult detectorResult = new Detector(image.getBlackMatrix()).detect();
    decoderResult = decoder.decode(detectorResult.getBits());
    points = detectorResult.getPoints();
  }
  Result result = new Result(decoderResult.getText(), decoderResult.getRawBytes(), points,
      BarcodeFormat.DATA_MATRIX);
  List<byte[]> byteSegments = decoderResult.getByteSegments();
  if (byteSegments != null) {
    result.putMetadata(ResultMetadataType.BYTE_SEGMENTS, byteSegments);
  }
  String ecLevel = decoderResult.getECLevel();
  if (ecLevel != null) {
    result.putMetadata(ResultMetadataType.ERROR_CORRECTION_LEVEL, ecLevel);
  }
  return result;
}
 
Example 6
Source Project: MiBandDecompiled   Source File: PDF417Reader.java    License: Apache License 2.0 6 votes vote down vote up
public Result decode(BinaryBitmap binarybitmap, Map map)
{
    DecoderResult decoderresult;
    ResultPoint aresultpoint[];
    if (map != null && map.containsKey(DecodeHintType.PURE_BARCODE))
    {
        BitMatrix bitmatrix = a(binarybitmap.getBlackMatrix());
        decoderresult = b.decode(bitmatrix);
        aresultpoint = a;
    } else
    {
        DetectorResult detectorresult = (new Detector(binarybitmap)).detect();
        decoderresult = b.decode(detectorresult.getBits());
        aresultpoint = detectorresult.getPoints();
    }
    return new Result(decoderresult.getText(), decoderresult.getRawBytes(), aresultpoint, BarcodeFormat.PDF_417);
}
 
Example 7
Source Project: android-quick-response-code   Source File: QRCodeReader.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Result decode(BinaryBitmap image, Map<DecodeHintType, ?> hints) throws NotFoundException, ChecksumException, FormatException {
    DecoderResult decoderResult;
    ResultPoint[] points;
    if (hints != null && hints.containsKey(DecodeHintType.PURE_BARCODE)) {
        BitMatrix bits = extractPureBits(image.getBlackMatrix());
        decoderResult = decoder.decode(bits, hints);
        points = NO_POINTS;
    } else {
        DetectorResult detectorResult = new Detector(image.getBlackMatrix()).detect(hints);
        decoderResult = decoder.decode(detectorResult.getBits(), hints);
        points = detectorResult.getPoints();
    }

    Result result = new Result(decoderResult.getText(), decoderResult.getRawBytes(), points, BarcodeFormat.QR_CODE);
    List<byte[]> byteSegments = decoderResult.getByteSegments();
    if (byteSegments != null) {
        result.putMetadata(ResultMetadataType.BYTE_SEGMENTS, byteSegments);
    }
    String ecLevel = decoderResult.getECLevel();
    if (ecLevel != null) {
        result.putMetadata(ResultMetadataType.ERROR_CORRECTION_LEVEL, ecLevel);
    }
    return result;
}
 
Example 8
public DetectorResult[] detectMulti(Map<DecodeHintType,?> hints) throws NotFoundException {
  BitMatrix image = getImage();
  ResultPointCallback resultPointCallback =
      hints == null ? null : (ResultPointCallback) hints.get(DecodeHintType.NEED_RESULT_POINT_CALLBACK);
  MultiFinderPatternFinder finder = new MultiFinderPatternFinder(image, resultPointCallback);
  FinderPatternInfo[] infos = finder.findMulti(hints);

  if (infos.length == 0) {
    throw NotFoundException.getNotFoundInstance();
  }

  List<DetectorResult> result = new ArrayList<>();
  for (FinderPatternInfo info : infos) {
    try {
      result.add(processFinderPatternInfo(info));
    } catch (ReaderException e) {
      // ignore
    }
  }
  if (result.isEmpty()) {
    return EMPTY_DETECTOR_RESULTS;
  } else {
    return result.toArray(new DetectorResult[result.size()]);
  }
}
 
Example 9
Source Project: barcodescanner-lib-aar   Source File: Detector.java    License: MIT License 6 votes vote down vote up
/**
 * <p>Detects a PDF417 Code in an image. Only checks 0 and 180 degree rotations.</p>
 *
 * @param image barcode image to decode
 * @param hints optional hints to detector
 * @param multiple if true, then the image is searched for multiple codes. If false, then at most one code will
 * be found and returned
 * @return {@link PDF417DetectorResult} encapsulating results of detecting a PDF417 code
 * @throws NotFoundException if no PDF417 Code can be found
 */
public static PDF417DetectorResult detect(BinaryBitmap image, Map<DecodeHintType,?> hints, boolean multiple)
    throws NotFoundException {
  // TODO detection improvement, tryHarder could try several different luminance thresholds/blackpoints or even 
  // different binarizers
  //boolean tryHarder = hints != null && hints.containsKey(DecodeHintType.TRY_HARDER);

  BitMatrix bitMatrix = image.getBlackMatrix();

  List<ResultPoint[]> barcodeCoordinates = detect(multiple, bitMatrix);
  if (barcodeCoordinates.isEmpty()) {
    bitMatrix = bitMatrix.clone();
    bitMatrix.rotate180();
    barcodeCoordinates = detect(multiple, bitMatrix);
  }
  return new PDF417DetectorResult(bitMatrix, barcodeCoordinates);
}
 
Example 10
Source Project: ScreenCapture   Source File: RSS14Reader.java    License: MIT License 6 votes vote down vote up
@Override
public Result decodeRow(int rowNumber,
                        BitArray row,
                        Map<DecodeHintType,?> hints) throws NotFoundException {
  Pair leftPair = decodePair(row, false, rowNumber, hints);
  addOrTally(possibleLeftPairs, leftPair);
  row.reverse();
  Pair rightPair = decodePair(row, true, rowNumber, hints);
  addOrTally(possibleRightPairs, rightPair);
  row.reverse();
  for (Pair left : possibleLeftPairs) {
    if (left.getCount() > 1) {
      for (Pair right : possibleRightPairs) {
        if (right.getCount() > 1 && checkChecksum(left, right)) {
          return constructResult(left, right);
        }
      }
    }
  }
  throw NotFoundException.getNotFoundInstance();
}
 
Example 11
private static Result[] decode(BinaryBitmap image, Map<DecodeHintType, ?> hints, boolean multiple) 
    throws NotFoundException, FormatException, ChecksumException {
  List<Result> results = new ArrayList<>();
  PDF417DetectorResult detectorResult = Detector.detect(image, hints, multiple);
  for (ResultPoint[] points : detectorResult.getPoints()) {
    DecoderResult decoderResult = PDF417ScanningDecoder.decode(detectorResult.getBits(), points[4], points[5],
        points[6], points[7], getMinCodewordWidth(points), getMaxCodewordWidth(points));
    Result result = new Result(decoderResult.getText(), decoderResult.getRawBytes(), points, BarcodeFormat.PDF_417);
    result.putMetadata(ResultMetadataType.ERROR_CORRECTION_LEVEL, decoderResult.getECLevel());
    PDF417ResultMetadata pdf417ResultMetadata = (PDF417ResultMetadata) decoderResult.getOther();
    if (pdf417ResultMetadata != null) {
      result.putMetadata(ResultMetadataType.PDF417_EXTRA_METADATA, pdf417ResultMetadata);
    }
    results.add(result);
  }
  return results.toArray(new Result[results.size()]);
}
 
Example 12
Source Project: ScreenCapture   Source File: MultiDetector.java    License: MIT License 6 votes vote down vote up
public DetectorResult[] detectMulti(Map<DecodeHintType,?> hints) throws NotFoundException {
  BitMatrix image = getImage();
  ResultPointCallback resultPointCallback =
      hints == null ? null : (ResultPointCallback) hints.get(DecodeHintType.NEED_RESULT_POINT_CALLBACK);
  MultiFinderPatternFinder finder = new MultiFinderPatternFinder(image, resultPointCallback);
  FinderPatternInfo[] infos = finder.findMulti(hints);

  if (infos.length == 0) {
    throw NotFoundException.getNotFoundInstance();
  }

  List<DetectorResult> result = new ArrayList<>();
  for (FinderPatternInfo info : infos) {
    try {
      result.add(processFinderPatternInfo(info));
    } catch (ReaderException e) {
      // ignore
    }
  }
  if (result.isEmpty()) {
    return EMPTY_DETECTOR_RESULTS;
  } else {
    return result.toArray(new DetectorResult[result.size()]);
  }
}
 
Example 13
Source Project: myapplication   Source File: DecodeUtils.java    License: Apache License 2.0 6 votes vote down vote up
private Map<DecodeHintType, Object> changeZXingDecodeDataMode() {
    Map<DecodeHintType, Object> hints = new EnumMap<DecodeHintType, Object>(DecodeHintType.class);
    Collection<BarcodeFormat> decodeFormats = new ArrayList<BarcodeFormat>();

    switch (mDataMode) {
        case DECODE_DATA_MODE_ALL:
            decodeFormats.addAll(DecodeFormatManager.getBarCodeFormats());
            decodeFormats.addAll(DecodeFormatManager.getQrCodeFormats());
            break;

        case DECODE_DATA_MODE_QRCODE:
            decodeFormats.addAll(DecodeFormatManager.getQrCodeFormats());
            break;

        case DECODE_DATA_MODE_BARCODE:
            decodeFormats.addAll(DecodeFormatManager.getBarCodeFormats());
            break;
    }
    hints.put(DecodeHintType.POSSIBLE_FORMATS, decodeFormats);

    return hints;
}
 
Example 14
Source Project: barterli_android   Source File: DecodedBitStreamParser.java    License: Apache License 2.0 5 votes vote down vote up
private static void decodeByteSegment(BitSource bits, StringBuilder result, int count, CharacterSetECI currentCharacterSetECI,
        Collection<byte[]> byteSegments, Map<DecodeHintType, ?> hints) throws FormatException {
    // Don't crash trying to read more bits than we have available.
    if (count << 3 > bits.available()) {
        throw FormatException.getFormatInstance();
    }

    byte[] readBytes = new byte[count];
    for (int i = 0; i < count; i++) {
        readBytes[i] = (byte) bits.readBits(8);
    }
    String encoding;
    if (currentCharacterSetECI == null) {
        // The spec isn't clear on this mode; see
        // section 6.4.5: t does not say which encoding to assuming
        // upon decoding. I have seen ISO-8859-1 used as well as
        // Shift_JIS -- without anything like an ECI designator to
        // give a hint.
        encoding = StringUtils.guessEncoding(readBytes, hints);
    } else {
        encoding = currentCharacterSetECI.name();
    }
    try {
        result.append(new String(readBytes, encoding));
    } catch (UnsupportedEncodingException uce) {
        throw FormatException.getFormatInstance();
    }
    byteSegments.add(readBytes);
}
 
Example 15
Source Project: BarcodeEye   Source File: DecodeHintManager.java    License: Apache License 2.0 5 votes vote down vote up
static Map<DecodeHintType, Object> parseDecodeHints(Intent intent) {
  Bundle extras = intent.getExtras();
  if (extras == null || extras.isEmpty()) {
    return null;
  }
  Map<DecodeHintType,Object> hints = new EnumMap<DecodeHintType,Object>(DecodeHintType.class);

  for (DecodeHintType hintType: DecodeHintType.values()) {

    if (hintType == DecodeHintType.CHARACTER_SET ||
        hintType == DecodeHintType.NEED_RESULT_POINT_CALLBACK ||
        hintType == DecodeHintType.POSSIBLE_FORMATS) {
      continue; // This hint is specified in another way
    }

    String hintName = hintType.name();
    if (extras.containsKey(hintName)) {
      if (hintType.getValueType().equals(Void.class)) {
        // Void hints are just flags: use the constant specified by the DecodeHintType
        hints.put(hintType, Boolean.TRUE);
      } else {
        Object hintData = extras.get(hintName);
        if (hintType.getValueType().isInstance(hintData)) {
          hints.put(hintType, hintData);
        } else {
          Log.w(TAG, "Ignoring hint " + hintType + " because it is not assignable from " + hintData);
        }
      }
    }
  }

  Log.i(TAG, "Hints from the Intent: " + hints);
  return hints;
}
 
Example 16
Source Project: StarBarcode   Source File: AbsCodec.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 使用RGB解析bitmap
 *
 * @param data
 * @param width
 * @param height
 * @param hintTypeMap
 * @return
 */
private Result decodeFromRGB(int[] data, int width, int height, Map<DecodeHintType, ?> hintTypeMap) {
    RGBLuminanceSource source = new RGBLuminanceSource(width, height, data);
    BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(source));
    try {
        return new MultiFormatReader().decode(binaryBitmap, hintTypeMap);
    } catch (NotFoundException e) {
        e.printStackTrace();
    }
    return null;
}
 
Example 17
Source Project: ZXing-Orient   Source File: Decoder.java    License: Apache License 2.0 5 votes vote down vote up
public DecoderResult decode(BitMatrix bits,
                            Map<DecodeHintType,?> hints) throws FormatException, ChecksumException {
  BitMatrixParser parser = new BitMatrixParser(bits);
  byte[] codewords = parser.readCodewords();

  correctErrors(codewords, 0, 10, 10, ALL);
  int mode = codewords[0] & 0x0F;
  byte[] datawords;
  switch (mode) {
    case 2:
    case 3:
    case 4:
      correctErrors(codewords, 20, 84, 40, EVEN);
      correctErrors(codewords, 20, 84, 40, ODD);
      datawords = new byte[94];
      break;
    case 5:
      correctErrors(codewords, 20, 68, 56, EVEN);
      correctErrors(codewords, 20, 68, 56, ODD);
      datawords = new byte[78];
      break;
    default:
      throw FormatException.getFormatInstance();
  }

  System.arraycopy(codewords, 0, datawords, 0, 10);
  System.arraycopy(codewords, 20, datawords, 10, datawords.length - 10);

  return DecodedBitStreamParser.decode(datawords, mode);
}
 
Example 18
Source Project: Android   Source File: DecodeThread.java    License: MIT License 5 votes vote down vote up
public DecodeThread(BaseScanActivity activity, int decodeMode) {

		this.activity = activity;
		handlerInitLatch = new CountDownLatch(1);

		hints = new EnumMap<>(DecodeHintType.class);

		Collection<BarcodeFormat> decodeFormats = new ArrayList<>();
		decodeFormats.addAll(EnumSet.of(BarcodeFormat.AZTEC));
		decodeFormats.addAll(EnumSet.of(BarcodeFormat.PDF_417));

		switch (decodeMode) {
		case BARCODE_MODE:
			decodeFormats.addAll(DecodeFormatManager.getBarCodeFormats());
			break;

		case QRCODE_MODE:
			decodeFormats.addAll(DecodeFormatManager.getQrCodeFormats());
			break;

		case ALL_MODE:
			decodeFormats.addAll(DecodeFormatManager.getBarCodeFormats());
			decodeFormats.addAll(DecodeFormatManager.getQrCodeFormats());
			break;

		default:
			break;
		}

		hints.put(DecodeHintType.POSSIBLE_FORMATS, decodeFormats);
	}
 
Example 19
Source Project: weex   Source File: Detector.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * <p>Detects a QR Code in an image.</p>
 *
 * @param hints optional hints to detector
 * @return {@link DetectorResult} encapsulating results of detecting a QR Code
 * @throws NotFoundException if QR Code cannot be found
 * @throws FormatException if a QR Code cannot be decoded
 */
public final DetectorResult detect(Map<DecodeHintType,?> hints) throws NotFoundException, FormatException {

  resultPointCallback = hints == null ? null :
      (ResultPointCallback) hints.get(DecodeHintType.NEED_RESULT_POINT_CALLBACK);

  FinderPatternFinder finder = new FinderPatternFinder(image, resultPointCallback);
  FinderPatternInfo info = finder.find(hints);

  return processFinderPatternInfo(info);
}
 
Example 20
Source Project: o2oa   Source File: DecodeThread.java    License: GNU Affero General Public License v3.0 5 votes vote down vote up
DecodeThread(CaptureActivity activity,
               Vector<BarcodeFormat> decodeFormats,
               String characterSet,
               ResultPointCallback resultPointCallback) {

    this.activity = activity;
    handlerInitLatch = new CountDownLatch(1);

    hints = new Hashtable<DecodeHintType, Object>(4);

    //没必要支持全部的格式 我们只需要二维码 加速解码
    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);
    hints.put(DecodeHintType.TRY_HARDER,  Boolean.TRUE);

    if (characterSet != null) {
      hints.put(DecodeHintType.CHARACTER_SET, characterSet);
    }else {
      hints.put(DecodeHintType.CHARACTER_SET, "utf-8");
    }

    hints.put(DecodeHintType.NEED_RESULT_POINT_CALLBACK, resultPointCallback);
  }
 
Example 21
Source Project: barcodescanner-lib-aar   Source File: OneDReader.java    License: MIT License 5 votes vote down vote up
@Override
public Result decode(BinaryBitmap image,
                     Map<DecodeHintType,?> hints) throws NotFoundException, FormatException {
  try {
    return doDecode(image, hints);
  } catch (NotFoundException nfe) {
    boolean tryHarder = hints != null && hints.containsKey(DecodeHintType.TRY_HARDER);
    if (tryHarder && image.isRotateSupported()) {
      BinaryBitmap rotatedImage = image.rotateCounterClockwise();
      Result result = doDecode(rotatedImage, hints);
      // Record that we found it rotated 90 degrees CCW / 270 degrees CW
      Map<ResultMetadataType,?> metadata = result.getResultMetadata();
      int orientation = 270;
      if (metadata != null && metadata.containsKey(ResultMetadataType.ORIENTATION)) {
        // But if we found it reversed in doDecode(), add in that result here:
        orientation = (orientation +
            (Integer) metadata.get(ResultMetadataType.ORIENTATION)) % 360;
      }
      result.putMetadata(ResultMetadataType.ORIENTATION, orientation);
      // Update result points
      ResultPoint[] points = result.getResultPoints();
      if (points != null) {
        int height = rotatedImage.getHeight();
        for (int i = 0; i < points.length; i++) {
          points[i] = new ResultPoint(height - points[i].getY() - 1, points[i].getX());
        }
      }
      return result;
    } else {
      throw nfe;
    }
  }
}
 
Example 22
Source Project: code-scanner   Source File: Decoder.java    License: MIT License 5 votes vote down vote up
public Decoder(@NonNull final StateListener stateListener,
        @NonNull final List<BarcodeFormat> formats, @Nullable final DecodeCallback callback) {
    mReader = new MultiFormatReader();
    mDecoderThread = new DecoderThread();
    mHints = new EnumMap<>(DecodeHintType.class);
    mHints.put(DecodeHintType.POSSIBLE_FORMATS, formats);
    mReader.setHints(mHints);
    mCallback = callback;
    mStateListener = stateListener;
    mState = State.INITIALIZED;
}
 
Example 23
Source Project: StarBarcode   Source File: BarCodeReaderManager.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 添加指定条码格式
 *
 * @param formats
 */
void addBarCodeFormat(Collection<BarcodeFormat> formats) {
    decodeFormats.addAll(formats);
    hintTypeMap.put(DecodeHintType.POSSIBLE_FORMATS, decodeFormats);
    if (formats.contains(BarcodeFormat.QR_CODE))
        hintTypeMap.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
    hintTypeMap.put(DecodeHintType.CHARACTER_SET, "utf-8");
    multiFormatReader.setHints(hintTypeMap);
}
 
Example 24
Source Project: RipplePower   Source File: MultiFormatOneDReader.java    License: Apache License 2.0 5 votes vote down vote up
public MultiFormatOneDReader(Map<DecodeHintType, ?> hints) {
	@SuppressWarnings("unchecked")
	Collection<BarcodeFormat> possibleFormats = hints == null ? null
			: (Collection<BarcodeFormat>) hints.get(DecodeHintType.POSSIBLE_FORMATS);
	boolean useCode39CheckDigit = hints != null && hints.get(DecodeHintType.ASSUME_CODE_39_CHECK_DIGIT) != null;
	Collection<OneDReader> readers = new ArrayList<>();
	if (possibleFormats != null) {
		if (possibleFormats.contains(BarcodeFormat.EAN_13) || possibleFormats.contains(BarcodeFormat.UPC_A)
				|| possibleFormats.contains(BarcodeFormat.EAN_8) || possibleFormats.contains(BarcodeFormat.UPC_E)) {
			readers.add(new MultiFormatUPCEANReader(hints));
		}
		if (possibleFormats.contains(BarcodeFormat.CODE_39)) {
			readers.add(new Code39Reader(useCode39CheckDigit));
		}
		if (possibleFormats.contains(BarcodeFormat.CODE_93)) {
			readers.add(new Code93Reader());
		}
		if (possibleFormats.contains(BarcodeFormat.CODE_128)) {
			readers.add(new Code128Reader());
		}
		if (possibleFormats.contains(BarcodeFormat.ITF)) {
			readers.add(new ITFReader());
		}
		if (possibleFormats.contains(BarcodeFormat.CODABAR)) {
			readers.add(new CodaBarReader());
		}
	}
	if (readers.isEmpty()) {
		readers.add(new MultiFormatUPCEANReader(hints));
		readers.add(new Code39Reader());
		readers.add(new CodaBarReader());
		readers.add(new Code93Reader());
		readers.add(new Code128Reader());
		readers.add(new ITFReader());
	}
	this.readers = readers.toArray(new OneDReader[readers.size()]);
}
 
Example 25
Source Project: imsdk-android   Source File: DecodeThread.java    License: MIT License 5 votes vote down vote up
public DecodeThread(CaptureActivity activity, int decodeMode) {

        this.activity = activity;
        handlerInitLatch = new CountDownLatch(1);

        hints = new EnumMap<DecodeHintType, Object>(DecodeHintType.class);

        Collection<BarcodeFormat> decodeFormats = new ArrayList<BarcodeFormat>();
        decodeFormats.addAll(EnumSet.of(BarcodeFormat.AZTEC));
        decodeFormats.addAll(EnumSet.of(BarcodeFormat.PDF_417));

        switch (decodeMode) {
            case BARCODE_MODE:
                decodeFormats.addAll(DecodeFormatManager.getBarCodeFormats());
                break;

            case QRCODE_MODE:
                decodeFormats.addAll(DecodeFormatManager.getQrCodeFormats());
                break;

            case ALL_MODE:
                decodeFormats.addAll(DecodeFormatManager.getBarCodeFormats());
                decodeFormats.addAll(DecodeFormatManager.getQrCodeFormats());
                break;

            default:
                break;
        }

        hints.put(DecodeHintType.POSSIBLE_FORMATS, decodeFormats);
    }
 
Example 26
private static void decodeByteSegment(BitSource bits,
                                      StringBuilder result,
                                      int count,
                                      CharacterSetECI currentCharacterSetECI,
                                      Collection<byte[]> byteSegments,
                                      Map<DecodeHintType,?> hints) throws FormatException {
  // Don't crash trying to read more bits than we have available.
  if (8 * count > bits.available()) {
    throw FormatException.getFormatInstance();
  }

  byte[] readBytes = new byte[count];
  for (int i = 0; i < count; i++) {
    readBytes[i] = (byte) bits.readBits(8);
  }
  String encoding;
  if (currentCharacterSetECI == null) {
    // The spec isn't clear on this mode; see
    // section 6.4.5: t does not say which encoding to assuming
    // upon decoding. I have seen ISO-8859-1 used as well as
    // Shift_JIS -- without anything like an ECI designator to
    // give a hint.
    encoding = StringUtils.guessEncoding(readBytes, hints);
  } else {
    encoding = currentCharacterSetECI.name();
  }
  try {
    result.append(new String(readBytes, encoding));
  } catch (UnsupportedEncodingException ignored) {
    throw FormatException.getFormatInstance();
  }
  byteSegments.add(readBytes);
}
 
Example 27
Source Project: mobile-manager-tool   Source File: DecodeThread.java    License: MIT License 5 votes vote down vote up
DecodeThread(CaptureActivity activity,
               Vector<BarcodeFormat> decodeFormats,
               String characterSet,
               ResultPointCallback resultPointCallback) {

    this.activity = activity;
    handlerInitLatch = new CountDownLatch(1);

    hints = new Hashtable<DecodeHintType, Object>(3);

//    // The prefs can't change while the thread is running, so pick them up once here.
//    if (decodeFormats == null || decodeFormats.isEmpty()) {
//      SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
//      decodeFormats = new Vector<BarcodeFormat>();
//      if (prefs.getBoolean(PreferencesActivity.KEY_DECODE_1D, true)) {
//        decodeFormats.addAll(DecodeFormatManager.ONE_D_FORMATS);
//      }
//      if (prefs.getBoolean(PreferencesActivity.KEY_DECODE_QR, true)) {
//        decodeFormats.addAll(DecodeFormatManager.QR_CODE_FORMATS);
//      }
//      if (prefs.getBoolean(PreferencesActivity.KEY_DECODE_DATA_MATRIX, true)) {
//        decodeFormats.addAll(DecodeFormatManager.DATA_MATRIX_FORMATS);
//      }
//    }
    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);

    if (characterSet != null) {
      hints.put(DecodeHintType.CHARACTER_SET, characterSet);
    }

    hints.put(DecodeHintType.NEED_RESULT_POINT_CALLBACK, resultPointCallback);
  }
 
Example 28
Source Project: Tesseract-OCR-Scanner   Source File: QRCodeMultiReader.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Result[] decodeMultiple(BinaryBitmap image, Map<DecodeHintType,?> hints) throws NotFoundException {
  List<Result> results = new ArrayList<>();
  DetectorResult[] detectorResults = new MultiDetector(image.getBlackMatrix()).detectMulti(hints);
  for (DetectorResult detectorResult : detectorResults) {
    try {
      DecoderResult decoderResult = getDecoder().decode(detectorResult.getBits(), hints);
      ResultPoint[] points = detectorResult.getPoints();
      // If the code was mirrored: swap the bottom-left and the top-right points.
      if (decoderResult.getOther() instanceof QRCodeDecoderMetaData) {
        ((QRCodeDecoderMetaData) decoderResult.getOther()).applyMirroredCorrection(points);
      }
      Result result = new Result(decoderResult.getText(), decoderResult.getRawBytes(), points,
                                 BarcodeFormat.QR_CODE);
      List<byte[]> byteSegments = decoderResult.getByteSegments();
      if (byteSegments != null) {
        result.putMetadata(ResultMetadataType.BYTE_SEGMENTS, byteSegments);
      }
      String ecLevel = decoderResult.getECLevel();
      if (ecLevel != null) {
        result.putMetadata(ResultMetadataType.ERROR_CORRECTION_LEVEL, ecLevel);
      }
      if (decoderResult.hasStructuredAppend()) {
        result.putMetadata(ResultMetadataType.STRUCTURED_APPEND_SEQUENCE,
                           decoderResult.getStructuredAppendSequenceNumber());
        result.putMetadata(ResultMetadataType.STRUCTURED_APPEND_PARITY,
                           decoderResult.getStructuredAppendParity());
      }
      results.add(result);
    } catch (ReaderException re) {
      // ignore and continue 
    }
  }
  if (results.isEmpty()) {
    return EMPTY_RESULT_ARRAY;
  } else {
    results = processStructuredAppend(results);
    return results.toArray(new Result[results.size()]);
  }
}
 
Example 29
Source Project: ZXing-Orient   Source File: MultiFormatOneDReader.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Result decodeRow(int rowNumber,
                        BitArray row,
                        Map<DecodeHintType,?> hints) throws NotFoundException {
  for (OneDReader reader : readers) {
    try {
      return reader.decodeRow(rowNumber, row, hints);
    } catch (ReaderException re) {
      // continue
    }
  }

  throw NotFoundException.getNotFoundInstance();
}
 
Example 30
Source Project: green_android   Source File: ScanActivity.java    License: GNU General Public License v3.0 5 votes vote down vote up
private void decode(final byte[] data)
{
    final PlanarYUVLuminanceSource source = cameraManager.buildLuminanceSource(data);
    final BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));

    try {
        hints.put(DecodeHintType.NEED_RESULT_POINT_CALLBACK,
                  (ResultPointCallback) dot -> runOnUiThread(() -> scannerView.addDot(dot)));
        final Result scanResult = reader.decode(bitmap, hints);

        final int thumbnailWidth = source.getThumbnailWidth();
        final int thumbnailHeight = source.getThumbnailHeight();
        final float thumbnailScaleFactor = (float) thumbnailWidth / source.getWidth();

        final Bitmap thumbnailImage = Bitmap.createBitmap(thumbnailWidth, thumbnailHeight,
                                                          Bitmap.Config.ARGB_8888);
        thumbnailImage.setPixels(
            source.renderThumbnail(), 0, thumbnailWidth, 0, 0, thumbnailWidth, thumbnailHeight);

        runOnUiThread(() -> handleResult(scanResult, thumbnailImage, thumbnailScaleFactor));
    } catch (final ReaderException x) {
        // retry
        cameraHandler.post(fetchAndDecodeRunnable);
    } finally {
        reader.reset();
    }
}