Java Code Examples for com.google.zxing.Result#getBarcodeFormat()

The following examples show how to use com.google.zxing.Result#getBarcodeFormat() . 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 check out the related API usage on the sidebar.
Example 1
Source File: GenericMultipleBarcodeReader.java    From ScreenCapture with MIT License 6 votes vote down vote up
private static Result translateResultPoints(Result result, int xOffset, int yOffset) {
  ResultPoint[] oldResultPoints = result.getResultPoints();
  if (oldResultPoints == null) {
    return result;
  }
  ResultPoint[] newResultPoints = new ResultPoint[oldResultPoints.length];
  for (int i = 0; i < oldResultPoints.length; i++) {
    ResultPoint oldPoint = oldResultPoints[i];
    if (oldPoint != null) {
      newResultPoints[i] = new ResultPoint(oldPoint.getX() + xOffset, oldPoint.getY() + yOffset);
    }
  }
  Result newResult = new Result(result.getText(),
                                result.getRawBytes(),
                                result.getNumBits(),
                                newResultPoints,
                                result.getBarcodeFormat(),
                                result.getTimestamp());
  newResult.putAllMetadata(result.getResultMetadata());
  return newResult;
}
 
Example 2
Source File: CaptureActivity.java    From barcodescanner-lib-aar with MIT License 6 votes vote down vote up
/**
 * Superimpose a line for 1D or dots for 2D to highlight the key features of the barcode.
 *
 * @param barcode   A bitmap of the captured image.
 * @param scaleFactor amount by which thumbnail was scaled
 * @param rawResult The decoded results which contains the points to draw.
 */
private void drawResultPoints(Bitmap barcode, float scaleFactor, Result rawResult) {
  ResultPoint[] points = rawResult.getResultPoints();
  if (points != null && points.length > 0) {
    Canvas canvas = new Canvas(barcode);
    Paint paint = new Paint();
    paint.setColor(getResources().getColor(R.color.result_points));
    if (points.length == 2) {
      paint.setStrokeWidth(4.0f);
      drawLine(canvas, paint, points[0], points[1], scaleFactor);
    } else if (points.length == 4 &&
               (rawResult.getBarcodeFormat() == BarcodeFormat.UPC_A ||
                rawResult.getBarcodeFormat() == BarcodeFormat.EAN_13)) {
      // Hacky special case -- draw two lines, for the barcode and metadata
      drawLine(canvas, paint, points[0], points[1], scaleFactor);
      drawLine(canvas, paint, points[2], points[3], scaleFactor);
    } else {
      paint.setStrokeWidth(10.0f);
      for (ResultPoint point : points) {
        if (point != null) {
          canvas.drawPoint(scaleFactor * point.getX(), scaleFactor * point.getY(), paint);
        }
      }
    }
  }
}
 
Example 3
Source File: GenericMultipleBarcodeReader.java    From analyzer-of-android-for-Apache-Weex with Apache License 2.0 6 votes vote down vote up
private static Result translateResultPoints(Result result, int xOffset, int yOffset) {
  ResultPoint[] oldResultPoints = result.getResultPoints();
  if (oldResultPoints == null) {
    return result;
  }
  ResultPoint[] newResultPoints = new ResultPoint[oldResultPoints.length];
  for (int i = 0; i < oldResultPoints.length; i++) {
    ResultPoint oldPoint = oldResultPoints[i];
    if (oldPoint != null) {
      newResultPoints[i] = new ResultPoint(oldPoint.getX() + xOffset, oldPoint.getY() + yOffset);
    }
  }
  Result newResult = new Result(result.getText(), result.getRawBytes(), newResultPoints, result.getBarcodeFormat());
  newResult.putAllMetadata(result.getResultMetadata());
  return newResult;
}
 
