Search in sources :

Example 31 with WriterException

use of com.google.zxing.WriterException in project smartmodule by carozhu.

the class GenerateQrcodePIcHelper method createQRCode.

/**
     * 生成二维码
     *
     * @param text 需要生成二维码的文字、网址等
     * @param size 需要生成二维码的大小()
     * @return bitmap
     */
public static Bitmap createQRCode(String text, int size) {
    try {
        Hashtable<EncodeHintType, String> hints = new Hashtable<>();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        BitMatrix bitMatrix = new QRCodeWriter().encode(text, BarcodeFormat.QR_CODE, size, size, hints);
        int[] pixels = new int[size * size];
        for (int y = 0; y < size; y++) {
            for (int x = 0; x < size; x++) {
                if (bitMatrix.get(x, y)) {
                    pixels[y * size + x] = 0xff000000;
                } else {
                    pixels[y * size + x] = 0xffffffff;
                }
            }
        }
        Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
        bitmap.setPixels(pixels, 0, size, 0, 0, size, size);
        return bitmap;
    } catch (WriterException e) {
        e.printStackTrace();
        return null;
    }
}
Also used : QRCodeWriter(com.google.zxing.qrcode.QRCodeWriter) Bitmap(android.graphics.Bitmap) EncodeHintType(com.google.zxing.EncodeHintType) Hashtable(java.util.Hashtable) BitMatrix(com.google.zxing.common.BitMatrix) WriterException(com.google.zxing.WriterException)

Example 32 with WriterException

use of com.google.zxing.WriterException in project JustAndroid by chinaltz.

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

WriterException (com.google.zxing.WriterException)32 Bitmap (android.graphics.Bitmap)14 BitMatrix (com.google.zxing.common.BitMatrix)10 QRCodeWriter (com.google.zxing.qrcode.QRCodeWriter)10 IOException (java.io.IOException)8 Intent (android.content.Intent)7 EncodeHintType (com.google.zxing.EncodeHintType)7 BitArray (com.google.zxing.common.BitArray)7 FileOutputStream (java.io.FileOutputStream)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 Hashtable (java.util.Hashtable)4 Point (android.graphics.Point)3 Uri (android.net.Uri)3 Bundle (android.os.Bundle)3 Display (android.view.Display)3 WindowManager (android.view.WindowManager)3 ImageView (android.widget.ImageView)3 TextView (android.widget.TextView)3 Result (com.google.zxing.Result)3 AddressBookParsedResult (com.google.zxing.client.result.AddressBookParsedResult)3