android zxing根据url生成二维码
时间: 2023-11-09 22:06:12 浏览: 45
你可以使用ZXing库来生成二维码。首先,需要在你的项目中添加ZXing库的依赖。然后,你可以使用以下代码来生成一个包含URL的二维码:
```
String url = "http://www.example.com";
int width = 500;
int height = 500;
QRCodeWriter writer = new QRCodeWriter();
BitMatrix bitMatrix = writer.encode(url, BarcodeFormat.QR_CODE, width, height);
int matrixWidth = bitMatrix.getWidth();
int matrixHeight = bitMatrix.getHeight();
Bitmap bmp = Bitmap.createBitmap(matrixWidth, matrixHeight, Bitmap.Config.RGB_565);
for (int x = 0; x < matrixWidth; x++) {
for (int y = 0; y < matrixHeight; y++) {
bmp.setPixel(x, y, bitMatrix.get(x, y) ? Color.BLACK : Color.WHITE);
}
}
ImageView imageView = (ImageView) findViewById(R.id.qr_code_image_view);
imageView.setImageBitmap(bmp);
```
这段代码将生成一个500x500像素的二维码,其中包含了"http://www.example.com"的URL。你可以将生成的二维码显示在一个ImageView中。