Example 4
Source File: ScannerFragment.java    From xmrwallet with Apache License 2.0 6 votes vote down vote up
@Override
public void handleResult(Result rawResult) {
    if ((rawResult.getBarcodeFormat() == BarcodeFormat.QR_CODE)) {
        if (onScannedListener.onScanned(rawResult.getText())) {
            return;
        } else {
            Toast.makeText(getActivity(), getString(R.string.send_qr_address_invalid), Toast.LENGTH_SHORT).show();
        }
    } else {
        Toast.makeText(getActivity(), getString(R.string.send_qr_invalid), Toast.LENGTH_SHORT).show();
    }

    // Note from dm77:
    // * Wait 2 seconds to resume the preview.
    // * On older devices continuously stopping and resuming camera preview can result in freezing the app.
    // * I don't know why this is the case but I don't have the time to figure out.
    Handler handler = new Handler();
    handler.postDelayed(new

                                Runnable() {
                                    @Override
                                    public void run() {
                                        mScannerView.resumeCameraPreview(ScannerFragment.this);
                                    }
                                }, 2000);
}
 
Example 5
Source File: CaptureActivity.java    From Study_Android_Demo with Apache License 2.0 6 votes vote down vote up
/**
 * Superimpose a line for 1D or dots for 2D to highlight the key features of the barcode.
 *
 * @param barcode   A bitmap of the captured image.
 * @param scaleFactor amount by which thumbnail was scaled
 * @param rawResult The decoded results which contains the points to draw.
 */
private void drawResultPoints(Bitmap barcode, float scaleFactor, Result rawResult) {
  ResultPoint[] points = rawResult.getResultPoints();
  if (points != null && points.length > 0) {
    Canvas canvas = new Canvas(barcode);
    Paint paint = new Paint();
    paint.setColor(getResources().getColor(R.color.result_points));
    if (points.length == 2) {
      paint.setStrokeWidth(4.0f);
      drawLine(canvas, paint, points[0], points[1], scaleFactor);
    } else if (points.length == 4 &&
               (rawResult.getBarcodeFormat() == BarcodeFormat.UPC_A ||
                rawResult.getBarcodeFormat() == BarcodeFormat.EAN_13)) {
      // Hacky special case -- draw two lines, for the barcode and metadata
      drawLine(canvas, paint, points[0], points[1], scaleFactor);
      drawLine(canvas, paint, points[2], points[3], scaleFactor);
    } else {
      paint.setStrokeWidth(10.0f);
      for (ResultPoint point : points) {
        if (point != null) {
          canvas.drawPoint(scaleFactor * point.getX(), scaleFactor * point.getY(), paint);
        }
      }
    }
  }
}
 
Example 6
Source File: ProductResultParser.java    From weex with Apache License 2.0 6 votes vote down vote up
@Override
public ProductParsedResult parse(Result result) {
  BarcodeFormat format = result.getBarcodeFormat();
  if (!(format == BarcodeFormat.UPC_A || format == BarcodeFormat.UPC_E ||
        format == BarcodeFormat.EAN_8 || format == BarcodeFormat.EAN_13)) {
    return null;
  }
  String rawText = getMassagedText(result);
  if (!isStringOfDigits(rawText, rawText.length())) {
    return null;
  }
  // Not actually checking the checksum again here    

  String normalizedProductID;
  // Expand UPC-E for purposes of searching
  if (format == BarcodeFormat.UPC_E && rawText.length() == 8) {
    normalizedProductID = UPCEReader.convertUPCEtoUPCA(rawText);
  } else {
    normalizedProductID = rawText;
  }

  return new ProductParsedResult(rawText, normalizedProductID);
}
 
Example 7
Source File: ISBNResultParser.java    From RipplePower with Apache License 2.0 6 votes vote down vote up
/**
 * See <a href="http://www.bisg.org/isbn-13/for.dummies.html">ISBN-13 For
 * Dummies</a>
 */
@Override
public ISBNParsedResult parse(Result result) {
	BarcodeFormat format = result.getBarcodeFormat();
	if (format != BarcodeFormat.EAN_13) {
		return null;
	}
	String rawText = getMassagedText(result);
	int length = rawText.length();
	if (length != 13) {
		return null;
	}
	if (!rawText.startsWith("978") && !rawText.startsWith("979")) {
		return null;
	}

	return new ISBNParsedResult(rawText);
}
 
