Search in sources :

Example 26 with BitArray

use of com.google.zxing.common.BitArray in project zxing by zxing.

the class BitArrayBuilderTest method checkBinary.

private static void checkBinary(int[][] pairValues, String expected) {
    BitArray binary = buildBitArray(pairValues);
    assertEquals(expected, binary.toString());
}
Also used : BitArray(com.google.zxing.common.BitArray)

Example 27 with BitArray

use of com.google.zxing.common.BitArray in project zxing by zxing.

the class RSSExpandedImage2binaryTestCase method assertCorrectImage2binary.

private static void assertCorrectImage2binary(String fileName, String expected) throws IOException, NotFoundException {
    Path path = AbstractBlackBoxTestCase.buildTestBase("src/test/resources/blackbox/rssexpanded-1/").resolve(fileName);
    BufferedImage image = ImageIO.read(path.toFile());
    BinaryBitmap binaryMap = new BinaryBitmap(new GlobalHistogramBinarizer(new BufferedImageLuminanceSource(image)));
    int rowNumber = binaryMap.getHeight() / 2;
    BitArray row = binaryMap.getBlackRow(rowNumber, null);
    List<ExpandedPair> pairs;
    try {
        RSSExpandedReader rssExpandedReader = new RSSExpandedReader();
        pairs = rssExpandedReader.decodeRow2pairs(rowNumber, row);
    } catch (ReaderException re) {
        fail(re.toString());
        return;
    }
    BitArray binary = BitArrayBuilder.buildBitArray(pairs);
    assertEquals(expected, binary.toString());
}
Also used : Path(java.nio.file.Path) GlobalHistogramBinarizer(com.google.zxing.common.GlobalHistogramBinarizer) BufferedImageLuminanceSource(com.google.zxing.BufferedImageLuminanceSource) BitArray(com.google.zxing.common.BitArray) BinaryBitmap(com.google.zxing.BinaryBitmap) BufferedImage(java.awt.image.BufferedImage) ReaderException(com.google.zxing.ReaderException)

Example 28 with BitArray

use of com.google.zxing.common.BitArray in project zxing by zxing.

the class RSSExpandedImage2resultTestCase method assertCorrectImage2result.

private static void assertCorrectImage2result(String fileName, ExpandedProductParsedResult expected) throws IOException, NotFoundException {
    Path path = AbstractBlackBoxTestCase.buildTestBase("src/test/resources/blackbox/rssexpanded-1/").resolve(fileName);
    BufferedImage image = ImageIO.read(path.toFile());
    BinaryBitmap binaryMap = new BinaryBitmap(new GlobalHistogramBinarizer(new BufferedImageLuminanceSource(image)));
    int rowNumber = binaryMap.getHeight() / 2;
    BitArray row = binaryMap.getBlackRow(rowNumber, null);
    Result theResult;
    try {
        RSSExpandedReader rssExpandedReader = new RSSExpandedReader();
        theResult = rssExpandedReader.decodeRow(rowNumber, row, null);
    } catch (ReaderException re) {
        fail(re.toString());
        return;
    }
    assertSame(BarcodeFormat.RSS_EXPANDED, theResult.getBarcodeFormat());
    ParsedResult result = ResultParser.parseResult(theResult);
    assertEquals(expected, result);
}
Also used : Path(java.nio.file.Path) GlobalHistogramBinarizer(com.google.zxing.common.GlobalHistogramBinarizer) BufferedImageLuminanceSource(com.google.zxing.BufferedImageLuminanceSource) ParsedResult(com.google.zxing.client.result.ParsedResult) ExpandedProductParsedResult(com.google.zxing.client.result.ExpandedProductParsedResult) BitArray(com.google.zxing.common.BitArray) BinaryBitmap(com.google.zxing.BinaryBitmap) BufferedImage(java.awt.image.BufferedImage) Result(com.google.zxing.Result) ParsedResult(com.google.zxing.client.result.ParsedResult) ExpandedProductParsedResult(com.google.zxing.client.result.ExpandedProductParsedResult) ReaderException(com.google.zxing.ReaderException)

Example 29 with BitArray

use of com.google.zxing.common.BitArray in project zxing by zxing.

the class RSSExpandedImage2stringTestCase method assertCorrectImage2string.

private static void assertCorrectImage2string(String fileName, String expected) throws IOException, NotFoundException {
    Path path = AbstractBlackBoxTestCase.buildTestBase("src/test/resources/blackbox/rssexpanded-1/").resolve(fileName);
    BufferedImage image = ImageIO.read(path.toFile());
    BinaryBitmap binaryMap = new BinaryBitmap(new GlobalHistogramBinarizer(new BufferedImageLuminanceSource(image)));
    int rowNumber = binaryMap.getHeight() / 2;
    BitArray row = binaryMap.getBlackRow(rowNumber, null);
    Result result;
    try {
        RSSExpandedReader rssExpandedReader = new RSSExpandedReader();
        result = rssExpandedReader.decodeRow(rowNumber, row, null);
    } catch (ReaderException re) {
        fail(re.toString());
        return;
    }
    assertSame(BarcodeFormat.RSS_EXPANDED, result.getBarcodeFormat());
    assertEquals(expected, result.getText());
}
Also used : Path(java.nio.file.Path) GlobalHistogramBinarizer(com.google.zxing.common.GlobalHistogramBinarizer) BufferedImageLuminanceSource(com.google.zxing.BufferedImageLuminanceSource) BitArray(com.google.zxing.common.BitArray) BinaryBitmap(com.google.zxing.BinaryBitmap) BufferedImage(java.awt.image.BufferedImage) Result(com.google.zxing.Result) ReaderException(com.google.zxing.ReaderException)

