Search in sources :

Example 1 with ErrorCorrectionLevel

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

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

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

the class QRCodeUtils method generateQRBitMap.

public static Bitmap generateQRBitMap(String data, int sideLength) throws IOException, WriterException {
    final long time = System.currentTimeMillis();
    String compressedData = CompressionUtils.compress(data);
    // Maximum capacity for QR Codes is 4,296 characters (Alphanumeric)
    if (compressedData.length() > 4000) {
        throw new IOException(Collect.getInstance().getString(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, sideLength, sideLength, hints);
    Bitmap bmp = Bitmap.createBitmap(sideLength, sideLength, Bitmap.Config.RGB_565);
    for (int x = 0; x < sideLength; x++) {
        for (int y = 0; y < sideLength; y++) {
            bmp.setPixel(x, y, bitMatrix.get(x, y) ? Color.BLACK : Color.WHITE);
        }
    }
    Timber.i("QR Code generation took : %d ms", (System.currentTimeMillis() - time));
    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) IOException(java.io.IOException) BitMatrix(com.google.zxing.common.BitMatrix) ErrorCorrectionLevel(com.google.zxing.qrcode.decoder.ErrorCorrectionLevel)

Example 3 with ErrorCorrectionLevel

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

the class QrCodeCreateUtil method createQrCode.

/**
 * 生成包含字符串信息的二维码图片
 *
 * @param outputStream 文件输出流路径
 * @param content      二维码携带信息
 * @param qrCodeSize   二维码图片大小
 * @param imageFormat  二维码的格式
 * @throws WriterException
 * @throws IOException
 */
public static boolean createQrCode(OutputStream outputStream, String content, int qrCodeSize, String imageFormat) throws WriterException, IOException {
    // 设置二维码纠错级别MAP
    Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<EncodeHintType, ErrorCorrectionLevel>();
    // 矫错级别
    hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
    QRCodeWriter qrCodeWriter = new QRCodeWriter();
    // 创建比特矩阵(位矩阵)的QR码编码的字符串
    BitMatrix byteMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, qrCodeSize, qrCodeSize, hintMap);
    // 使BufferedImage勾画QRCode  (matrixWidth 是行二维码像素点)
    int matrixWidth = byteMatrix.getWidth();
    BufferedImage image = new BufferedImage(matrixWidth - 200, matrixWidth - 200, BufferedImage.TYPE_INT_RGB);
    image.createGraphics();
    Graphics2D graphics = (Graphics2D) image.getGraphics();
    graphics.setColor(Color.WHITE);
    graphics.fillRect(0, 0, matrixWidth, matrixWidth);
    // 使用比特矩阵画并保存图像
    graphics.setColor(Color.BLACK);
    for (int i = 0; i < matrixWidth; i++) {
        for (int j = 0; j < matrixWidth; j++) {
            if (byteMatrix.get(i, j)) {
                graphics.fillRect(i - 100, j - 100, 1, 1);
            }
        }
    }
    return ImageIO.write(image, imageFormat, outputStream);
}
Also used : QRCodeWriter(com.google.zxing.qrcode.QRCodeWriter) EncodeHintType(com.google.zxing.EncodeHintType) Hashtable(java.util.Hashtable) BitMatrix(com.google.zxing.common.BitMatrix) ErrorCorrectionLevel(com.google.zxing.qrcode.decoder.ErrorCorrectionLevel) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D)

Example 4 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 5 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)

Aggregations

ErrorCorrectionLevel (com.google.zxing.qrcode.decoder.ErrorCorrectionLevel)9 QRCode (com.google.zxing.qrcode.encoder.QRCode)5 EncodeHintType (com.google.zxing.EncodeHintType)3 BitMatrix (com.google.zxing.common.BitMatrix)3 QRCodeWriter (com.google.zxing.qrcode.QRCodeWriter)2 HashMap (java.util.HashMap)2 Bitmap (android.graphics.Bitmap)1 BinaryBitmap (com.google.zxing.BinaryBitmap)1 MultiFormatWriter (com.google.zxing.MultiFormatWriter)1 Graphics2D (java.awt.Graphics2D)1 BufferedImage (java.awt.image.BufferedImage)1 IOException (java.io.IOException)1 Charset (java.nio.charset.Charset)1 Hashtable (java.util.Hashtable)1