碰到了一個(gè)問(wèn)題,需求是要將每一個(gè)老人的二維碼展示在前臺(tái),可以讓不同的子女去掃描老人的二維碼,以達(dá)到快速綁定老人信息,我們平常掃描二維碼的時(shí)候,是將二維碼的信息解析為字符串等,現(xiàn)在剛好是反著來(lái)的。具體怎么做呢,請(qǐng)看代碼
/**
* 生成二維碼的方法
*
* @param address
* @return
*/
private Bitmap createQRImage(String address) {
try {
//判斷URL合法性
if (address == null || "".equals(address) || address.length() < 1) {
return null;
}
Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
//圖像數(shù)據(jù)轉(zhuǎn)換,使用了矩陣轉(zhuǎn)換
BitMatrix bitMatrix = new QRCodeWriter().encode(address, BarcodeFormat.QR_CODE, QR_WIDTH, QR_HEIGHT, hints);
int[] pixels = new int[QR_WIDTH * QR_HEIGHT];
//下面這里按照二維碼的算法,逐個(gè)生成二維碼的圖片,
//兩個(gè)for循環(huán)是圖片橫列掃描的結(jié)果
for (int y = 0; y < QR_HEIGHT; y++) {
for (int x = 0; x < QR_WIDTH; x++) {
if (bitMatrix.get(x, y)) {
pixels[y * QR_WIDTH + x] = 0xff000000;
} else {
pixels[y * QR_WIDTH + x] = 0xffffffff;
}
}
}
//生成二維碼圖片的格式,使用ARGB_8888
bitmap = Bitmap.createBitmap(QR_WIDTH, QR_HEIGHT, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, QR_WIDTH, 0, 0, QR_WIDTH, QR_HEIGHT);
//顯示到一個(gè)ImageView上面
imgQrcode.setImageBitmap(bitmap);
} catch (WriterException e) {
e.printStackTrace();
}
return bitmap;
}
最后返回Bitmap對(duì)象,剩下的就交給你處理了。
原文地址:http://hedgehog.love/2016/03/06/String-converted-into-Bitmap/ 轉(zhuǎn)載請(qǐng)注明出處!