Search in sources :

Example 6 with DecodingFailedException

use of jp.sourceforge.qrcode.exception.DecodingFailedException in project qrcode by yanbe.

the class QRCodeDecoder method decode.

public byte[] decode(QRCodeImage qrCodeImage) throws DecodingFailedException {
    Point[] adjusts = getAdjustPoints();
    Vector results = new Vector();
    numTryDecode = 0;
    while (numTryDecode < adjusts.length) {
        try {
            DecodeResult result = decode(qrCodeImage, adjusts[numTryDecode]);
            if (result.isCorrectionSucceeded()) {
                return result.getDecodedBytes();
            } else {
                results.addElement(result);
                canvas.println("Decoding succeeded but could not correct");
                canvas.println("all errors. Retrying..");
            }
        } catch (DecodingFailedException dfe) {
            if (dfe.getMessage().indexOf("Finder Pattern") >= 0)
                throw dfe;
        } finally {
            numTryDecode += 1;
        }
    }
    if (results.size() == 0)
        throw new DecodingFailedException("Give up decoding");
    int minErrorIndex = -1;
    int minError = Integer.MAX_VALUE;
    for (int i = 0; i < results.size(); i++) {
        DecodeResult result = (DecodeResult) results.elementAt(i);
        if (result.getNumCorrectuionFailures() < minError) {
            minError = result.getNumCorrectuionFailures();
            minErrorIndex = i;
        }
    }
    canvas.println("All trials need for correct error");
    canvas.println("Reporting #" + (minErrorIndex) + " that,");
    canvas.println("corrected minimum errors (" + minError + ")");
    canvas.println("Decoding finished.");
    return ((DecodeResult) results.elementAt(minErrorIndex)).getDecodedBytes();
}
Also used : Point(jp.sourceforge.qrcode.geom.Point) Vector(java.util.Vector) Point(jp.sourceforge.qrcode.geom.Point) DecodingFailedException(jp.sourceforge.qrcode.exception.DecodingFailedException)

Aggregations

DecodingFailedException (jp.sourceforge.qrcode.exception.DecodingFailedException)6 IOException (java.io.IOException)3 BufferedImage (java.awt.image.BufferedImage)2 File (java.io.File)2 URL (java.net.URL)2 QRCodeDecoder (jp.sourceforge.qrcode.QRCodeDecoder)2 QRCodeImage (jp.sourceforge.qrcode.data.QRCodeImage)2 Point (jp.sourceforge.qrcode.geom.Point)2 Image (java.awt.Image)1 Vector (java.util.Vector)1 Buffer (javax.media.Buffer)1 FrameGrabbingControl (javax.media.control.FrameGrabbingControl)1 BufferToImage (javax.media.util.BufferToImage)1 Image (javax.microedition.lcdui.Image)1 InvalidDataBlockException (jp.sourceforge.qrcode.exception.InvalidDataBlockException)1 InvalidVersionInfoException (jp.sourceforge.qrcode.exception.InvalidVersionInfoException)1 SymbolNotFoundException (jp.sourceforge.qrcode.exception.SymbolNotFoundException)1 QRCodeImageReader (jp.sourceforge.qrcode.reader.QRCodeImageReader)1 DebugCanvas (jp.sourceforge.qrcode.util.DebugCanvas)1