我包括了我的堆栈跟踪图像
MultiFormatWriter multiFormatWriter = new
MultiFormatWriter();
try {
BitMatrix bitMatrix = multiFormatWriter.encode(uid,
BarcodeFormat.QR_CODE,200,200);----55
BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
Bitmap bitmap = barcodeEncoder.createBitmap(bitMatrix);
imageView.setImageBitmap(Bitmap.createBitmap(bitmap));
} catch (WriterException e) {
e.printStackTrace();
}
目前在55处出现空指针异常错误。我使用了zxing库,但无法执行所需的操作,请帮助我
答案 0 :(得分:0)
尝试
public static Bitmap encodeStringToBitmap(String contents) throws WriterException {
//Null check, just b/c
if (contents == null) {
return null;
}
Map<EncodeHintType, Object> hints = new EnumMap<>(EncodeHintType.class);
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
MultiFormatWriter writer = new MultiFormatWriter();
BitMatrix result = writer.encode(contents, BarcodeFormat.PDF_417, 700, 900, hints);
int width = result.getWidth();
int height = result.getHeight();
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
int offset = y * width;
for (int x = 0; x < width; x++) {
pixels[offset + x] = result.get(x, y) ? 0xFF000000 : 0xFFFFFFFF;
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}
更多试用