use of com.google.zxing.common.DecoderResult in project android-zxing by PearceXu.
the class MaxiCodeReader method decode.
@Override
public Result decode(BinaryBitmap image, Map<DecodeHintType, ?> hints) throws NotFoundException, ChecksumException, FormatException {
DecoderResult decoderResult;
if (hints != null && hints.containsKey(DecodeHintType.PURE_BARCODE)) {
BitMatrix bits = extractPureBits(image.getBlackMatrix());
decoderResult = decoder.decode(bits, hints);
} else {
throw NotFoundException.getNotFoundInstance();
}
Result result = new Result(decoderResult.getText(), decoderResult.getRawBytes(), NO_POINTS, BarcodeFormat.MAXICODE);
String ecLevel = decoderResult.getECLevel();
if (ecLevel != null) {
result.putMetadata(ResultMetadataType.ERROR_CORRECTION_LEVEL, ecLevel);
}
return result;
}
use of com.google.zxing.common.DecoderResult in project android-zxing by PearceXu.
the class DecodedBitStreamParser method decode.
static DecoderResult decode(byte[] bytes, int mode) {
StringBuilder result = new StringBuilder(144);
switch(mode) {
case 2:
case 3:
String postcode;
if (mode == 2) {
int pc = getPostCode2(bytes);
NumberFormat df = new DecimalFormat("0000000000".substring(0, getPostCode2Length(bytes)));
postcode = df.format(pc);
} else {
postcode = getPostCode3(bytes);
}
NumberFormat threeDigits = new DecimalFormat("000");
String country = threeDigits.format(getCountry(bytes));
String service = threeDigits.format(getServiceClass(bytes));
result.append(getMessage(bytes, 10, 84));
if (result.toString().startsWith("[)>" + RS + "01" + GS)) {
result.insert(9, postcode + GS + country + GS + service + GS);
} else {
result.insert(0, postcode + GS + country + GS + service + GS);
}
break;
case 4:
result.append(getMessage(bytes, 1, 93));
break;
case 5:
result.append(getMessage(bytes, 1, 77));
break;
}
return new DecoderResult(bytes, result.toString(), null, String.valueOf(mode));
}
use of com.google.zxing.common.DecoderResult in project android-zxing by PearceXu.
the class PDF417Reader method decode.
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()]);
}
use of com.google.zxing.common.DecoderResult in project android-zxing by PearceXu.
the class DecodedBitStreamParser method decode.
static DecoderResult decode(int[] codewords, String ecLevel) throws FormatException {
StringBuilder result = new StringBuilder(codewords.length * 2);
Charset encoding = StandardCharsets.ISO_8859_1;
// Get compaction mode
int codeIndex = 1;
int code = codewords[codeIndex++];
PDF417ResultMetadata resultMetadata = new PDF417ResultMetadata();
while (codeIndex < codewords[0]) {
switch(code) {
case TEXT_COMPACTION_MODE_LATCH:
codeIndex = textCompaction(codewords, codeIndex, result);
break;
case BYTE_COMPACTION_MODE_LATCH:
case BYTE_COMPACTION_MODE_LATCH_6:
codeIndex = byteCompaction(code, codewords, encoding, codeIndex, result);
break;
case MODE_SHIFT_TO_BYTE_COMPACTION_MODE:
result.append((char) codewords[codeIndex++]);
break;
case NUMERIC_COMPACTION_MODE_LATCH:
codeIndex = numericCompaction(codewords, codeIndex, result);
break;
case ECI_CHARSET:
CharacterSetECI charsetECI = CharacterSetECI.getCharacterSetECIByValue(codewords[codeIndex++]);
encoding = Charset.forName(charsetECI.name());
break;
case ECI_GENERAL_PURPOSE:
// Can't do anything with generic ECI; skip its 2 characters
codeIndex += 2;
break;
case ECI_USER_DEFINED:
// Can't do anything with user ECI; skip its 1 character
codeIndex++;
break;
case BEGIN_MACRO_PDF417_CONTROL_BLOCK:
codeIndex = decodeMacroBlock(codewords, codeIndex, resultMetadata);
break;
case BEGIN_MACRO_PDF417_OPTIONAL_FIELD:
case MACRO_PDF417_TERMINATOR:
// Should not see these outside a macro block
throw FormatException.getFormatInstance();
default:
// Default to text compaction. During testing numerous barcodes
// appeared to be missing the starting mode. In these cases defaulting
// to text compaction seems to work.
codeIndex--;
codeIndex = textCompaction(codewords, codeIndex, result);
break;
}
if (codeIndex < codewords.length) {
code = codewords[codeIndex++];
} else {
throw FormatException.getFormatInstance();
}
}
if (result.length() == 0) {
throw FormatException.getFormatInstance();
}
DecoderResult decoderResult = new DecoderResult(null, result.toString(), null, ecLevel);
decoderResult.setOther(resultMetadata);
return decoderResult;
}
use of com.google.zxing.common.DecoderResult in project ofbiz-framework by apache.
the class QRCodeServices method generateQRCodeImage.
/**
* Streams QR Code to the result.
*/
public static Map<String, Object> generateQRCodeImage(DispatchContext ctx, Map<String, Object> context) {
Locale locale = (Locale) context.get("locale");
String message = (String) context.get("message");
Integer width = (Integer) context.get("width");
Integer height = (Integer) context.get("height");
String format = (String) context.get("format");
String encoding = (String) context.get("encoding");
Boolean verifyOutput = (Boolean) context.get("verifyOutput");
String logoImage = (String) context.get("logoImage");
Integer logoImageMaxWidth = (Integer) context.get("logoImageMaxWidth");
Integer logoImageMaxHeight = (Integer) context.get("logoImageMaxHeight");
if (UtilValidate.isEmpty(message)) {
return ServiceUtil.returnError(UtilProperties.getMessage("QRCodeUiLabels", "ParameterCannotEmpty", new Object[] { "message" }, locale));
}
if (width == null) {
width = Integer.parseInt(QRCODE_DEFAULT_WIDTH);
}
if (width.intValue() < MIN_SIZE || width.intValue() > MAX_SIZE) {
return ServiceUtil.returnError(UtilProperties.getMessage("QRCodeUiLabels", "SizeOutOfBorderError", new Object[] { "width", String.valueOf(width), String.valueOf(MIN_SIZE), String.valueOf(MAX_SIZE) }, locale));
}
if (height == null) {
height = Integer.parseInt(QRCODE_DEFAULT_HEIGHT);
}
if (height.intValue() < MIN_SIZE || height.intValue() > MAX_SIZE) {
return ServiceUtil.returnError(UtilProperties.getMessage("QRCodeUiLabels", "SizeOutOfBorderError", new Object[] { "height", String.valueOf(height), String.valueOf(MIN_SIZE), String.valueOf(MAX_SIZE) }, locale));
}
if (UtilValidate.isEmpty(format)) {
format = QRCODE_DEFAULT_FORMAT;
}
if (!FORMATS_SUPPORTED.contains(format)) {
return ServiceUtil.returnError(UtilProperties.getMessage("QRCodeUiLabels", "ErrorFormatNotSupported", new Object[] { format }, locale));
}
Map<EncodeHintType, Object> encodeHints = null;
if (UtilValidate.isNotEmpty(encoding)) {
encodeHints = new EnumMap<>(EncodeHintType.class);
encodeHints.put(EncodeHintType.CHARACTER_SET, encoding);
}
try {
BitMatrix bitMatrix = new MultiFormatWriter().encode(message, BarcodeFormat.QR_CODE, width, height, encodeHints);
BufferedImage bufferedImage = toBufferedImage(bitMatrix, format);
BufferedImage logoBufferedImage = null;
if (UtilValidate.isNotEmpty(logoImage)) {
Map<String, Object> logoImageResult;
try {
logoImageResult = ImageTransform.getBufferedImage(FileUtil.getFile(logoImage).getAbsolutePath(), locale);
logoBufferedImage = (BufferedImage) logoImageResult.get("bufferedImage");
} catch (IllegalArgumentException | IOException e) {
Debug.logError(e, module);
}
}
if (UtilValidate.isEmpty(logoBufferedImage)) {
logoBufferedImage = defaultLogoImage;
}
BufferedImage newBufferedImage = null;
if (UtilValidate.isNotEmpty(logoBufferedImage)) {
if (UtilValidate.isNotEmpty(logoImageMaxWidth) && UtilValidate.isNotEmpty(logoImageMaxHeight) && (logoBufferedImage.getWidth() > logoImageMaxWidth.intValue() || logoBufferedImage.getHeight() > logoImageMaxHeight.intValue())) {
Map<String, String> typeMap = new HashMap<>();
typeMap.put("width", logoImageMaxWidth.toString());
typeMap.put("height", logoImageMaxHeight.toString());
Map<String, Map<String, String>> dimensionMap = new HashMap<>();
dimensionMap.put("QRCode", typeMap);
Map<String, Object> logoImageResult = ImageTransform.scaleImage(logoBufferedImage, (double) logoBufferedImage.getWidth(), (double) logoBufferedImage.getHeight(), dimensionMap, "QRCode", locale);
logoBufferedImage = (BufferedImage) logoImageResult.get("bufferedImage");
}
BitMatrix newBitMatrix = bitMatrix.clone();
newBufferedImage = toBufferedImage(newBitMatrix, format);
Graphics2D graphics = newBufferedImage.createGraphics();
graphics.drawImage(logoBufferedImage, new AffineTransformOp(AffineTransform.getTranslateInstance(1, 1), null), (newBufferedImage.getWidth() - logoBufferedImage.getWidth()) / 2, (newBufferedImage.getHeight() - logoBufferedImage.getHeight()) / 2);
graphics.dispose();
}
if (UtilValidate.isNotEmpty(verifyOutput) && verifyOutput.booleanValue()) {
Decoder decoder = new Decoder();
Map<DecodeHintType, Object> decodeHints = new EnumMap<>(DecodeHintType.class);
decodeHints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
if (UtilValidate.isNotEmpty(encoding)) {
decodeHints.put(DecodeHintType.CHARACTER_SET, encoding);
}
DetectorResult detectorResult = null;
if (UtilValidate.isNotEmpty(newBufferedImage)) {
BitMatrix newBitMatrix = createMatrixFromImage(newBufferedImage);
DecoderResult result = null;
try {
detectorResult = new Detector(newBitMatrix).detect(decodeHints);
result = decoder.decode(detectorResult.getBits(), decodeHints);
} catch (ChecksumException | FormatException | NotFoundException e) {
Debug.logError(e, module);
}
if (UtilValidate.isNotEmpty(result) && !result.getText().equals(message)) {
detectorResult = new Detector(bitMatrix).detect(decodeHints);
result = decoder.decode(detectorResult.getBits(), decodeHints);
if (!result.getText().equals(message)) {
return ServiceUtil.returnError(UtilProperties.getMessage("QRCodeUiLabels", "GeneratedTextNotMatchOriginal", new Object[] { result.getText(), message }, locale));
}
} else {
bufferedImage = newBufferedImage;
}
} else {
detectorResult = new Detector(bitMatrix).detect(decodeHints);
DecoderResult result = decoder.decode(detectorResult.getBits(), decodeHints);
if (!result.getText().equals(message)) {
return ServiceUtil.returnError(UtilProperties.getMessage("QRCodeUiLabels", "GeneratedTextNotMatchOriginal", new Object[] { result.getText(), message }, locale));
}
}
} else if (UtilValidate.isNotEmpty(newBufferedImage)) {
bufferedImage = newBufferedImage;
}
Map<String, Object> result = ServiceUtil.returnSuccess();
result.put("bufferedImage", bufferedImage);
return result;
} catch (WriterException e) {
return ServiceUtil.returnError(UtilProperties.getMessage("QRCodeUiLabels", "ErrorGenerateQRCode", new Object[] { e.toString() }, locale));
} catch (ChecksumException e) {
return ServiceUtil.returnError(UtilProperties.getMessage("QRCodeUiLabels", "ErrorVerifyQRCode", new Object[] { e.toString() }, locale));
} catch (FormatException e) {
return ServiceUtil.returnError(UtilProperties.getMessage("QRCodeUiLabels", "ErrorVerifyQRCode", new Object[] { e.toString() }, locale));
} catch (NotFoundException e) {
return ServiceUtil.returnError(UtilProperties.getMessage("QRCodeUiLabels", "ErrorVerifyQRCode", new Object[] { e.toString() }, locale));
}
}
Aggregations