【发布时间】:2020-07-04 22:09:02
【问题描述】:
我正在尝试使用 Java 在我的 Android 应用程序中使用 PGP 公钥生成 QR 码并将其设置为 ImageView。我使用的库是 XZing,我在 Android Studio 上。 这在使用较小的字符串之前效果很好,但是当我尝试使用像 PGP 公钥这样大的东西时,我得到了这个错误:
com.google.zxing.WriterException: Data too big
所以我的数据太大了。有没有可能在 XZing 中用这么大的字符串创建一个二维码?
我尝试了两种不同的方法,但都导致相同的异常。
方法一:
private void generateQR(String value) {
qrImage.setVisibility(View.VISIBLE);
QRGEncoder qrgEncoder = new QRGEncoder(value, null, QRGContents.Type.TEXT, 1000);
try {
// Getting QR-Code as Bitmap
Bitmap bm = qrgEncoder.encodeAsBitmap();
qrImage.setImageBitmap(bm);
// Setting Bitmap to ImageView
} catch (WriterException e) {
Log.v(TAG, e.toString());
}
}
方法二:
private Bitmap textToImage(String text, int width, int height) throws WriterException, NullPointerException {
BitMatrix bitMatrix;
try {
bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.DATA_MATRIX.QR_CODE,
width, height, null);
} catch (IllegalArgumentException Illegalargumentexception) {
return null;
}
int bitMatrixWidth = bitMatrix.getWidth();
int bitMatrixHeight = bitMatrix.getHeight();
int[] pixels = new int[bitMatrixWidth * bitMatrixHeight];
int colorWhite = 0xFFFFFFFF;
int colorBlack = 0xFF000000;
for (int y = 0; y < bitMatrixHeight; y++) {
int offset = y * bitMatrixWidth;
for (int x = 0; x < bitMatrixWidth; x++) {
pixels[offset + x] = bitMatrix.get(x, y) ? colorBlack : colorWhite;
}
}
Bitmap bitmap = Bitmap.createBitmap(bitMatrixWidth, bitMatrixHeight, Bitmap.Config.ARGB_4444);
bitmap.setPixels(pixels, 0, width, 0, 0, bitMatrixWidth, bitMatrixHeight);
return bitmap;
}
【问题讨论】:
标签: java android android-studio qr-code