Search in sources :

Example 1 with Dimension

use of com.google.zxing.Dimension in project zxing by zxing.

the class DataMatrixWriter method encode.

@Override
public BitMatrix encode(String contents, BarcodeFormat format, int width, int height, Map<EncodeHintType, ?> hints) {
    if (contents.isEmpty()) {
        throw new IllegalArgumentException("Found empty contents");
    }
    if (format != BarcodeFormat.DATA_MATRIX) {
        throw new IllegalArgumentException("Can only encode DATA_MATRIX, but got " + format);
    }
    if (width < 0 || height < 0) {
        throw new IllegalArgumentException("Requested dimensions are too small: " + width + 'x' + height);
    }
    // Try to get force shape & min / max size
    SymbolShapeHint shape = SymbolShapeHint.FORCE_NONE;
    Dimension minSize = null;
    Dimension maxSize = null;
    if (hints != null) {
        SymbolShapeHint requestedShape = (SymbolShapeHint) hints.get(EncodeHintType.DATA_MATRIX_SHAPE);
        if (requestedShape != null) {
            shape = requestedShape;
        }
        @SuppressWarnings("deprecation") Dimension requestedMinSize = (Dimension) hints.get(EncodeHintType.MIN_SIZE);
        if (requestedMinSize != null) {
            minSize = requestedMinSize;
        }
        @SuppressWarnings("deprecation") Dimension requestedMaxSize = (Dimension) hints.get(EncodeHintType.MAX_SIZE);
        if (requestedMaxSize != null) {
            maxSize = requestedMaxSize;
        }
    }
    //1. step: Data encodation
    String encoded = HighLevelEncoder.encodeHighLevel(contents, shape, minSize, maxSize);
    SymbolInfo symbolInfo = SymbolInfo.lookup(encoded.length(), shape, minSize, maxSize, true);
    //2. step: ECC generation
    String codewords = ErrorCorrection.encodeECC200(encoded, symbolInfo);
    //3. step: Module placement in Matrix
    DefaultPlacement placement = new DefaultPlacement(codewords, symbolInfo.getSymbolDataWidth(), symbolInfo.getSymbolDataHeight());
    placement.place();
    //4. step: low-level encoding
    return encodeLowLevel(placement, symbolInfo);
}
Also used : DefaultPlacement(com.google.zxing.datamatrix.encoder.DefaultPlacement) Dimension(com.google.zxing.Dimension) SymbolShapeHint(com.google.zxing.datamatrix.encoder.SymbolShapeHint) SymbolInfo(com.google.zxing.datamatrix.encoder.SymbolInfo)

Example 2 with Dimension

use of com.google.zxing.Dimension in project zxing by zxing.

the class SymbolInfoTestCase method testSymbolInfo.

@Test
public void testSymbolInfo() {
    SymbolInfo info = SymbolInfo.lookup(3);
    assertEquals(5, info.getErrorCodewords());
    assertEquals(8, info.matrixWidth);
    assertEquals(8, info.matrixHeight);
    assertEquals(10, info.getSymbolWidth());
    assertEquals(10, info.getSymbolHeight());
    info = SymbolInfo.lookup(3, SymbolShapeHint.FORCE_RECTANGLE);
    assertEquals(7, info.getErrorCodewords());
    assertEquals(16, info.matrixWidth);
    assertEquals(6, info.matrixHeight);
    assertEquals(18, info.getSymbolWidth());
    assertEquals(8, info.getSymbolHeight());
    info = SymbolInfo.lookup(9);
    assertEquals(11, info.getErrorCodewords());
    assertEquals(14, info.matrixWidth);
    assertEquals(6, info.matrixHeight);
    assertEquals(32, info.getSymbolWidth());
    assertEquals(8, info.getSymbolHeight());
    info = SymbolInfo.lookup(9, SymbolShapeHint.FORCE_SQUARE);
    assertEquals(12, info.getErrorCodewords());
    assertEquals(14, info.matrixWidth);
    assertEquals(14, info.matrixHeight);
    assertEquals(16, info.getSymbolWidth());
    assertEquals(16, info.getSymbolHeight());
    try {
        SymbolInfo.lookup(1559);
        fail("There's no rectangular symbol for more than 1558 data codewords");
    } catch (IllegalArgumentException iae) {
    //expected
    }
    try {
        SymbolInfo.lookup(50, SymbolShapeHint.FORCE_RECTANGLE);
        fail("There's no rectangular symbol for 50 data codewords");
    } catch (IllegalArgumentException iae) {
    //expected
    }
    info = SymbolInfo.lookup(35);
    assertEquals(24, info.getSymbolWidth());
    assertEquals(24, info.getSymbolHeight());
    Dimension fixedSize = new Dimension(26, 26);
    info = SymbolInfo.lookup(35, SymbolShapeHint.FORCE_NONE, fixedSize, fixedSize, false);
    assertEquals(26, info.getSymbolWidth());
    assertEquals(26, info.getSymbolHeight());
    info = SymbolInfo.lookup(45, SymbolShapeHint.FORCE_NONE, fixedSize, fixedSize, false);
    assertNull(info);
    Dimension minSize = fixedSize;
    Dimension maxSize = new Dimension(32, 32);
    info = SymbolInfo.lookup(35, SymbolShapeHint.FORCE_NONE, minSize, maxSize, false);
    assertEquals(26, info.getSymbolWidth());
    assertEquals(26, info.getSymbolHeight());
    info = SymbolInfo.lookup(40, SymbolShapeHint.FORCE_NONE, minSize, maxSize, false);
    assertEquals(26, info.getSymbolWidth());
    assertEquals(26, info.getSymbolHeight());
    info = SymbolInfo.lookup(45, SymbolShapeHint.FORCE_NONE, minSize, maxSize, false);
    assertEquals(32, info.getSymbolWidth());
    assertEquals(32, info.getSymbolHeight());
    info = SymbolInfo.lookup(63, SymbolShapeHint.FORCE_NONE, minSize, maxSize, false);
    assertNull(info);
}
Also used : Dimension(com.google.zxing.Dimension) Test(org.junit.Test)

