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

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

我需要的图书馆必须:

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

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

你看过ZXING吗? 我已经成功地使用它来创build条码。 您可以在比特币应用程序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 } 

与zxing这是我创buildQR的代码

  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(); } 

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

QRGen

在android中使用它的例子

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

这是我生成一个位图的简单和工作的function! 我只使用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; } 

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

1)findzxing-1.3.jar,下载并添加属性(添加外部jar)。

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

3)在我的活动中包含代码以创buildqr图像(在本例中,我正在为比特币付款创buildQR):

  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); } 

如果有人想知道,variables“btc_acc_adress”是一个string(与BTC地址),amountBTC是一个双,当然,交易金额。

zxing不(仅)提供一个web API; 实际上,就是Google提供的API,这个源代码稍后在项目中是开源的。

正如Rob在这里所说的,您可以使用QR码编码器的Java源代码创build一个原始条码,然后将其渲染为位图。

我仍然可以提供一个更简单的方法。 您可以通过Intent调用条码扫描器来对条码进行编码。 在android-integration下,只需要几行代码和项目中的两个类。 主要的是IntentIntegrator 。 只需调用shareText()