Search in sources :

Example 11 with ErrorCorrectionLevel

use of com.google.zxing.qrcode.decoder.ErrorCorrectionLevel in project i2p.i2p by i2p.

the class QRCodeWriter method encode.

@Override
public BitMatrix encode(String contents, BarcodeFormat format, int width, int height, Map<EncodeHintType, ?> hints) throws WriterException {
    if (contents.isEmpty()) {
        throw new IllegalArgumentException("Found empty contents");
    }
    if (format != BarcodeFormat.QR_CODE) {
        throw new IllegalArgumentException("Can only encode QR_CODE, but got " + format);
    }
    if (width < 0 || height < 0) {
        throw new IllegalArgumentException("Requested dimensions are too small: " + width + 'x' + height);
    }
    ErrorCorrectionLevel errorCorrectionLevel = ErrorCorrectionLevel.L;
    int quietZone = QUIET_ZONE_SIZE;
    if (hints != null) {
        if (hints.containsKey(EncodeHintType.ERROR_CORRECTION)) {
            errorCorrectionLevel = ErrorCorrectionLevel.valueOf(hints.get(EncodeHintType.ERROR_CORRECTION).toString());
        }
        if (hints.containsKey(EncodeHintType.MARGIN)) {
            quietZone = Integer.parseInt(hints.get(EncodeHintType.MARGIN).toString());
        }
    }
    QRCode code = Encoder.encode(contents, errorCorrectionLevel, hints);
    return renderResult(code, width, height, quietZone);
}
Also used : QRCode(com.google.zxing.qrcode.encoder.QRCode) ErrorCorrectionLevel(com.google.zxing.qrcode.decoder.ErrorCorrectionLevel)

Example 12 with ErrorCorrectionLevel

use of com.google.zxing.qrcode.decoder.ErrorCorrectionLevel in project actframework by actframework.

the class ZXingResult method renderCode.

private void renderCode(H.Response response) {
    response.contentType("image/png");
    Map<EncodeHintType, Object> hints = new HashMap<>();
    hints.put(EncodeHintType.CHARACTER_SET, Act.appConfig().encoding());
    hints.put(EncodeHintType.MARGIN, 0);
    ErrorCorrectionLevel level = errorCorrectionLevel();
    if (null != level) {
        hints.put(EncodeHintType.ERROR_CORRECTION, level);
    }
    MultiFormatWriter writer = new MultiFormatWriter();
    try {
        BitMatrix bitMatrix = writer.encode(getMessage(), barcodeFormat(), width, height, hints);
        MatrixToImageWriter.writeToStream(bitMatrix, "png", response.outputStream());
    } catch (Exception e) {
        throw E.unexpected(e);
    }
}
Also used : EncodeHintType(com.google.zxing.EncodeHintType) HashMap(java.util.HashMap) MultiFormatWriter(com.google.zxing.MultiFormatWriter) BitMatrix(com.google.zxing.common.BitMatrix) ErrorCorrectionLevel(com.google.zxing.qrcode.decoder.ErrorCorrectionLevel)

Aggregations

ErrorCorrectionLevel (com.google.zxing.qrcode.decoder.ErrorCorrectionLevel)12 QRCode (com.google.zxing.qrcode.encoder.QRCode)6 BitMatrix (com.google.zxing.common.BitMatrix)5 EncodeHintType (com.google.zxing.EncodeHintType)4 QRCodeWriter (com.google.zxing.qrcode.QRCodeWriter)4 HashMap (java.util.HashMap)3 Bitmap (android.graphics.Bitmap)2 BinaryBitmap (com.google.zxing.BinaryBitmap)2 BufferedImage (java.awt.image.BufferedImage)2 IOException (java.io.IOException)2 Hashtable (java.util.Hashtable)2 BitMatrixEx (com.github.hui.quick.plugin.qrcode.wrapper.BitMatrixEx)1 MultiFormatWriter (com.google.zxing.MultiFormatWriter)1 Graphics2D (java.awt.Graphics2D)1 Charset (java.nio.charset.Charset)1 LocalizedApplicationKt.getLocalizedString (org.odk.collect.strings.localization.LocalizedApplicationKt.getLocalizedString)1