Example 3 with Dimension

use of com.google.zxing.Dimension in project weex-example by KalicyZhou.

the class DataMatrixWriter method encode.

@Override
public BitMatrix encode(String contents, BarcodeFormat format, int width, int height, Map<EncodeHintType, ?> hints) {
    if (contents.isEmpty()) {
        throw new IllegalArgumentException("Found empty contents");
    }
    if (format != BarcodeFormat.DATA_MATRIX) {
        throw new IllegalArgumentException("Can only encode DATA_MATRIX, but got " + format);
    }
    if (width < 0 || height < 0) {
        throw new IllegalArgumentException("Requested dimensions are too small: " + width + 'x' + height);
    }
    // Try to get force shape & min / max size
    SymbolShapeHint shape = SymbolShapeHint.FORCE_NONE;
    Dimension minSize = null;
    Dimension maxSize = null;
    if (hints != null) {
        SymbolShapeHint requestedShape = (SymbolShapeHint) hints.get(EncodeHintType.DATA_MATRIX_SHAPE);
        if (requestedShape != null) {
            shape = requestedShape;
        }
        @SuppressWarnings("deprecation") Dimension requestedMinSize = (Dimension) hints.get(EncodeHintType.MIN_SIZE);
        if (requestedMinSize != null) {
            minSize = requestedMinSize;
        }
        @SuppressWarnings("deprecation") Dimension requestedMaxSize = (Dimension) hints.get(EncodeHintType.MAX_SIZE);
        if (requestedMaxSize != null) {
            maxSize = requestedMaxSize;
        }
    }
    //1. step: Data encodation
    String encoded = HighLevelEncoder.encodeHighLevel(contents, shape, minSize, maxSize);
    SymbolInfo symbolInfo = SymbolInfo.lookup(encoded.length(), shape, minSize, maxSize, true);
    //2. step: ECC generation
    String codewords = ErrorCorrection.encodeECC200(encoded, symbolInfo);
    //3. step: Module placement in Matrix
    DefaultPlacement placement = new DefaultPlacement(codewords, symbolInfo.getSymbolDataWidth(), symbolInfo.getSymbolDataHeight());
    placement.place();
    //4. step: low-level encoding
    return encodeLowLevel(placement, symbolInfo);
}
Also used : DefaultPlacement(com.google.zxing.datamatrix.encoder.DefaultPlacement) Dimension(com.google.zxing.Dimension) SymbolShapeHint(com.google.zxing.datamatrix.encoder.SymbolShapeHint) SymbolInfo(com.google.zxing.datamatrix.encoder.SymbolInfo)

Aggregations

Dimension (com.google.zxing.Dimension)3 DefaultPlacement (com.google.zxing.datamatrix.encoder.DefaultPlacement)2 SymbolInfo (com.google.zxing.datamatrix.encoder.SymbolInfo)2 SymbolShapeHint (com.google.zxing.datamatrix.encoder.SymbolShapeHint)2 Test (org.junit.Test)1