How to generate a QR code for an Android application?

I need to create qrcode in my Android application, and I need a library or source code that allows me to create a QR code in an Android application.

I need a library:

  • do not leave a watermark (e.g. onbarcode library)
  • Do not use the web service API to create qrcode (e.g. Google zxing library)
  • No third-party installers needed (e.g. QR Droid)

I have already created such code for the iPhone (Objective-C), but I need a quick fix for Android, until I have time to create my own QR code generator. This is my first Android project, so any help would be appreciated.

+49
java android qr-code
Jan 10 '12 at 9:23
source share
6 answers

Have you looked at ZXING ? I have successfully used it to create barcodes. You can see the full working example in the bitcoin src application

 // 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 } 
+46
Jan 10 2018-12-12T00:
source share

with zxing this is my QR code

  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(); } 
+51
Aug 13 '14 at 9:53 on
source share

This may be an old topic, but I found this library very useful and easy to use.

QRGen

example for use in android

  Bitmap myBitmap = QRCode.from("www.example.org").bitmap(); ImageView myImage = (ImageView) findViewById(R.id.imageView); myImage.setImageBitmap(myBitmap); 
+12
Nov 16 '15 at 3:01
source share

Here is my simple and working function for creating Bitmap! I use only ZXing1.3.jar! I also set the correction level higher!

PS: x and y are inverted, this is normal, because the Matrix bit is inverse of x and y. This code works fine with a square image.

 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; } 
+11
May 14 '15 at 19:22
source share

I used the zxing-1.3 jar, and I had to make some changes to the code implementation from other answers, so I will leave my solution to others. I have done the following:

1) find zxing-1.3.jar, download it and add properties (add external jar).

2) in the activity layout add an ImageView and name it (in my example it is tnsd_iv_qr).

3) include the code in my activity to create a qr image (in this example, I created a QR for bitcoin payments):

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

If someone wonders, the variable "btc_acc_adress" is a string (with a BTC address), amountBTC is double, and, of course, the transaction amount.

+6
Dec 01
source share

zxing does not provide (only) a web API; indeed, this is Google providing an API from the source code that was later opened in the project.

As Rob says, here you can use the Java source code for the QR code encoder to create a raw barcode and then make it like Raster.

I can offer an even simpler way. You can call the barcode scanner by Intent to encode the barcode. You only need a few lines of code and two classes from the project under android-integration . Primary IntentIntegrator . Just call shareText() .

+5
Jan 10 2018-12-12T00:
source share



All Articles