使用zxing生成二维码

 

使用zxing生成二维码

private void getQR(ImageView imageView, String test) {
    int width = imageView.getWidth();
    int height = imageView.getHeight();
    try {
        HashMap<EncodeHintType, Object> hints = new HashMap<>();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        hints.put(EncodeHintType.MARGIN, 0);
        BitMatrix encode = new QRCodeWriter().encode(test, BarcodeFormat.QR_CODE, width, height, hints);
        //数组颜色计算
        int[] pixel = new int[width * height];
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                if (encode.get(j, i)) {
                    pixel[i * width + j] = 0xff000000;
                } else {
                    pixel[i * width + j] = 0xffffffff;
                }
            }
        }
        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        bitmap.setPixels(pixel, 0, width, 0, 0, width, height);
        imageView.setImageBitmap(bitmap);
    } catch (Exception e) {
        e.printStackTrace();
    }
}