Example 8
Source File: ISBNResultParser.java    From reacteu-app with MIT License 6 votes vote down vote up
/**
 * See <a href="http://www.bisg.org/isbn-13/for.dummies.html">ISBN-13 For Dummies</a>
 */
@Override
public ISBNParsedResult parse(Result result) {
  BarcodeFormat format = result.getBarcodeFormat();
  if (format != BarcodeFormat.EAN_13) {
    return null;
  }
  String rawText = getMassagedText(result);
  int length = rawText.length();
  if (length != 13) {
    return null;
  }
  if (!rawText.startsWith("978") && !rawText.startsWith("979")) {
    return null;
  }
 
  return new ISBNParsedResult(rawText);
}
 
Example 9
Source File: GenericMultipleBarcodeReader.java    From MiBandDecompiled with Apache License 2.0 6 votes vote down vote up
private static Result a(Result result, int i, int j)
{
    ResultPoint aresultpoint[] = result.getResultPoints();
    if (aresultpoint == null)
    {
        return result;
    }
    ResultPoint aresultpoint1[] = new ResultPoint[aresultpoint.length];
    for (int k = 0; k < aresultpoint.length; k++)
    {
        ResultPoint resultpoint = aresultpoint[k];
        aresultpoint1[k] = new ResultPoint(resultpoint.getX() + (float)i, resultpoint.getY() + (float)j);
    }

    return new Result(result.getText(), result.getRawBytes(), aresultpoint1, result.getBarcodeFormat());
}
 
Example 10
Source File: CaptureActivity.java    From weex with Apache License 2.0 5 votes vote down vote up
/**
 * Superimpose a line for 1D or dots for 2D to highlight the key features of
 * the barcode.
 * 
 * @param barcode
 *            A bitmap of the captured image.
 * @param scaleFactor
 *            amount by which thumbnail was scaled
 * @param rawResult
 *            The decoded results which contains the points to draw.
 */
private void drawResultPoints(Bitmap barcode, float scaleFactor,
		Result rawResult) {
	ResultPoint[] points = rawResult.getResultPoints();
	if (points != null && points.length > 0) {
		Canvas canvas = new Canvas(barcode);
		Paint paint = new Paint();
		paint.setColor(getResources().getColor(R.color.result_points));
		if (points.length == 2) {
			paint.setStrokeWidth(4.0f);
			drawLine(canvas, paint, points[0], points[1], scaleFactor);
		} else if (points.length == 4
				&& (rawResult.getBarcodeFormat() == BarcodeFormat.UPC_A || rawResult
						.getBarcodeFormat() == BarcodeFormat.EAN_13)) {
			// Hacky special case -- draw two lines, for the barcode and
			// metadata
			drawLine(canvas, paint, points[0], points[1], scaleFactor);
			drawLine(canvas, paint, points[2], points[3], scaleFactor);
		} else {
			paint.setStrokeWidth(10.0f);
			for (ResultPoint point : points) {
				if (point != null) {
					canvas.drawPoint(scaleFactor * point.getX(),
							scaleFactor * point.getY(), paint);
				}
			}
		}
	}
}
 
Example 11
Source File: ZxingScanView.java    From ProjectX with Apache License 2.0 5 votes vote down vote up
/**
 * 为结果图添加识别效果
 *
 * @param result      扫描结果
 * @param barcode     结果图片
 * @param scaleFactor 缩放比
 * @param color       颜色
 */
