生成专属二维码

时间:2022-11-17 07:39:32

项目的需要,最近做了一个自动生成二维码,希望对大家有帮助。请看效果图

生成专属二维码生成专属二维码

首先就是要添加依赖集成Zxing 

compile 'com.google.zxing:core:3.2.1'

接下来输入你想输入的东西,点击生成二维码

mGetQr.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        String content = mContent.getText().toString();
        if (!TextUtils.isEmpty(content)) {
            try {
                content = new String(content.getBytes("UTF-8"), "ISO-8859-1");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            MakeQrDialog.getInstance(content, "1", "ok").show(getSupportFragmentManager(), "tag");
        } else {
            Toast.makeText(MainActivity.this, "请输入内容", Toast.LENGTH_SHORT).show();
        }
    }
});

最后就是对二维码的处理了,以下是处理二维码的代码

private final static QRCodeWriter QR_CODE_WRITER = new QRCodeWriter();
private static final ErrorCorrectionLevel ERROR_CORRECTION_LEVEL = ErrorCorrectionLevel.M;
private static final int DARK_COLOR = 0xdd000000;
private static final int LIGHT_COLOR = 0;

public static boolean setQr(final ImageView view, final Resources res, final String content) {
    return setQr(view, res, content, R.dimen.qr_code_size, R.dimen.qr_code_quite_zone_pixels);
}
private static boolean setQr(ImageView view, Resources res, String content,
                             int viewSizeResId, int qrQuiteZoneResId) {
    int qrCodeViewSize = res.getDimensionPixelSize(viewSizeResId);
    int qrQuiteZone = (int) res.getDimension(qrQuiteZoneResId);

    Bitmap bitmap = create(content, qrQuiteZone);
    if (bitmap == null) {
        return false;
    }

    BitmapDrawable qr = new BitmapDrawable(res, bitmap);
    qr.setFilterBitmap(false);
    view.requestLayout();
    view.setImageDrawable(qr);
    return true;
}

public static Bitmap create(final String content, final int marginSize) {
    return create(content, DARK_COLOR, LIGHT_COLOR, marginSize);
}

public static Bitmap create(final String content, final int darkColor, final int lightColor,
                            final int marginSize) {
    try {
        QRCode code = Encoder.encode(content, ERROR_CORRECTION_LEVEL, null);
        int size = code.getMatrix().getWidth();

        final Hashtable<EncodeHintType, Object> hints = new Hashtable<>();
        hints.put(EncodeHintType.MARGIN, marginSize);
        hints.put(EncodeHintType.ERROR_CORRECTION, ERROR_CORRECTION_LEVEL);
        final BitMatrix result =
                QR_CODE_WRITER.encode(content, 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) ? darkColor : lightColor;
            }
        }
        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 x) {
        return null;
    }
}

最后贴上项目的代码,想下载的朋友可以看一下。

https://download.csdn.net/download/lou_liang/10294758