Example 30 with BitArray

use of com.google.zxing.common.BitArray in project zxing by zxing.

the class Encoder method encode.

public static QRCode encode(String content, ErrorCorrectionLevel ecLevel, Map<EncodeHintType, ?> hints) throws WriterException {
    // Determine what character encoding has been specified by the caller, if any
    String encoding = DEFAULT_BYTE_MODE_ENCODING;
    boolean hasEncodingHint = hints != null && hints.containsKey(EncodeHintType.CHARACTER_SET);
    if (hasEncodingHint) {
        encoding = hints.get(EncodeHintType.CHARACTER_SET).toString();
    }
    // Pick an encoding mode appropriate for the content. Note that this will not attempt to use
    // multiple modes / segments even if that were more efficient. Twould be nice.
    Mode mode = chooseMode(content, encoding);
    // This will store the header information, like mode and
    // length, as well as "header" segments like an ECI segment.
    BitArray headerBits = new BitArray();
    // Append ECI segment if applicable
    if (mode == Mode.BYTE && (hasEncodingHint || !DEFAULT_BYTE_MODE_ENCODING.equals(encoding))) {
        CharacterSetECI eci = CharacterSetECI.getCharacterSetECIByName(encoding);
        if (eci != null) {
            appendECI(eci, headerBits);
        }
    }
    // (With ECI in place,) Write the mode marker
    appendModeInfo(mode, headerBits);
    // Collect data within the main segment, separately, to count its size if needed. Don't add it to
    // main payload yet.
    BitArray dataBits = new BitArray();
    appendBytes(content, mode, dataBits, encoding);
    Version version;
    if (hints != null && hints.containsKey(EncodeHintType.QR_VERSION)) {
        int versionNumber = Integer.parseInt(hints.get(EncodeHintType.QR_VERSION).toString());
        version = Version.getVersionForNumber(versionNumber);
        int bitsNeeded = calculateBitsNeeded(mode, headerBits, dataBits, version);
        if (!willFit(bitsNeeded, version, ecLevel)) {
            throw new WriterException("Data too big for requested version");
        }
    } else {
        version = recommendVersion(ecLevel, mode, headerBits, dataBits);
    }
    BitArray headerAndDataBits = new BitArray();
    headerAndDataBits.appendBitArray(headerBits);
    // Find "length" of main segment and write it
    int numLetters = mode == Mode.BYTE ? dataBits.getSizeInBytes() : content.length();
    appendLengthInfo(numLetters, version, mode, headerAndDataBits);
    // Put data together into the overall payload
    headerAndDataBits.appendBitArray(dataBits);
    Version.ECBlocks ecBlocks = version.getECBlocksForLevel(ecLevel);
    int numDataBytes = version.getTotalCodewords() - ecBlocks.getTotalECCodewords();
    // Terminate the bits properly.
    terminateBits(numDataBytes, headerAndDataBits);
    // Interleave data bits with error correction code.
    BitArray finalBits = interleaveWithECBytes(headerAndDataBits, version.getTotalCodewords(), numDataBytes, ecBlocks.getNumBlocks());
    QRCode qrCode = new QRCode();
    qrCode.setECLevel(ecLevel);
    qrCode.setMode(mode);
    qrCode.setVersion(version);
    //  Choose the mask pattern and set to "qrCode".
    int dimension = version.getDimensionForVersion();
    ByteMatrix matrix = new ByteMatrix(dimension, dimension);
    int maskPattern = chooseMaskPattern(finalBits, ecLevel, version, matrix);
    qrCode.setMaskPattern(maskPattern);
    // Build the matrix and set it to "qrCode".
    MatrixUtil.buildMatrix(finalBits, ecLevel, version, maskPattern, matrix);
    qrCode.setMatrix(matrix);
    return qrCode;
}
Also used : Version(com.google.zxing.qrcode.decoder.Version) Mode(com.google.zxing.qrcode.decoder.Mode) BitArray(com.google.zxing.common.BitArray) CharacterSetECI(com.google.zxing.common.CharacterSetECI) WriterException(com.google.zxing.WriterException)

Aggregations

BitArray (com.google.zxing.common.BitArray)68 Test (org.junit.Test)28 BinaryBitmap (com.google.zxing.BinaryBitmap)8 Result (com.google.zxing.Result)8 BufferedImageLuminanceSource (com.google.zxing.BufferedImageLuminanceSource)7 WriterException (com.google.zxing.WriterException)7 GlobalHistogramBinarizer (com.google.zxing.common.GlobalHistogramBinarizer)7 BufferedImage (java.awt.image.BufferedImage)7 ResultPoint (com.google.zxing.ResultPoint)6 NotFoundException (com.google.zxing.NotFoundException)5 ReaderException (com.google.zxing.ReaderException)5 BitMatrix (com.google.zxing.common.BitMatrix)4 FinderPattern (com.google.zxing.oned.rss.FinderPattern)4 ArrayList (java.util.ArrayList)4 AbstractExpandedDecoder (com.google.zxing.oned.rss.expanded.decoders.AbstractExpandedDecoder)3 Path (java.nio.file.Path)3 DecodeHintType (com.google.zxing.DecodeHintType)2 CharacterSetECI (com.google.zxing.common.CharacterSetECI)2 ReedSolomonEncoder (com.google.zxing.common.reedsolomon.ReedSolomonEncoder)2 DataCharacter (com.google.zxing.oned.rss.DataCharacter)2