Search in sources :

Example 1 with InvalidDataBlockException

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

the class QRCodeDecoder method decode.

DecodeResult decode(QRCodeImage qrCodeImage, Point adjust) throws DecodingFailedException {
    try {
        if (numTryDecode == 0) {
            canvas.println("Decoding started");
            int[][] intImage = imageToIntArray(qrCodeImage);
            imageReader = new QRCodeImageReader();
            qrCodeSymbol = imageReader.getQRCodeSymbol(intImage);
        } else {
            canvas.println("--");
            canvas.println("Decoding restarted #" + (numTryDecode));
            qrCodeSymbol = imageReader.getQRCodeSymbolWithAdjustedGrid(adjust);
        }
    } catch (SymbolNotFoundException e) {
        throw new DecodingFailedException(e.getMessage());
    }
    canvas.println("Created QRCode symbol.");
    canvas.println("Reading symbol.");
    canvas.println("Version: " + qrCodeSymbol.getVersionReference());
    canvas.println("Mask pattern: " + qrCodeSymbol.getMaskPatternRefererAsString());
    // blocks contains all (data and RS) blocks in QR Code symbol
    int[] blocks = qrCodeSymbol.getBlocks();
    canvas.println("Correcting data errors.");
    // now blocks turn to data blocks (corrected and extracted from original blocks)
    blocks = correctDataBlocks(blocks);
    try {
        byte[] decodedByteArray = getDecodedByteArray(blocks, qrCodeSymbol.getVersion(), qrCodeSymbol.getNumErrorCollectionCode());
        return new DecodeResult(decodedByteArray, numLastCorrectionFailures);
    } catch (InvalidDataBlockException e) {
        canvas.println(e.getMessage());
        throw new DecodingFailedException(e.getMessage());
    }
}
Also used : QRCodeImageReader(jp.sourceforge.qrcode.reader.QRCodeImageReader) InvalidDataBlockException(jp.sourceforge.qrcode.exception.InvalidDataBlockException) SymbolNotFoundException(jp.sourceforge.qrcode.exception.SymbolNotFoundException) DecodingFailedException(jp.sourceforge.qrcode.exception.DecodingFailedException)

Example 2 with InvalidDataBlockException

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

the class QRCodeDecoder method getDecodedByteArray.

byte[] getDecodedByteArray(int[] blocks, int version, int numErrorCorrectionCode) throws InvalidDataBlockException {
    byte[] byteArray;
    QRCodeDataBlockReader reader = new QRCodeDataBlockReader(blocks, version, numErrorCorrectionCode);
    try {
        byteArray = reader.getDataByte();
    } catch (InvalidDataBlockException e) {
        throw e;
    }
    return byteArray;
}
Also used : InvalidDataBlockException(jp.sourceforge.qrcode.exception.InvalidDataBlockException) QRCodeDataBlockReader(jp.sourceforge.qrcode.reader.QRCodeDataBlockReader)

Example 3 with InvalidDataBlockException

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

the class QRCodeDataBlockReader method getDataByte.

public byte[] getDataByte() throws InvalidDataBlockException {
    canvas.println("Reading data blocks.");
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    try {
        do {
            int mode = getNextMode();
            //canvas.println("mode: " + mode);
            if (mode == 0) {
                if (output.size() > 0)
                    break;
                else
                    throw new InvalidDataBlockException("Empty data block");
            }
            //}
            if (mode != MODE_NUMBER && mode != MODE_ROMAN_AND_NUMBER && mode != MODE_8BIT_BYTE && mode != MODE_KANJI) {
                /*					canvas.println("Invalid mode: " + mode);
					mode = guessMode(mode);
					canvas.println("Guessed mode: " + mode); */
                throw new InvalidDataBlockException("Invalid mode: " + mode + " in (block:" + blockPointer + " bit:" + bitPointer + ")");
            }
            dataLength = getDataLength(mode);
            if (dataLength < 1)
                throw new InvalidDataBlockException("Invalid data length: " + dataLength);
            //canvas.println("length: " + dataLength);
            switch(mode) {
                case MODE_NUMBER:
                    //canvas.println("Mode: Figure");
                    output.write(getFigureString(dataLength).getBytes());
                    break;
                case MODE_ROMAN_AND_NUMBER:
                    //canvas.println("Mode: Roman&Figure");
                    output.write(getRomanAndFigureString(dataLength).getBytes());
                    break;
                case MODE_8BIT_BYTE:
                    //canvas.println("Mode: 8bit Byte");
                    output.write(get8bitByteArray(dataLength));
                    break;
                case MODE_KANJI:
                    //canvas.println("Mode: Kanji");
                    output.write(getKanjiString(dataLength).getBytes());
                    break;
            }
        //			
        //canvas.println("DataLength: " + dataLength);
        //System.out.println(dataString);
        } while (true);
    } catch (ArrayIndexOutOfBoundsException e) {
        //e.printStackTrace();
        throw new InvalidDataBlockException("Data Block Error in (block:" + blockPointer + " bit:" + bitPointer + ")");
    } catch (IOException e) {
        throw new InvalidDataBlockException(e.getMessage());
    }
    return output.toByteArray();
}
Also used : InvalidDataBlockException(jp.sourceforge.qrcode.exception.InvalidDataBlockException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Aggregations

InvalidDataBlockException (jp.sourceforge.qrcode.exception.InvalidDataBlockException)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 DecodingFailedException (jp.sourceforge.qrcode.exception.DecodingFailedException)1 SymbolNotFoundException (jp.sourceforge.qrcode.exception.SymbolNotFoundException)1 QRCodeDataBlockReader (jp.sourceforge.qrcode.reader.QRCodeDataBlockReader)1 QRCodeImageReader (jp.sourceforge.qrcode.reader.QRCodeImageReader)1