使用zxing类库可以很容易生成二维码QRCode,主要代码如下:
private Bitmap createQRCode(String str,int width,int height) { Bitmap bmp=null; if(str.equals(""))return null; try { MultiFormatWriter multiFormatWriter = new MultiFormatWriter(); Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>(); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); BitMatrix bitMatrix=null; try { bitMatrix = multiFormatWriter.encode(str, BarcodeFormat.QR_CODE, width, height,hints); } catch (WriterException e) { e.printStackTrace(); } int[] pixels = new int[width * height]; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { if (bitMatrix.get(x, y)) { pixels[y * width + x] = 0xff000000; } else { pixels[y * width + x] = 0xffffffff; } } } bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); bmp.setPixels(pixels, 0, width, 0, 0, width, height); } catch(Exception e) { e.printStackTrace(); bmp=null; } return bmp; }
时间: 2024-11-11 17:25:51