我尝试显示由网址生成的二维码。但是位图没有显示
解决方法是什么。
我使用xzing
库:
compile 'com.google.zxing:core:3.2.1'
我的activity
:
public class QRCodeActivity extends AppCompatActivity {
private static final int WIDTH = 256;
private static final int BLACK = 0xFF000000;
private static final int WHITE = 0xFFFFFFFF;
@BindView(R.id.qrcode_toolbar)
Toolbar _toolBar;
@BindView(R.id.generate_code_button)
Button _generate_code_button;
@BindView(R.id.qrCode_imageView)
ImageView _qrCode_imageView;
Bitmap qrcode;
@Override
protected void onStart() {
super.onStart();
try {
qrcode = encodeAsBitmap("aadeaokode");
_qrCode_imageView.setImageBitmap(qrcode);
} catch (WriterException e) {
e.printStackTrace();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_qrcode);
ButterKnife.bind(this);
}
Bitmap encodeAsBitmap(String str) throws WriterException {
BitMatrix result;
try {
result = new MultiFormatWriter().encode(str,
BarcodeFormat.QR_CODE, WIDTH, WIDTH, 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) ? BLACK : WHITE;
}
}
Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, WIDTH, 0, 0, w, h);
return bitmap;
}
}