Search in sources :

Example 11 with QRCodeWriter

use of com.google.zxing.qrcode.QRCodeWriter in project krypton-android by kryptco.

the class MemberQR method onCreateClient.

@Subscribe(threadMode = ThreadMode.MAIN)
public void onCreateClient(TeamService.GenerateClientResult r) {
    if (r.c.error != null) {
        Error.shortToast(getContext(), r.c.error);
        Transitions.beginFade(this).remove(this).commitAllowingStateLoss();
        return;
    }
    Sigchain.Identity identity = r.c.success;
    Sigchain.MemberQRPayload memberQRPayload = new Sigchain.MemberQRPayload(identity.email, identity.publicKey);
    Sigchain.QRPayload qrPayload = new Sigchain.QRPayload(memberQRPayload);
    try {
        BitMatrix qrData = new QRCodeWriter().encode(JSON.toJson(qrPayload), BarcodeFormat.DATA_MATRIX.QR_CODE, 500, 500);
        qr.setImageBitmap(new BarcodeEncoder().createBitmap(qrData));
        qrPayloads.set(new QRPayloads(memberQRPayload, MemberScan.lastScannedPayload.get()));
        loadQRProgress.setAlpha(0);
        joinProgress.animate().setDuration(1000).alpha(1).start();
        new JoinTeamProgress(getContext()).updateTeamData((s, d) -> {
            d.identity = identity;
            d.teamName = qrPayloads.get().admin.teamName;
            return s;
        });
    } catch (WriterException e) {
        e.printStackTrace();
        Error.shortToast(getContext(), "Error creating QRCode");
        Transitions.beginFade(this).remove(this).commitAllowingStateLoss();
        return;
    }
    EventBus.getDefault().post(new PollRead());
}
Also used : Sigchain(co.krypt.krypton.team.Sigchain) BarcodeEncoder(com.journeyapps.barcodescanner.BarcodeEncoder) JoinTeamProgress(co.krypt.krypton.team.onboarding.join.JoinTeamProgress) BitMatrix(com.google.zxing.common.BitMatrix) QRCodeWriter(com.google.zxing.qrcode.QRCodeWriter) WriterException(com.google.zxing.WriterException) Subscribe(org.greenrobot.eventbus.Subscribe)

Example 12 with QRCodeWriter

use of com.google.zxing.qrcode.QRCodeWriter in project mycore by MyCoRe-Org.

the class MCRQRCodeServlet method getPNGContent.

private MCRContent getPNGContent(final String url, final int size) throws IOException {
    QRCodeWriter writer = new QRCodeWriter();
    BitMatrix matrix;
    try {
        matrix = writer.encode(url, BarcodeFormat.QR_CODE, size, size);
    } catch (WriterException e) {
        throw new IOException(e);
    }
    BufferedImage image = toBufferedImage(matrix);
    return pngTools.toPNGContent(image);
}
Also used : QRCodeWriter(com.google.zxing.qrcode.QRCodeWriter) BitMatrix(com.google.zxing.common.BitMatrix) IOException(java.io.IOException) WriterException(com.google.zxing.WriterException) BufferedImage(java.awt.image.BufferedImage)

Example 13 with QRCodeWriter

use of com.google.zxing.qrcode.QRCodeWriter in project i2p.i2p-bote by i2p.

the class QrCodeUtils method getQRCodeBitmap.

/**
 * Generate Bitmap with QR Code based on input.
 *
 * @param input The data to render as a QR code.
 * @param size The preferred width and height of the QR code in pixels.
 * @return QR Code as Bitmap
 */
public static Bitmap getQRCodeBitmap(final String input, final int size) {
    try {
        final Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
        final BitMatrix result = new QRCodeWriter().encode(input, BarcodeFormat.QR_CODE, size, size, hints);
        final int width = result.getWidth();
        final int height = result.getHeight();
        final int[] pixels = new int[width * height];
        for (int y = 0; y < height; y++) {
            final int offset = y * width;
            for (int x = 0; x < width; x++) {
                pixels[offset + x] = result.get(x, y) ? Color.BLACK : Color.WHITE;
            }
        }
        final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
        return bitmap;
    } catch (final WriterException e) {
        Log log = I2PAppContext.getGlobalContext().logManager().getLog(QrCodeUtils.class);
        if (log.shouldLog(Log.ERROR))
            log.error("QrCodeUtils", e);
        return null;
    }
}
Also used : QRCodeWriter(com.google.zxing.qrcode.QRCodeWriter) Bitmap(android.graphics.Bitmap) EncodeHintType(com.google.zxing.EncodeHintType) Log(net.i2p.util.Log) Hashtable(java.util.Hashtable) BitMatrix(com.google.zxing.common.BitMatrix) WriterException(com.google.zxing.WriterException)

