1 回答
TA贡献1839条经验 获得超15个赞
你有没有试过使用ZXing,它是一个非常酷的QR码库之类的东西,这是我不久前的做法:
//generate and set QR code
ImageView imgQRCode = (ImageView) findViewById(R.id.imgQRCode);
try {
Bitmap qr = encodeAsBitmap("Any String HERE");
if(qr != null)
imgQRCode.setImageBitmap(qr);
else {
//Do whatever based on your logic
//Toast.makeText(Prompt_ViewQRActivity.this, "Error message", Toast.LENGTH_LONG).show();
//finish();
}
} catch (Exception e) {
}
然后是“encodeAsBitmap”方法
Bitmap encodeAsBitmap(String str) throws WriterException {
BitMatrix result;
try {
result = new MultiFormatWriter().encode(str, BarcodeFormat.QR_CODE, 300, 300, null);
} catch (IllegalArgumentException iae) {
// Unsupported format
return null;
}
int w = result.getWidth();
int h = result.getHeight();
int[] pixels = new int[w * h];
for (int y = 0; y < h; y++) {
int offset = y * w;
for (int x = 0; x < w; x++) {
pixels[offset + x] = result.get(x, y) ? Color.BLACK : Color.WHITE;
}
}
Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, 300, 0, 0, w, h);
return bitmap;
}
添加回答
举报
