Search in sources :

Example 26 with NotFoundException

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 "";
}
Also used : MultiFormatReader(com.google.zxing.MultiFormatReader) DecodeHintType(com.google.zxing.DecodeHintType) Hashtable(java.util.Hashtable) NotFoundException(com.google.zxing.NotFoundException) IOException(java.io.IOException) HybridBinarizer(com.google.zxing.common.HybridBinarizer) BufferedImage(java.awt.image.BufferedImage) Result(com.google.zxing.Result) LuminanceSource(com.google.zxing.LuminanceSource) BufferedImageLuminanceSource(com.google.zxing.client.j2se.BufferedImageLuminanceSource) BufferedImageLuminanceSource(com.google.zxing.client.j2se.BufferedImageLuminanceSource) BinaryBitmap(com.google.zxing.BinaryBitmap) File(java.io.File)

Example 27 with NotFoundException

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;
}
Also used : ResultPointCallback(com.google.zxing.ResultPointCallback) ResultPoint(com.google.zxing.ResultPoint) NotFoundException(com.google.zxing.NotFoundException) Decoder(com.google.zxing.aztec.decoder.Decoder) FormatException(com.google.zxing.FormatException) Result(com.google.zxing.Result) DecoderResult(com.google.zxing.common.DecoderResult) Detector(com.google.zxing.aztec.detector.Detector) DecoderResult(com.google.zxing.common.DecoderResult)

Example 28 with NotFoundException

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;
}
Also used : NotFoundException(com.google.zxing.NotFoundException) BinaryBitmap(com.google.zxing.BinaryBitmap) ResultPoint(com.google.zxing.ResultPoint) Result(com.google.zxing.Result)

Example 29 with NotFoundException

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");
}
Also used : NotFoundException(com.google.zxing.NotFoundException) BitArray(com.google.zxing.common.BitArray) BitMatrix(com.google.zxing.common.BitMatrix) ResultPoint(com.google.zxing.ResultPoint)

Example 30 with NotFoundException

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;
}
Also used : MultiFormatReader(com.google.zxing.MultiFormatReader) GenericMultipleBarcodeReader(com.google.zxing.multi.GenericMultipleBarcodeReader) ResultPoint(com.google.zxing.ResultPoint) GenericMultipleBarcodeReader(com.google.zxing.multi.GenericMultipleBarcodeReader) MultipleBarcodeReader(com.google.zxing.multi.MultipleBarcodeReader) NotFoundException(com.google.zxing.NotFoundException) HybridBinarizer(com.google.zxing.common.HybridBinarizer) BufferedImage(java.awt.image.BufferedImage) ResultPoint(com.google.zxing.ResultPoint) Result(com.google.zxing.Result) ParsedResult(com.google.zxing.client.result.ParsedResult) StringWriter(java.io.StringWriter) LuminanceSource(com.google.zxing.LuminanceSource) ParsedResult(com.google.zxing.client.result.ParsedResult) BinaryBitmap(com.google.zxing.BinaryBitmap)

Aggregations

NotFoundException (com.google.zxing.NotFoundException)56 ResultPoint (com.google.zxing.ResultPoint)38 Result (com.google.zxing.Result)22 BinaryBitmap (com.google.zxing.BinaryBitmap)20 DecodeHintType (com.google.zxing.DecodeHintType)12 FormatException (com.google.zxing.FormatException)12 HybridBinarizer (com.google.zxing.common.HybridBinarizer)12 FinderPattern (com.google.zxing.oned.rss.FinderPattern)9 ChecksumException (com.google.zxing.ChecksumException)8 ResultPointCallback (com.google.zxing.ResultPointCallback)8 BitArray (com.google.zxing.common.BitArray)7 BitMatrix (com.google.zxing.common.BitMatrix)7 ReaderException (com.google.zxing.ReaderException)6 DecoderResult (com.google.zxing.common.DecoderResult)6 Hashtable (java.util.Hashtable)6 Decoder (com.google.zxing.aztec.decoder.Decoder)5 DetectorResult (com.google.zxing.common.DetectorResult)5 QRCodeReader (com.google.zxing.qrcode.QRCodeReader)5 EnumMap (java.util.EnumMap)5 BitmapFactory (android.graphics.BitmapFactory)4