android - 如何使用 Zxing 缩短生成二维码的时间

标签 android qr-code

我正在创建一个带有二维码的应用程序。条形码加载正确,但加载速度有点慢,在我点击/单击菜单后大约需要 3-5 秒。

我们可以让它更快吗?还是页面加载那么长时间正常?其他部分加载仅需 1 秒或更短时间。该应用程序也离线,因此不需要互联网连接。

这里是我生成二维码的代码:

ImageView imageViewBarcode = (ImageView)findViewById(R.id.imageViewBarcode);

    try {
        bitmap = TextToImageEncode(barcode_user);

        imageViewBarcode.setImageBitmap(bitmap);

    } catch (WriterException e) {
        e.printStackTrace();
    }

上面的代码放在onCreate里面。因此,当页面加载时,它会生成条形码。

这里是创建条形码的函数

Bitmap TextToImageEncode(String Value) throws WriterException {
    BitMatrix bitMatrix;
    try {
        bitMatrix = new MultiFormatWriter().encode(
                Value,
                BarcodeFormat.DATA_MATRIX.QR_CODE,
                QRcodeWidth, QRcodeWidth, null
        );

    } catch (IllegalArgumentException Illegalargumentexception) {

        return null;
    }
    int bitMatrixWidth = bitMatrix.getWidth();

    int bitMatrixHeight = bitMatrix.getHeight();

    int[] pixels = new int[bitMatrixWidth * bitMatrixHeight];

    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) ?
                    getResources().getColor(R.color.colorBlack):getResources().getColor(R.color.colorWhite);
        }
    }
    Bitmap bitmap = Bitmap.createBitmap(bitMatrixWidth, bitMatrixHeight, Bitmap.Config.ARGB_4444);

    bitmap.setPixels(pixels, 0, 500, 0, 0, bitMatrixWidth, bitMatrixHeight);
    return bitmap;
}

最佳答案

您在双循环内调用 getResources().getColor() - 即当您的图像大小为 100*100 像素时,这将被调用 10000 次。而是将颜色值分配给循环外的一些变量,并在循环内使用这些变量。

int color_black = getResources().getColor(R.color.colorBlack);
int color_white = getResources().getColor(R.color.colorWhite);

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) ? color_black : color_white;
    }
}

编辑:添加代码示例

关于android - 如何使用 Zxing 缩短生成二维码的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43754378/

相关文章:

android - 在 Google AppEngine 上运行部署的应用程序时出现问题

java - 3 个 TextView 旁边的 Android 布局按钮

android - 如何去除侧面二维码上的空白(使用zxing)

java - 我可以在没有网络摄像头的情况下使用 Android 模拟器扫描二维码吗?

java - 我在动态生成二维码时遇到问题

java - getHeight 为所有 Android UI 对象返回 0

android - 如何在 Android 中的软键盘上方显示文本

java - 如何在二维码上添加标志

ios - 如何在 Swift 中检测图像中的条形码?

android - EMV - 生成 AC - 验证码