Android 二维码生成

来源:互联网 发布:c语言中与的符号 编辑:程序博客网 时间:2024/05/18 01:34
content 为传入生成二维码的内容,500 为二维码的宽高
private Bitmap generateQRCode(String content) {   try {      QRCodeWriter writer = new QRCodeWriter();      // MultiFormatWriter writer = new MultiFormatWriter();      BitMatrix matrix = writer.encode(content, BarcodeFormat.QR_CODE,            500, 500);      return bitMatrix2Bitmap(matrix);   } catch (WriterException e) {      e.printStackTrace();   }   return null;}

private Bitmap bitMatrix2Bitmap(BitMatrix matrix) {   int w = matrix.getWidth();   int h = matrix.getHeight();   int[] rawData = new int[w * h];   for (int i = 0; i < w; i++) {      for (int j = 0; j < h; j++) {         int color = Color.WHITE;         if (matrix.get(i, j)) {            color = Color.BLACK;         }         rawData[i + (j * w)] = color;      }   }   Bitmap bitmap = Bitmap.createBitmap(w, h, Config.RGB_565);   bitmap.setPixels(rawData, 0, w, 0, 0, w, h);   return bitmap;}
0 0