Example 14 with QRCodeWriter

use of com.google.zxing.qrcode.QRCodeWriter in project wechat by motianhuo.

the class GetMoneyActivity method generateQRCode.

private Bitmap generateQRCode(String content) {
    try {
        QRCodeWriter writer = new QRCodeWriter();
        // MultiFormatWriter writer = new MultiFormatWriter();
        BitMatrix matrix = writer.encode(content, BarcodeFormat.QR_CODE, 500, 500);
        return bitMatrix2Bitmap(matrix);
    } catch (WriterException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : QRCodeWriter(com.google.zxing.qrcode.QRCodeWriter) BitMatrix(com.google.zxing.common.BitMatrix) WriterException(com.google.zxing.WriterException)

Example 15 with QRCodeWriter

use of com.google.zxing.qrcode.QRCodeWriter in project android-zxingLibrary by yipianfengye.

the class CodeUtils method createImage.

/**
 * 生成二维码图片
 * @param text
 * @param w
 * @param h
 * @param logo
 * @return
 */
public static Bitmap createImage(String text, int w, int h, Bitmap logo) {
    if (TextUtils.isEmpty(text)) {
        return null;
    }
    try {
        Bitmap scaleLogo = getScaleLogo(logo, w, h);
        int offsetX = w / 2;
        int offsetY = h / 2;
        int scaleWidth = 0;
        int scaleHeight = 0;
        if (scaleLogo != null) {
            scaleWidth = scaleLogo.getWidth();
            scaleHeight = scaleLogo.getHeight();
            offsetX = (w - scaleWidth) / 2;
            offsetY = (h - scaleHeight) / 2;
        }
        Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        // 容错级别
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        // 设置空白边距的宽度
        hints.put(EncodeHintType.MARGIN, 0);
        BitMatrix bitMatrix = new QRCodeWriter().encode(text, BarcodeFormat.QR_CODE, w, h, hints);
        int[] pixels = new int[w * h];
        for (int y = 0; y < h; y++) {
            for (int x = 0; x < w; x++) {
                if (x >= offsetX && x < offsetX + scaleWidth && y >= offsetY && y < offsetY + scaleHeight) {
                    int pixel = scaleLogo.getPixel(x - offsetX, y - offsetY);
                    if (pixel == 0) {
                        if (bitMatrix.get(x, y)) {
                            pixel = 0xff000000;
                        } else {
                            pixel = 0xffffffff;
                        }
                    }
                    pixels[y * w + x] = pixel;
                } else {
                    if (bitMatrix.get(x, y)) {
                        pixels[y * w + x] = 0xff000000;
                    } else {
                        pixels[y * w + x] = 0xffffffff;
                    }
                }
            }
        }
        Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        bitmap.setPixels(pixels, 0, w, 0, 0, w, h);
        return bitmap;
    } catch (WriterException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : Bitmap(android.graphics.Bitmap) BinaryBitmap(com.google.zxing.BinaryBitmap) QRCodeWriter(com.google.zxing.qrcode.QRCodeWriter) EncodeHintType(com.google.zxing.EncodeHintType) Hashtable(java.util.Hashtable) BitMatrix(com.google.zxing.common.BitMatrix) WriterException(com.google.zxing.WriterException)

Aggregations

BitMatrix (com.google.zxing.common.BitMatrix)32 QRCodeWriter (com.google.zxing.qrcode.QRCodeWriter)32 EncodeHintType (com.google.zxing.EncodeHintType)24 WriterException (com.google.zxing.WriterException)22 Bitmap (android.graphics.Bitmap)19 Hashtable (java.util.Hashtable)12 HashMap (java.util.HashMap)7 BufferedImage (java.awt.image.BufferedImage)6 IOException (java.io.IOException)5 EnumMap (java.util.EnumMap)5 BinaryBitmap (com.google.zxing.BinaryBitmap)4 ErrorCorrectionLevel (com.google.zxing.qrcode.decoder.ErrorCorrectionLevel)4 Graphics2D (java.awt.Graphics2D)4 Color (java.awt.Color)3 Paint (android.graphics.Paint)2 NonNull (android.support.annotation.NonNull)2 Sigchain (co.krypt.krypton.team.Sigchain)2 BarcodeFormat (com.google.zxing.BarcodeFormat)2 BarcodeEncoder (com.journeyapps.barcodescanner.BarcodeEncoder)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2