com.google.zxing.Writer Java Examples

The following examples show how to use com.google.zxing.Writer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: QRCodeWriter.java    From oath with MIT License 6 votes vote down vote up
private void doWrite(OutputStream os, Path path) throws IOException {
    try {
        Writer writer = new MultiFormatWriter();
        Map<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
        hints.put(EncodeHintType.CHARACTER_SET, StandardCharsets.UTF_8.name());
        hints.put(EncodeHintType.MARGIN, Integer.valueOf(margin));
        hints.put(EncodeHintType.ERROR_CORRECTION, com.google.zxing.qrcode.decoder.ErrorCorrectionLevel.forBits(errorCorrectionLevel.getBits()));
        BitMatrix matrix = writer.encode(uri.toUriString(), BarcodeFormat.QR_CODE, width, height, hints);
        if (os != null) {
            MatrixToImageWriter.writeToStream(matrix, imageFormatName, os);
        }
        else {
            MatrixToImageWriter.writeToPath(matrix, imageFormatName, path);
        }
    } catch (WriterException e) {
        throw new IOException(e);
    }
}
 
Example #2
Source File: ZxingPngQrGeneratorTest.java    From java-totp with MIT License 5 votes vote down vote up
@Test
public void testExceptionIsWrapped() throws WriterException {
    Throwable exception = new RuntimeException();
    Writer writer = mock(Writer.class);
    when(writer.encode(anyString(), any(), anyInt(), anyInt())).thenThrow(exception);

    ZxingPngQrGenerator generator = new ZxingPngQrGenerator(writer);

    QrGenerationException e = assertThrows(QrGenerationException.class, () -> {
        generator.generate(getData());
    });

    assertEquals("Failed to generate QR code. See nested exception.", e.getMessage());
    assertEquals(exception, e.getCause());
}
 
Example #3
Source File: ZxingPngQrGenerator.java    From java-totp with MIT License 4 votes vote down vote up
public ZxingPngQrGenerator(Writer writer) {
    this.writer = writer;
}