阅读背景:

Android 使用zxing生成和识别二维码

来源:互联网 

1.生成二维码

/**
     * 生成二维码
     *
     * @param contents 内容
     * @param width
     * @param height
     * @return 返回Bitmap对象
     * @throws WriterException
     */
    public static Bitmap encodeAsBitmap(String contents, int width, int height) {
        try {
            String contentsToEncode = contents;
            if (contentsToEncode == null) {
                return null;
            }
            Map<EncodeHintType, Object> hints = null;
            String encoding = "utf-8";
            if (encoding != null) {
                hints = new EnumMap<>(EncodeHintType.class);
                hints.put(EncodeHintType.CHARACTER_SET, encoding);
            }
            BitMatrix result;

            result = new MultiFormatWriter().encode(contentsToEncode, BarcodeFormat.QR_CODE, width, height, hints);

            int[] pixels = new int[width * height];
            for (int y = 0; y < height; y++) {
                int offset = y * width;
                for (int x = 0; x < width; x++) {
                    pixels[offset + x] = result.get(x, y) ? BLACK : WHITE;
                }
            }
            Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
            bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
            return bitmap;
        } catch (WriterException e) {
            e.printStackTrace();
            return null;
        }
    }/**
     * 生成二维码
     *
   



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: