下面列出了com.google.zxing.BarcodeFormat# UPC_A 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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);
}
@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);
}
/**
* 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(fakeR.getId("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);
}
}
}
}
private static Result a(Result result)
{
String s = result.getText();
if (s.charAt(0) == '0')
{
return new Result(s.substring(1), null, result.getResultPoints(), BarcodeFormat.UPC_A);
} else
{
throw FormatException.getFormatInstance();
}
}
private static Result maybeReturnResult(Result result) throws FormatException {
String text = result.getText();
if (text.charAt(0) == '0') {
return new Result(text.substring(1), null, result.getResultPoints(), BarcodeFormat.UPC_A);
} else {
throw FormatException.getFormatInstance();
}
}
/**
* 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);
}
}
}
}
}
/**
* Method is used to convert parsed id to BarcodeFormat to create an image of the qr code
* @param id as int
* @return format as BarcodeFormat
*/
public BarcodeFormat idToBarcodeFormat(int id){
BarcodeFormat format;
switch(id){
case 1:
format = BarcodeFormat.CODABAR;
break;
case 2:
format= BarcodeFormat.CODE_128;
break;
case 3:
format = BarcodeFormat.CODE_39;
break;
case 4:
format = BarcodeFormat.EAN_13;
break;
case 5:
format = BarcodeFormat.EAN_8;
break;
case 6:
format = BarcodeFormat.ITF;
break;
case 7:
format = BarcodeFormat.PDF_417;
break;
case 8:
format = BarcodeFormat.UPC_A;
break;
case 9:
format = BarcodeFormat.QR_CODE;
break;
case 10:
format = BarcodeFormat.AZTEC;
break;
default:
format = BarcodeFormat.CODABAR;
break;
}
return format;
}
/**
* Method is used to convert parsed String to BarcodeFormat to create an image of the qr code
* @param stringFormat as String
* @return format as BarcodeFormat
*/
public BarcodeFormat StringToBarcodeFormat(String stringFormat){
BarcodeFormat format;
switch(stringFormat){
case "CODBAR":
format = BarcodeFormat.CODABAR;
break;
case "CODE_128":
format= BarcodeFormat.CODE_128;
break;
case "CODE_39":
format = BarcodeFormat.CODE_39;
break;
case "EAN_13":
format = BarcodeFormat.EAN_13;
break;
case "EAN_8":
format = BarcodeFormat.EAN_8;
break;
case "ITF":
format = BarcodeFormat.ITF;
break;
case "PDF_417":
format = BarcodeFormat.PDF_417;
break;
case "UPC_A":
format = BarcodeFormat.UPC_A;
break;
case "QR_CODE":
format = BarcodeFormat.QR_CODE;
break;
case "AZTEC":
format = BarcodeFormat.AZTEC;
break;
default:
format = BarcodeFormat.CODABAR;
break;
}
return format;
}
/**
* 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);
}
}
}
}
}
/**
* 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);
}
}
}
}
private static Result maybeReturnResult(Result result) throws FormatException {
String text = result.getText();
if (text.charAt(0) == '0') {
Result retval = new Result(text.substring(1), null, result.getResultPoints(), BarcodeFormat.UPC_A);
retval.putAllMetadata(result.getResultMetadata());
return retval;
} else {
throw FormatException.getFormatInstance();
}
}
private static Result maybeReturnResult(Result result) throws FormatException {
String text = result.getText();
if (text.charAt(0) == '0') {
return new Result(text.substring(1), null, result.getResultPoints(), BarcodeFormat.UPC_A);
} else {
throw FormatException.getFormatInstance();
}
}
/**
* <p>Like {@link #decodeRow(int, BitArray, java.util.Map)}, but
* allows caller to inform method about where the UPC/EAN start pattern is
* found. This allows this to be computed once and reused across many implementations.</p>
*/
public Result decodeRow(int rowNumber,
BitArray row,
int[] startGuardRange,
Map<DecodeHintType,?> hints)
throws NotFoundException, ChecksumException, FormatException {
ResultPointCallback resultPointCallback = hints == null ? null :
(ResultPointCallback) hints.get(DecodeHintType.NEED_RESULT_POINT_CALLBACK);
if (resultPointCallback != null) {
resultPointCallback.foundPossibleResultPoint(new ResultPoint(
(startGuardRange[0] + startGuardRange[1]) / 2.0f, rowNumber
));
}
StringBuilder result = decodeRowStringBuffer;
result.setLength(0);
int endStart = decodeMiddle(row, startGuardRange, result);
if (resultPointCallback != null) {
resultPointCallback.foundPossibleResultPoint(new ResultPoint(
endStart, rowNumber
));
}
int[] endRange = decodeEnd(row, endStart);
if (resultPointCallback != null) {
resultPointCallback.foundPossibleResultPoint(new ResultPoint(
(endRange[0] + endRange[1]) / 2.0f, rowNumber
));
}
// Make sure there is a quiet zone at least as big as the end pattern after the barcode. The
// spec might want more whitespace, but in practice this is the maximum we can count on.
int end = endRange[1];
int quietEnd = end + (end - endRange[0]);
if (quietEnd >= row.getSize() || !row.isRange(end, quietEnd, false)) {
throw NotFoundException.getNotFoundInstance();
}
String resultString = result.toString();
if (!checkChecksum(resultString)) {
throw ChecksumException.getChecksumInstance();
}
float left = (float) (startGuardRange[1] + startGuardRange[0]) / 2.0f;
float right = (float) (endRange[1] + endRange[0]) / 2.0f;
BarcodeFormat format = getBarcodeFormat();
Result decodeResult = new Result(resultString,
null, // no natural byte representation for these barcodes
new ResultPoint[]{
new ResultPoint(left, (float) rowNumber),
new ResultPoint(right, (float) rowNumber)},
format);
try {
Result extensionResult = extensionReader.decodeRow(rowNumber, row, endRange[1]);
decodeResult.putMetadata(ResultMetadataType.UPC_EAN_EXTENSION, extensionResult.getText());
decodeResult.putAllMetadata(extensionResult.getResultMetadata());
decodeResult.addResultPoints(extensionResult.getResultPoints());
} catch (ReaderException re) {
// continue
}
if (format == BarcodeFormat.EAN_13 || format == BarcodeFormat.UPC_A) {
String countryID = eanManSupport.lookupCountryIdentifier(resultString);
if (countryID != null) {
decodeResult.putMetadata(ResultMetadataType.POSSIBLE_COUNTRY, countryID);
}
}
return decodeResult;
}
@Override
BarcodeFormat getBarcodeFormat() {
return BarcodeFormat.UPC_A;
}
@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();
}
@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();
}
@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();
}
@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();
}
@Override
BarcodeFormat getBarcodeFormat() {
return BarcodeFormat.UPC_A;
}
@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();
}