use of com.google.zxing.NotFoundException in project wechat by dllwh.
the class QrcodeUtilHelper method decode.
/**
* @方法描述: 解析二维码
* @创建者: 独泪了无痕
* @创建时间: 2016年2月26日 下午6:03:54
* @param imgPath
* 二维码路径
* @return 返回解析后的内容
*/
public static String decode(String imgPath) {
if (StringUtils.isBlank(imgPath)) {
System.out.println("尚未找到想要解析的图片");
return "";
}
BufferedImage image;
Result result;
try {
image = ImageIO.read(new File(imgPath));
LuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();
// 解码设置编码方式为:GBK
hints.put(DecodeHintType.CHARACTER_SET, "GBK");
result = new MultiFormatReader().decode(bitmap, hints);
// 对图像进行解码
String resultStr = result.getText();
return resultStr;
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (NotFoundException nfe) {
nfe.printStackTrace();
}
return "";
}
use of com.google.zxing.NotFoundException in project zxing by zxing.
the class AztecReader method decode.
@Override
public Result decode(BinaryBitmap image, Map<DecodeHintType, ?> hints) throws NotFoundException, FormatException {
NotFoundException notFoundException = null;
FormatException formatException = null;
Detector detector = new Detector(image.getBlackMatrix());
ResultPoint[] points = null;
DecoderResult decoderResult = null;
try {
AztecDetectorResult detectorResult = detector.detect(false);
points = detectorResult.getPoints();
decoderResult = new Decoder().decode(detectorResult);
} catch (NotFoundException e) {
notFoundException = e;
} catch (FormatException e) {
formatException = e;
}
if (decoderResult == null) {
try {
AztecDetectorResult detectorResult = detector.detect(true);
points = detectorResult.getPoints();
decoderResult = new Decoder().decode(detectorResult);
} catch (NotFoundException | FormatException e) {
if (notFoundException != null) {
throw notFoundException;
}
if (formatException != null) {
throw formatException;
}
throw e;
}
}
if (hints != null) {
ResultPointCallback rpcb = (ResultPointCallback) hints.get(DecodeHintType.NEED_RESULT_POINT_CALLBACK);
if (rpcb != null) {
for (ResultPoint point : points) {
rpcb.foundPossibleResultPoint(point);
}
}
}
Result result = new Result(decoderResult.getText(), decoderResult.getRawBytes(), decoderResult.getNumBits(), points, BarcodeFormat.AZTEC, System.currentTimeMillis());
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;
}
use of com.google.zxing.NotFoundException in project zxing by zxing.
the class ByQuadrantReader method decode.
@Override
public Result decode(BinaryBitmap image, Map<DecodeHintType, ?> hints) throws NotFoundException, ChecksumException, FormatException {
int width = image.getWidth();
int height = image.getHeight();
int halfWidth = width / 2;
int halfHeight = height / 2;
try {
// No need to call makeAbsolute as results will be relative to original top left here
return delegate.decode(image.crop(0, 0, halfWidth, halfHeight), hints);
} catch (NotFoundException re) {
// continue
}
try {
Result result = delegate.decode(image.crop(halfWidth, 0, halfWidth, halfHeight), hints);
makeAbsolute(result.getResultPoints(), halfWidth, 0);
return result;
} catch (NotFoundException re) {
// continue
}
try {
Result result = delegate.decode(image.crop(0, halfHeight, halfWidth, halfHeight), hints);
makeAbsolute(result.getResultPoints(), 0, halfHeight);
return result;
} catch (NotFoundException re) {
// continue
}
try {
Result result = delegate.decode(image.crop(halfWidth, halfHeight, halfWidth, halfHeight), hints);
makeAbsolute(result.getResultPoints(), halfWidth, halfHeight);
return result;
} catch (NotFoundException re) {
// continue
}
int quarterWidth = halfWidth / 2;
int quarterHeight = halfHeight / 2;
BinaryBitmap center = image.crop(quarterWidth, quarterHeight, halfWidth, halfHeight);
Result result = delegate.decode(center, hints);
makeAbsolute(result.getResultPoints(), quarterWidth, quarterHeight);
return result;
}
use of com.google.zxing.NotFoundException in project zxing by zxing.
the class DecodeWorker method dumpBlackPoint.
/**
* Writes out a single PNG which is three times the width of the input image, containing from left
* to right: the original image, the row sampling monochrome version, and the 2D sampling
* monochrome version.
*/
private static void dumpBlackPoint(URI uri, BufferedImage image, BinaryBitmap bitmap) throws IOException {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int stride = width * 3;
int[] pixels = new int[stride * height];
// The original image
int[] argb = new int[width];
for (int y = 0; y < height; y++) {
image.getRGB(0, y, width, 1, argb, 0, width);
System.arraycopy(argb, 0, pixels, y * stride, width);
}
// Row sampling
BitArray row = new BitArray(width);
for (int y = 0; y < height; y++) {
try {
row = bitmap.getBlackRow(y, row);
} catch (NotFoundException nfe) {
// If fetching the row failed, draw a red line and keep going.
int offset = y * stride + width;
Arrays.fill(pixels, offset, offset + width, RED);
continue;
}
int offset = y * stride + width;
for (int x = 0; x < width; x++) {
pixels[offset + x] = row.get(x) ? BLACK : WHITE;
}
}
// 2D sampling
try {
for (int y = 0; y < height; y++) {
BitMatrix matrix = bitmap.getBlackMatrix();
int offset = y * stride + width * 2;
for (int x = 0; x < width; x++) {
pixels[offset + x] = matrix.get(x, y) ? BLACK : WHITE;
}
}
} catch (NotFoundException ignored) {
// continue
}
writeResultImage(stride, height, pixels, uri, ".mono.png");
}
use of com.google.zxing.NotFoundException in project zxing by zxing.
the class DecodeWorker method decode.
private Result[] decode(URI uri, Map<DecodeHintType, ?> hints) throws IOException {
BufferedImage image = ImageReader.readImage(uri);
LuminanceSource source;
if (config.crop == null) {
source = new BufferedImageLuminanceSource(image);
} else {
List<Integer> crop = config.crop;
source = new BufferedImageLuminanceSource(image, crop.get(0), crop.get(1), crop.get(2), crop.get(3));
}
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
if (config.dumpBlackPoint) {
dumpBlackPoint(uri, image, bitmap);
}
MultiFormatReader multiFormatReader = new MultiFormatReader();
Result[] results;
try {
if (config.multi) {
MultipleBarcodeReader reader = new GenericMultipleBarcodeReader(multiFormatReader);
results = reader.decodeMultiple(bitmap, hints);
} else {
results = new Result[] { multiFormatReader.decode(bitmap, hints) };
}
} catch (NotFoundException ignored) {
System.out.println(uri + ": No barcode found");
return null;
}
if (config.brief) {
System.out.println(uri + ": Success");
} else {
StringWriter output = new StringWriter();
for (Result result : results) {
ParsedResult parsedResult = ResultParser.parseResult(result);
output.write(uri + " (format: " + result.getBarcodeFormat() + ", type: " + parsedResult.getType() + "):\n" + "Raw result:\n" + result.getText() + "\n" + "Parsed result:\n" + parsedResult.getDisplayResult() + "\n");
output.write("Found " + result.getResultPoints().length + " result points.\n");
for (int pointIndex = 0; pointIndex < result.getResultPoints().length; pointIndex++) {
ResultPoint rp = result.getResultPoints()[pointIndex];
output.write(" Point " + pointIndex + ": (" + rp.getX() + ',' + rp.getY() + ')');
if (pointIndex != result.getResultPoints().length - 1) {
output.write('\n');
}
}
output.write('\n');
}
System.out.println(output);
}
return results;
}
Aggregations