Search in sources :

Example 6 with ErrorCorrectionLevel

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

the class ChartServlet method doParseParameters.

private static ChartServletRequestParameters doParseParameters(ServletRequest request, boolean readBody) throws IOException {
    Preconditions.checkArgument("qr".equals(request.getParameter("cht")), "Bad type");
    String widthXHeight = request.getParameter("chs");
    Preconditions.checkNotNull(widthXHeight, "No size");
    int xIndex = widthXHeight.indexOf('x');
    Preconditions.checkArgument(xIndex >= 0, "Bad size");
    int width = Integer.parseInt(widthXHeight.substring(0, xIndex));
    int height = Integer.parseInt(widthXHeight.substring(xIndex + 1));
    Preconditions.checkArgument(width > 0 && height > 0, "Bad size");
    Preconditions.checkArgument(width <= MAX_DIMENSION && height <= MAX_DIMENSION, "Bad size");
    String outputEncodingName = request.getParameter("choe");
    Charset outputEncoding = StandardCharsets.UTF_8;
    if (outputEncodingName != null) {
        outputEncoding = Charset.forName(outputEncodingName);
        Preconditions.checkArgument(SUPPORTED_OUTPUT_ENCODINGS.contains(outputEncoding), "Bad output encoding");
    }
    ErrorCorrectionLevel ecLevel = ErrorCorrectionLevel.L;
    int margin = 4;
    String ldString = request.getParameter("chld");
    if (ldString != null) {
        int pipeIndex = ldString.indexOf('|');
        if (pipeIndex < 0) {
            // Only an EC level
            ecLevel = ErrorCorrectionLevel.valueOf(ldString);
        } else {
            ecLevel = ErrorCorrectionLevel.valueOf(ldString.substring(0, pipeIndex));
            margin = Integer.parseInt(ldString.substring(pipeIndex + 1));
            Preconditions.checkArgument(margin > 0, "Bad margin");
        }
    }
    String text;
    if (readBody) {
        text = CharStreams.toString(request.getReader());
    } else {
        text = request.getParameter("chl");
    }
    Preconditions.checkArgument(text != null && !text.isEmpty(), "No input");
    return new ChartServletRequestParameters(width, height, outputEncoding, ecLevel, margin, text);
}
Also used : Charset(java.nio.charset.Charset) ErrorCorrectionLevel(com.google.zxing.qrcode.decoder.ErrorCorrectionLevel)

Example 7 with ErrorCorrectionLevel

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

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 8 with ErrorCorrectionLevel

use of com.google.zxing.qrcode.decoder.ErrorCorrectionLevel in project weex-example by KalicyZhou.

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 9 with ErrorCorrectionLevel

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

the class QRCodeUtils method encode.

public Bitmap encode(String data) throws IOException, WriterException {
    String compressedData = CompressionUtils.compress(data);
    // Maximum capacity for QR Codes is 4,296 characters (Alphanumeric)
    if (compressedData.length() > 4000) {
        throw new IOException(getLocalizedString(Collect.getInstance(), R.string.encoding_max_limit));
    }
    Map<EncodeHintType, ErrorCorrectionLevel> hints = new HashMap<>();
    hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
    QRCodeWriter qrCodeWriter = new QRCodeWriter();
    BitMatrix bitMatrix = qrCodeWriter.encode(compressedData, BarcodeFormat.QR_CODE, QR_CODE_SIDE_LENGTH, QR_CODE_SIDE_LENGTH, hints);
    Bitmap bmp = Bitmap.createBitmap(QR_CODE_SIDE_LENGTH, QR_CODE_SIDE_LENGTH, Bitmap.Config.RGB_565);
    for (int x = 0; x < QR_CODE_SIDE_LENGTH; x++) {
        for (int y = 0; y < QR_CODE_SIDE_LENGTH; y++) {
            bmp.setPixel(x, y, bitMatrix.get(x, y) ? Color.BLACK : Color.WHITE);
        }
    }
    return bmp;
}
Also used : QRCodeWriter(com.google.zxing.qrcode.QRCodeWriter) Bitmap(android.graphics.Bitmap) BinaryBitmap(com.google.zxing.BinaryBitmap) EncodeHintType(com.google.zxing.EncodeHintType) HashMap(java.util.HashMap) LocalizedApplicationKt.getLocalizedString(org.odk.collect.strings.localization.LocalizedApplicationKt.getLocalizedString) IOException(java.io.IOException) BitMatrix(com.google.zxing.common.BitMatrix) ErrorCorrectionLevel(com.google.zxing.qrcode.decoder.ErrorCorrectionLevel)

Example 10 with ErrorCorrectionLevel

use of com.google.zxing.qrcode.decoder.ErrorCorrectionLevel in project incubator-weex by apache.

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)

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