Search in sources :

Example 1 with Writer

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

the class QRCodeWriterTestCase method testQRCodeWriter.

@Test
public void testQRCodeWriter() throws WriterException {
    // The QR should be multiplied up to fit, with extra padding if necessary
    int bigEnough = 256;
    Writer writer = new QRCodeWriter();
    BitMatrix matrix = writer.encode("http://www.google.com/", BarcodeFormat.QR_CODE, bigEnough, bigEnough, null);
    assertNotNull(matrix);
    assertEquals(bigEnough, matrix.getWidth());
    assertEquals(bigEnough, matrix.getHeight());
    // The QR will not fit in this size, so the matrix should come back bigger
    int tooSmall = 20;
    matrix = writer.encode("http://www.google.com/", BarcodeFormat.QR_CODE, tooSmall, tooSmall, null);
    assertNotNull(matrix);
    assertTrue(tooSmall < matrix.getWidth());
    assertTrue(tooSmall < matrix.getHeight());
    // We should also be able to handle non-square requests by padding them
    int strangeWidth = 500;
    int strangeHeight = 100;
    matrix = writer.encode("http://www.google.com/", BarcodeFormat.QR_CODE, strangeWidth, strangeHeight, null);
    assertNotNull(matrix);
    assertEquals(strangeWidth, matrix.getWidth());
    assertEquals(strangeHeight, matrix.getHeight());
}
Also used : BitMatrix(com.google.zxing.common.BitMatrix) Writer(com.google.zxing.Writer) Test(org.junit.Test)

Example 2 with Writer

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

the class QRCodeWriterTestCase method compareToGoldenFile.

private static void compareToGoldenFile(String contents, ErrorCorrectionLevel ecLevel, int resolution, String fileName) throws WriterException, IOException {
    BufferedImage image = loadImage(fileName);
    assertNotNull(image);
    BitMatrix goldenResult = createMatrixFromImage(image);
    assertNotNull(goldenResult);
    Map<EncodeHintType, Object> hints = new EnumMap<>(EncodeHintType.class);
    hints.put(EncodeHintType.ERROR_CORRECTION, ecLevel);
    Writer writer = new QRCodeWriter();
    BitMatrix generatedResult = writer.encode(contents, BarcodeFormat.QR_CODE, resolution, resolution, hints);
    assertEquals(resolution, generatedResult.getWidth());
    assertEquals(resolution, generatedResult.getHeight());
    assertEquals(goldenResult, generatedResult);
}
Also used : EncodeHintType(com.google.zxing.EncodeHintType) BitMatrix(com.google.zxing.common.BitMatrix) EnumMap(java.util.EnumMap) BufferedImage(java.awt.image.BufferedImage) Writer(com.google.zxing.Writer)

Aggregations

Writer (com.google.zxing.Writer)2 BitMatrix (com.google.zxing.common.BitMatrix)2 EncodeHintType (com.google.zxing.EncodeHintType)1 BufferedImage (java.awt.image.BufferedImage)1 EnumMap (java.util.EnumMap)1 Test (org.junit.Test)1