如何为Android应用程序生成QR码?

Rad*_*adu 77 java android qr-code

我需要在我的Android应用程序中创建一个qrcode,我需要一个库或源代码,让我在Android应用程序中创建一个QR码.

我需要的图书馆必须:

  1. 不要留下水印(如onbarcode图书馆)
  2. 不使用Web服务API来创建qrcode(如Google的库zxing)
  3. 不需要第三方安装程序(如QR Droid)

我已经为iPhone创建了这样的代码(Objective-C),但我需要快速修复Android,直到我有时间制作自己的QR代码生成器.这是我的第一个Android项目,所以任何帮助将不胜感激.

Ste*_*ano 83

与zxing这是我创建QR的代码

 QRCodeWriter writer = new QRCodeWriter();
    try {
        BitMatrix bitMatrix = writer.encode(content, BarcodeFormat.QR_CODE, 512, 512);
        int width = bitMatrix.getWidth();
        int height = bitMatrix.getHeight();
        Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                bmp.setPixel(x, y, bitMatrix.get(x, y) ? Color.BLACK : Color.WHITE);
            }
        }
        ((ImageView) findViewById(R.id.img_result_qr)).setImageBitmap(bmp);

    } catch (WriterException e) {
        e.printStackTrace();
    }
Run Code Online (Sandbox Code Playgroud)


Rob*_*Rob 63

你看过ZXING了吗?我一直在成功地使用它来创建条形码.您可以在比特币应用程序src中看到完整的工作示例

// this is a small sample use of the QRCodeEncoder class from zxing
try {
    // generate a 150x150 QR code
    Bitmap bm = encodeAsBitmap(barcode_content, BarcodeFormat.QR_CODE, 150, 150);

    if(bm != null) {
        image_view.setImageBitmap(bm);
    }
} catch (WriterException e) { //eek }
Run Code Online (Sandbox Code Playgroud)


Ant*_*wan 30

也许这个老话题,但我发现这个库非常有用,易于使用

QRGen

在android中使用它的示例

 Bitmap myBitmap = QRCode.from("www.example.org").bitmap();
ImageView myImage = (ImageView) findViewById(R.id.imageView);
myImage.setImageBitmap(myBitmap);
Run Code Online (Sandbox Code Playgroud)

  • 找不到.bitmap() (3认同)
  • 这是一个简单而有效的解决方案! (2认同)
  • 您应该使用 android 依赖项:`implementation 'com.github.kenglxn.QRGen:android:[version]'` 并导入 QRCode 类,如下所示:`import net.glxn.qrgen.android.QRCode` (2认同)
  • 无法解析:com.github.kenglxn.QRGen:android:2.6.0 (2认同)
  • 在您的项目 build.gradle 中添加 maven { url "https://jitpack.io" } 到 allprojects.repositiories (2认同)

blu*_*t83 14

这是我生成Bitmap的简单工作函数!我只使用ZXing1.3.jar!我还将修正等级设置为高!

PS:x和y相反,这是正常的,因为bitMatrix反转x和y.此代码与方形图像完美匹配.

public static Bitmap generateQrCode(String myCodeText) throws WriterException {
    Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<EncodeHintType, ErrorCorrectionLevel>();
    hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); // H = 30% damage

    QRCodeWriter qrCodeWriter = new QRCodeWriter();

    int size = 256;

    ByteMatrix bitMatrix = qrCodeWriter.encode(myCodeText,BarcodeFormat.QR_CODE, size, size, hintMap);
    int width = bitMatrix.width();
    Bitmap bmp = Bitmap.createBitmap(width, width, Bitmap.Config.RGB_565);
    for (int x = 0; x < width; x++) {
        for (int y = 0; y < width; y++) {
            bmp.setPixel(y, x, bitMatrix.get(x, y)==0 ? Color.BLACK : Color.WHITE);
        }
    }
    return bmp;
}
Run Code Online (Sandbox Code Playgroud)

编辑

使用像素int数组而不是bitmap.setPixel逐位使用bitmap.setPixels(...)会更快:

        BitMatrix bitMatrix = writer.encode(inputValue, BarcodeFormat.QR_CODE, size, size);
        int width = bitMatrix.getWidth();
        int height = bitMatrix.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] = bitMatrix.get(x, y) ? BLACK : WHITE;
            }
        }

        bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
Run Code Online (Sandbox Code Playgroud)

  • @dikkini为什么您要编辑我的代码?我不批准。你测试了吗?我不沉,所以,我的工作正常。那不是复制粘贴。 (2认同)
  • 我编辑您的代码是因为我在项目中使用了它。我测试一下。我支持您的回答,因为这是不错的代码,但有一点错误。 (2认同)

Ada*_*zak 10

我使用了zxing-1.3 jar,我不得不做一些改变来实现其他答案的代码,所以我会留下我的解决方案给别人.我做了以下事情:

1)找到zxing-1.3.jar,下载并添加属性(添加外部jar).

2)在我的活动布局中添加ImageView并命名它(在我的例子中它是tnsd_iv_qr).

3)在我的活动中包含用于创建qr图像的代码(在此示例中,我为比特币支付创建了QR):

    QRCodeWriter writer = new QRCodeWriter();
    ImageView tnsd_iv_qr = (ImageView)findViewById(R.id.tnsd_iv_qr);
    try {
        ByteMatrix bitMatrix = writer.encode("bitcoin:"+btc_acc_adress+"?amount="+amountBTC, BarcodeFormat.QR_CODE, 512, 512);
        int width = 512;
        int height = 512;
        Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                if (bitMatrix.get(x, y)==0)
                    bmp.setPixel(x, y, Color.BLACK);
                else
                    bmp.setPixel(x, y, Color.WHITE);
            }
        }
        tnsd_iv_qr.setImageBitmap(bmp);
    } catch (WriterException e) {
        //Log.e("QR ERROR", ""+e);

    }
Run Code Online (Sandbox Code Playgroud)

如果有人想知道,变量"btc_acc_adress"是一个字符串(带有BTC地址),则amountBTC是双倍的,当然还有交易金额.


Sea*_*wen 5

zxing不(仅)提供Web API; 真的,那就是Google提供API,源自后来在项目中开源的源代码.

正如Rob在这里所说,您可以使用QR源代码Java源代码创建原始条形码,然后将其渲染为位图.

我可以提供一种更简单的方式.您可以通过Intent调用Barcode Scanner来编码条形码.您只需要几行代码,以及项目中的两个类android-integration.主要是IntentIntegrator.打电话吧shareText().