@SuppressWarnings("unused")
public static void addResultPoints(Result result, Bitmap barcode, float scaleFactor, int color) {
    ResultPoint[] points = result.getResultPoints();
    if (points != null && points.length > 0) {
        Canvas canvas = new Canvas(barcode);
        Paint paint = new Paint();
        paint.setColor(color);
        if (points.length == 2) {
            paint.setStrokeWidth(4.0f);
            drawLine(canvas, paint, points[0], points[1], scaleFactor);
        } else if (points.length == 4 &&
                (result.getBarcodeFormat() == BarcodeFormat.UPC_A ||
                        result.getBarcodeFormat() == BarcodeFormat.EAN_13)) {
            // Hacky special case -- draw two lines, for the barcode and metadata
            drawLine(canvas, paint, points[0], points[1], scaleFactor);
            drawLine(canvas, paint, points[2], points[3], scaleFactor);
        } else {
            paint.setStrokeWidth(10.0f);
            for (ResultPoint point : points) {
                if (point != null) {
                    canvas.drawPoint(scaleFactor * point.getX(), scaleFactor * point.getY(), paint);
                }
            }
        }
    }
}
 
Example 12
Source File: VINResultParser.java    From weex with Apache License 2.0 5 votes vote down vote up
@Override
public VINParsedResult parse(Result result) {
  if (result.getBarcodeFormat() != BarcodeFormat.CODE_39) {
    return null;
  }
  String rawText = result.getText();
  rawText = IOQ.matcher(rawText).replaceAll("").trim();
  if (!AZ09.matcher(rawText).matches()) {
    return null;
  }
  try {
    if (!checkChecksum(rawText)) {
      return null;
    }
    String wmi = rawText.substring(0, 3);
    return new VINParsedResult(rawText,
        wmi,
        rawText.substring(3, 9),
        rawText.substring(9, 17),
        countryCode(wmi),
        rawText.substring(3, 8),
        modelYear(rawText.charAt(9)),
        rawText.charAt(10),
        rawText.substring(11));
  } catch (IllegalArgumentException iae) {
    return null;
  }
}
 
Example 13
Source File: VINResultParser.java    From barcodescanner-lib-aar with MIT License 5 votes vote down vote up
@Override
public VINParsedResult parse(Result result) {
  if (result.getBarcodeFormat() != BarcodeFormat.CODE_39) {
    return null;
  }
  String rawText = result.getText();
  rawText = IOQ.matcher(rawText).replaceAll("").trim();
  if (!AZ09.matcher(rawText).matches()) {
    return null;
  }
  try {
    if (!checkChecksum(rawText)) {
      return null;
    }
    String wmi = rawText.substring(0, 3);
    return new VINParsedResult(rawText,
        wmi,
        rawText.substring(3, 9),
        rawText.substring(9, 17),
        countryCode(wmi),
        rawText.substring(3, 8),
        modelYear(rawText.charAt(9)),
        rawText.charAt(10),
        rawText.substring(11));
  } catch (IllegalArgumentException iae) {
    return null;
  }
}
 
Example 14
Source File: CaptureActivity.java    From BarcodeEye with Apache License 2.0 5 votes vote down vote up
/**
 * Superimpose a line for 1D or dots for 2D to highlight the key features of
 * the barcode.
 *
 * @param barcode
 *            A bitmap of the captured image.
 * @param scaleFactor
 *            amount by which thumbnail was scaled
 * @param rawResult
 *            The decoded results which contains the points to draw.
 */
private static void drawResultPoints(Bitmap barcode, float scaleFactor,
        Result rawResult, int color) {
    ResultPoint[] points = rawResult.getResultPoints();
    if (points != null && points.length > 0) {
        Canvas canvas = new Canvas(barcode);
        Paint paint = new Paint();
        paint.setColor(color);
        if (points.length == 2) {
            paint.setStrokeWidth(4.0f);
            drawLine(canvas, paint, points[0], points[1], scaleFactor);
        } else if (points.length == 4
                && (rawResult.getBarcodeFormat() == BarcodeFormat.UPC_A || rawResult
                        .getBarcodeFormat() == BarcodeFormat.EAN_13)) {
            // Hacky special case -- draw two lines, for the barcode and metadata
            drawLine(canvas, paint, points[0], points[1], scaleFactor);
            drawLine(canvas, paint, points[2], points[3], scaleFactor);
        } else {
            paint.setStrokeWidth(10.0f);
            for (ResultPoint point : points) {
                if (point != null) {
                    canvas.drawPoint(scaleFactor * point.getX(),
                            scaleFactor * point.getY(), paint);
                }
            }
        }
    }
}
 
