【 开源框架 】Zxing 使用

【 开源框架 】Zxing 使用_第1张图片

Zxing GitHub 地址:

https://github.com/zxing/zxing

Zxing 使用

Zxing 二维码生成工具类

/**
 * Created by JerryLiu on 2017/1/22.
 */

public class QRCodeUtil {
    //宽度值,影响中间图片大小
    private static int IMAGE_HALFWIDTH = 50;

    /**
     *  //生成二维码,默认大小500*500,网址,汉字
     * @param text
     * @return
     */
   
    public static Bitmap createQRCode(String text) {
        return createQRCode(text, 500);
    }

    /**
     * 生成二维码,不带logo
     *
     * @param text
     * @param size
     * @return
     */
    public static Bitmap createQRCode(String text, int size) {
        try {
            Hashtable 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;
        }
    }

    /**
     * 生成默认大小带图片的二维码
     * @param text
     * @param bitmap
     * @return
     */
    public static Bitmap createQRCodeWithLogo(String text, Bitmap bitmap) {
        return createQRCodeWithLogo(text, 500, bitmap);
    }

    /**
     * 生成带图片的二维码
     * @param text 文本,网址
     * @param size 图片大小
     * @param bitmap 图片
     * @return
     */
    public static Bitmap createQRCodeWithLogo(String text, int size, Bitmap bitmap) {
        try {
            IMAGE_HALFWIDTH = size / 10;
            Hashtable hints = new Hashtable<>();
            hints.put(EncodeHintType.CHARACTER_SET, "utf-8");

            hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
            BitMatrix bitMatrix = new QRCodeWriter().encode(text, BarcodeFormat.QR_CODE, size, size, hints);

            int width = bitMatrix.getWidth();
            int height = bitMatrix.getHeight();
            int halfW = width / 2;
            int halfH = height / 2;

            Matrix m = new Matrix();
            float sx = (float) 2 * IMAGE_HALFWIDTH / bitmap.getWidth();
            float sy = (float) 2 * IMAGE_HALFWIDTH / bitmap.getHeight();
            m.setScale(sx, sy);
            bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), m, false);

            int[] pixels = new int[size * size];
            for (int y = 0; y < size; y++) {
                for (int x = 0; x < size; x++) {
                    if (x > halfW - IMAGE_HALFWIDTH && x < halfW + IMAGE_HALFWIDTH &&
                            y > halfH - IMAGE_HALFWIDTH && y < halfH + IMAGE_HALFWIDTH) {
                        pixels[y * width + x] = bitmap.getPixel(x - halfW + IMAGE_HALFWIDTH, y - halfH + IMAGE_HALFWIDTH);
                    } else {
                        if (bitMatrix.get(x, y)) {
                            pixels[y * size + x] = 0xff000000;
                        } else {
                            pixels[y * size + x] = 0xffffffff;
                        }
                    }
                }
            }
            Bitmap bitmap1 = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
            bitmap1.setPixels(pixels, 0, size, 0, 0, size, size);
            return bitmap1;
        } catch (WriterException e) {
            e.printStackTrace();
            return null;
        }
    }
}

参考链接:http://wuxiaolong.me/2016/04/22/zxing/

你可能感兴趣的:(【 开源框架 】Zxing 使用)