First, add the Zxing Library reference to the Grandle.
Compile ' com.google.zxing:core:3.1.0 '
Generate two-dimensional code static class, return a two-dimensional code bitmap
public class Qrcodeutil {//Generate QR Chart public static Bitmap getqrcodeimage (int qr_width, int qr_height, String text) thr oWS Writerexception {try {//need to introduce core package qrcodewriter writer = new Qrcodewriter (); if (text = = NULL | | "". Equals (text) | | Text.length () < 1) {return null; } hashtable<encodehinttype, object> hints = new Hashtable<encodehinttype, object> (); Hints.put (Encodehinttype.character_set, "utf-8"); Hints.put (encodehinttype.margin, 0); Bitmatrix Bitmatrix = new Qrcodewriter (). Encode (text, Barcodeformat.qr_code, qr_width, qr_height, hints); int[] pixels = new int[qr_width * Qr_height]; for (int y = 0, y < qr_height; y++) {for (int x = 0; x < qr_width; + +) {if (bit Matrix.get (x, y)) {Pixels[y * qr_width + x] = 0xff000000; } else { Pixels[y * qr_width + x] = 0X00FFFF; }}} Bitmap Bitmap = Bitmap.createbitmap (Qr_width, Qr_height, Bit Map. config.argb_8888); Bitmap.setpixels (pixels, 0, qr_width, 0, 0, qr_width, qr_height); return bitmap; } catch (Writerexception e) {e.printstacktrace (); return null; } }}
Call Bitmap bmp = Qrcodeutil.getqrcodeimage ($, $, "www.baidu.com");
Android-Using zxing to generate two-dimensional code