Example 15
Source File: CaptureActivity.java    From android-apps with MIT License 5 votes vote down vote up
/**
 * Superimpose a line for 1D or dots for 2D to highlight the key features of the barcode.
 *
 * @param barcode   A bitmap of the captured image.
 * @param rawResult The decoded results which contains the points to draw.
 */
private void drawResultPoints(Bitmap barcode, Result rawResult) {
  ResultPoint[] points = rawResult.getResultPoints();
  if (points != null && points.length > 0) {
    Canvas canvas = new Canvas(barcode);
    Paint paint = new Paint();
    paint.setColor(getResources().getColor(R.color.result_image_border));
    paint.setStrokeWidth(3.0f);
    paint.setStyle(Paint.Style.STROKE);
    Rect border = new Rect(2, 2, barcode.getWidth() - 2, barcode.getHeight() - 2);
    canvas.drawRect(border, paint);

    paint.setColor(getResources().getColor(R.color.result_points));
    if (points.length == 2) {
      paint.setStrokeWidth(4.0f);
      drawLine(canvas, paint, points[0], points[1]);
    } else if (points.length == 4 &&
               (rawResult.getBarcodeFormat() == BarcodeFormat.UPC_A ||
                rawResult.getBarcodeFormat() == BarcodeFormat.EAN_13)) {
      // Hacky special case -- draw two lines, for the barcode and metadata
      drawLine(canvas, paint, points[0], points[1]);
      drawLine(canvas, paint, points[2], points[3]);
    } else {
      paint.setStrokeWidth(10.0f);
      for (ResultPoint point : points) {
        canvas.drawPoint(point.getX(), point.getY(), paint);
      }
    }
  }
}
 
Example 16
Source File: CaptureActivity.java    From CodeScaner with MIT License 5 votes vote down vote up
/**
 * Superimpose a line for 1D or dots for 2D to highlight the key features of the barcode.
 *
 * @param barcode     A bitmap of the captured image.
 * @param scaleFactor amount by which thumbnail was scaled
 * @param rawResult   The decoded results which contains the points to draw.
 */
private void drawResultPoints(Bitmap barcode, float scaleFactor, Result rawResult) {
    ResultPoint[] points = rawResult.getResultPoints();
    if (points != null && points.length > 0) {
        Canvas canvas = new Canvas(barcode);
        Paint paint = new Paint();
        paint.setColor(getResources().getColor(R.color.result_points));
        if (points.length == 2) {
            paint.setStrokeWidth(4.0f);
            drawLine(canvas, paint, points[0], points[1], scaleFactor);
        } else if (points.length == 4 &&
                (rawResult.getBarcodeFormat() == BarcodeFormat.UPC_A ||
                        rawResult.getBarcodeFormat() == BarcodeFormat.EAN_13)) {
            // Hacky special case -- draw two lines, for the barcode and metadata
            drawLine(canvas, paint, points[0], points[1], scaleFactor);
            drawLine(canvas, paint, points[2], points[3], scaleFactor);
        } else {
            paint.setStrokeWidth(10.0f);
            for (ResultPoint point : points) {
                if (point != null) {
                    canvas.drawPoint(scaleFactor * point.getX(), scaleFactor * point.getY(),
                            paint);
                }
            }
        }
    }
}
 
Example 17
Source File: ActivityScanerCode.java    From RxTools-master with Apache License 2.0 5 votes vote down vote up
private void initDialogResult(Result result) {
    BarcodeFormat type = result.getBarcodeFormat();
    String realContent = result.getText();

    if (rxDialogSure == null) {
        rxDialogSure = new RxDialogSure(mContext);//提示弹窗
    }

    if (BarcodeFormat.QR_CODE.equals(type)) {
        rxDialogSure.setTitle("二维码扫描结果");
    } else if (BarcodeFormat.EAN_13.equals(type)) {
        rxDialogSure.setTitle("条形码扫描结果");
    } else {
        rxDialogSure.setTitle("扫描结果");
    }

    rxDialogSure.setContent(realContent);
    rxDialogSure.setSureListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            rxDialogSure.cancel();
        }
    });
    rxDialogSure.setOnCancelListener(new DialogInterface.OnCancelListener() {
        @Override
        public void onCancel(DialogInterface dialog) {
            if (handler != null) {
                // 连续扫描,不发送此消息扫描一次结束后就不能再次扫描
                handler.sendEmptyMessage(R.id.restart_preview);
            }
        }
    });

    if (!rxDialogSure.isShowing()) {
        rxDialogSure.show();
    }

    RxSPTool.putContent(mContext, RxConstants.SP_SCAN_CODE, RxDataTool.stringToInt(RxSPTool.getContent(mContext, RxConstants.SP_SCAN_CODE)) + 1 + "");
}
 
Example 18
Source File: MultiFormatUPCEANReader.java    From weex with Apache License 2.0 4 votes vote down vote up
@Override
public Result decodeRow(int rowNumber,
                        BitArray row,
                        Map<DecodeHintType,?> hints) throws NotFoundException {
  // Compute this location once and reuse it on multiple implementations
  int[] startGuardPattern = UPCEANReader.findStartGuardPattern(row);
  for (UPCEANReader reader : readers) {
    Result result;
    try {
      result = reader.decodeRow(rowNumber, row, startGuardPattern, hints);
    } catch (ReaderException ignored) {
      continue;
    }
    // Special case: a 12-digit code encoded in UPC-A is identical to a "0"
    // followed by those 12 digits encoded as EAN-13. Each will recognize such a code,
    // UPC-A as a 12-digit string and EAN-13 as a 13-digit string starting with "0".
    // Individually these are correct and their readers will both read such a code
    // and correctly call it EAN-13, or UPC-A, respectively.
    //
    // In this case, if we've been looking for both types, we'd like to call it
    // a UPC-A code. But for efficiency we only run the EAN-13 decoder to also read
    // UPC-A. So we special case it here, and convert an EAN-13 result to a UPC-A
    // result if appropriate.
    //
    // But, don't return UPC-A if UPC-A was not a requested format!
    boolean ean13MayBeUPCA =
        result.getBarcodeFormat() == BarcodeFormat.EAN_13 &&
            result.getText().charAt(0) == '0';
    @SuppressWarnings("unchecked")      
    Collection<BarcodeFormat> possibleFormats =
        hints == null ? null : (Collection<BarcodeFormat>) hints.get(DecodeHintType.POSSIBLE_FORMATS);
    boolean canReturnUPCA = possibleFormats == null || possibleFormats.contains(BarcodeFormat.UPC_A);

    if (ean13MayBeUPCA && canReturnUPCA) {
      // Transfer the metdata across
      Result resultUPCA = new Result(result.getText().substring(1),
                                     result.getRawBytes(),
                                     result.getResultPoints(),
                                     BarcodeFormat.UPC_A);
      resultUPCA.putAllMetadata(result.getResultMetadata());
      return resultUPCA;
    }
    return result;
  }

  throw NotFoundException.getNotFoundInstance();
}
 
Example 19
Source File: MultiFormatUPCEANReader.java    From QrCodeScanner with GNU General Public License v3.0 4 votes vote down vote up
@Override
public Result decodeRow(int rowNumber,
                        BitArray row,
                        Map<DecodeHintType,?> hints) throws NotFoundException {
  // Compute this location once and reuse it on multiple implementations
  int[] startGuardPattern = UPCEANReader.findStartGuardPattern(row);
  for (UPCEANReader reader : readers) {
    try {
      Result result = reader.decodeRow(rowNumber, row, startGuardPattern, hints);
      // Special case: a 12-digit code encoded in UPC-A is identical to a "0"
      // followed by those 12 digits encoded as EAN-13. Each will recognize such a code,
      // UPC-A as a 12-digit string and EAN-13 as a 13-digit string starting with "0".
      // Individually these are correct and their readers will both read such a code
      // and correctly call it EAN-13, or UPC-A, respectively.
      //
      // In this case, if we've been looking for both types, we'd like to call it
      // a UPC-A code. But for efficiency we only run the EAN-13 decoder to also read
      // UPC-A. So we special case it here, and convert an EAN-13 result to a UPC-A
      // result if appropriate.
      //
      // But, don't return UPC-A if UPC-A was not a requested format!
      boolean ean13MayBeUPCA =
          result.getBarcodeFormat() == BarcodeFormat.EAN_13 &&
              result.getText().charAt(0) == '0';
      @SuppressWarnings("unchecked")
      Collection<BarcodeFormat> possibleFormats =
          hints == null ? null : (Collection<BarcodeFormat>) hints.get(DecodeHintType.POSSIBLE_FORMATS);
      boolean canReturnUPCA = possibleFormats == null || possibleFormats.contains(BarcodeFormat.UPC_A);

      if (ean13MayBeUPCA && canReturnUPCA) {
        // Transfer the metdata across
        Result resultUPCA = new Result(result.getText().substring(1),
                                       result.getRawBytes(),
                                       result.getResultPoints(),
                                       BarcodeFormat.UPC_A);
        resultUPCA.putAllMetadata(result.getResultMetadata());
        return resultUPCA;
      }
      return result;
    } catch (ReaderException ignored) {
      // continue
    }
  }

  throw NotFoundException.getNotFoundInstance();
}
 
Example 20
Source File: MultiFormatUPCEANReader.java    From ScreenCapture with MIT License 4 votes vote down vote up
@Override
public Result decodeRow(int rowNumber,
                        BitArray row,
                        Map<DecodeHintType,?> hints) throws NotFoundException {
  // Compute this location once and reuse it on multiple implementations
  int[] startGuardPattern = UPCEANReader.findStartGuardPattern(row);
  for (UPCEANReader reader : readers) {
    try {
      Result result = reader.decodeRow(rowNumber, row, startGuardPattern, hints);
      // Special case: a 12-digit code encoded in UPC-A is identical to a "0"
      // followed by those 12 digits encoded as EAN-13. Each will recognize such a code,
      // UPC-A as a 12-digit string and EAN-13 as a 13-digit string starting with "0".
      // Individually these are correct and their readers will both read such a code
      // and correctly call it EAN-13, or UPC-A, respectively.
      //
      // In this case, if we've been looking for both types, we'd like to call it
      // a UPC-A code. But for efficiency we only run the EAN-13 decoder to also read
      // UPC-A. So we special case it here, and convert an EAN-13 result to a UPC-A
      // result if appropriate.
      //
      // But, don't return UPC-A if UPC-A was not a requested format!
      boolean ean13MayBeUPCA =
          result.getBarcodeFormat() == BarcodeFormat.EAN_13 &&
              result.getText().charAt(0) == '0';
      @SuppressWarnings("unchecked")
      Collection<BarcodeFormat> possibleFormats =
          hints == null ? null : (Collection<BarcodeFormat>) hints.get(DecodeHintType.POSSIBLE_FORMATS);
      boolean canReturnUPCA = possibleFormats == null || possibleFormats.contains(BarcodeFormat.UPC_A);

      if (ean13MayBeUPCA && canReturnUPCA) {
        // Transfer the metdata across
        Result resultUPCA = new Result(result.getText().substring(1),
                                       result.getRawBytes(),
                                       result.getResultPoints(),
                                       BarcodeFormat.UPC_A);
        resultUPCA.putAllMetadata(result.getResultMetadata());
        return resultUPCA;
      }
      return result;
    } catch (ReaderException ignored) {
      // continue
    }
  }

  throw NotFoundException.getNotFoundInstance();
}