This example uses text to generate the corresponding QR code image and parse the content of the QR code image. The Code is as follows:
MainActivity:
Package com. home. testqrcode; import java. util. hashMap; import java. util. hashtable; import java. util. map; import android. app. activity; import android. graphics. bitmap; import android. graphics. drawable. bitmapDrawable; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. editText; import android. widget. imageView; import and Roid. widget. textView; import android. widget. toast; import com. google. zxing. barcodeFormat; import com. google. zxing. binaryBitmap; import com. google. zxing. decodeHintType; import com. google. zxing. encodeHintType; import com. google. zxing. result; import com. google. zxing. writerException; import com. google. zxing. client. androidtest. RGBLuminanceSource; import com. google. zxing. common. bitMatrix; import com. google. zxing. Common. hybridBinarizer; import com. google. zxing. qrcode. QRCodeReader; import com. google. zxing. qrcode. QRCodeWriter; public class MainActivity extends Activity implements OnClickListener {private Button generateBtn; private Button decodeQRCodeBtn; private ImageView imageView; private TextView textView; private EditText contentText; private static int QR_WIDTH = 400; private static int QR_HEIGHT = 400; @ Overrid Eprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); initWidget ();} private void initWidget () {generateBtn = (Button) findViewById (R. id. main_btn_generateQRCode); generateBtn. setOnClickListener (this); decodeQRCodeBtn = (Button) findViewById (R. id. main_btn_decodeQRCode); decodeQRCodeBtn. setOnClickListener (this); imageView = (ImageView) findV IewById (R. id. main_iv); textView = (TextView) findViewById (R. id. main_ TV); contentText = (EditText) findViewById (R. id. main_et) ;}@ Overridepublic void onClick (View v) {if (v = generateBtn) {String text = contentText. getText (). toString (); if ("". equals (text) | null = text) {Toast. makeText (this, "Enter the content", Toast. LENGTH_SHORT ). show (); return;} Bitmap bitmap = createBitmap (text); if (bitmap! = Null) {imageView. setImageBitmap (bitmap) ;}} else if (v = decodeQRCodeBtn) {String content = readImage (imageView); textView. setText (content) ;}}/*** generate a QR code image ** @ return */private Bitmap createBitmap (String text) {Bitmap bitmap = null; try {Hashtable
Hints = new Hashtable
(); Hints. put (EncodeHintType. CHARACTER_SET, "UTF-8"); BitMatrix bitMatrix = new QRCodeWriter (). encode (text, BarcodeFormat. QR_CODE, QR_WIDTH, QR_HEIGHT, hints); // QRCodeWriter writer = new QRCodeWriter (); // convert the input text into a QR code // BitMatrix bitMatrix = writer. encode (text, BarcodeFormat. QR_CODE, // QR_WIDTH, QR_HEIGHT); int [] pixels = new int [QR_WIDTH * QR_HEIGHT]; for (int y = 0; y <QR_HEIGHT; y ++) {for (int x = 0; x <QR_WIDTH; x ++) {if (bitMatrix. get (x, y) {pixels [y * QR_WIDTH + x] = 0xff000000;} else {pixels [y * QR_WIDTH + x] = 0 xffffffff ;}}} bitmap = Bitmap. createBitmap (QR_WIDTH, QR_HEIGHT, Bitmap. config. ARGB_8888); bitmap. setPixels (pixels, 0, QR_WIDTH, 0, 0, QR_WIDTH, QR_HEIGHT);} catch (WriterException e) {e. printStackTrace ();} return bitmap;}/*** parse the QR image content ** @ param imageView * @ return */private String readImage (ImageView imageView) {String content = null; map
Hints = new HashMap
(); Hints. put (DecodeHintType. CHARACTER_SET, "UTF-8"); // obtain the image to be parsed Bitmap bitmap = (BitmapDrawable) imageView. getDrawable ()). getBitmap (); RGBLuminanceSource source = new RGBLuminanceSource (bitmap); BinaryBitmap bitmap1 = new BinaryBitmap (new region (source); QRCodeReader reader = new QRCodeReader (); try {Result = reader. decode (bitmap1, hints); // obtain the parsed text content = result. getText ();} catch (Exception e) {e. printStackTrace () ;}return content ;}}
RGBLuminanceSource:
package com.google.zxing.client.androidtest;import com.google.zxing.LuminanceSource;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import java.io.FileNotFoundException;/** * This class is used to help decode images from files which arrive as RGB data * from Android bitmaps. It does not support cropping or rotation. * * @author dswitkin@google.com (Daniel Switkin) */public final class RGBLuminanceSource extends LuminanceSource {private final byte[] luminances;public RGBLuminanceSource(String path) throws FileNotFoundException {this(loadBitmap(path));}public RGBLuminanceSource(Bitmap bitmap) {super(bitmap.getWidth(), bitmap.getHeight());int width = bitmap.getWidth();int height = bitmap.getHeight();int[] pixels = new int[width * height];bitmap.getPixels(pixels, 0, width, 0, 0, width, height);// In order to measure pure decoding speed, we convert the entire image// to a greyscale array// up front, which is the same as the Y channel of the// YUVLuminanceSource in the real app.luminances = new byte[width * height];for (int y = 0; y < height; y++) {int offset = y * width;for (int x = 0; x < width; x++) {int pixel = pixels[offset + x];int r = (pixel >> 16) & 0xff;int g = (pixel >> 8) & 0xff;int b = pixel & 0xff;if (r == g && g == b) {// Image is already greyscale, so pick any channel.luminances[offset + x] = (byte) r;} else {// Calculate luminance cheaply, favoring green.luminances[offset + x] = (byte) ((r + g + g + b) >> 2);}}}}@Overridepublic byte[] getRow(int y, byte[] row) {if (y < 0 || y >= getHeight()) {throw new IllegalArgumentException("Requested row is outside the image: " + y);}int width = getWidth();if (row == null || row.length < width) {row = new byte[width];}System.arraycopy(luminances, y * width, row, 0, width);return row;}// Since this class does not support cropping, the underlying byte array// already contains// exactly what the caller is asking for, so give it to them without a copy.@Overridepublic byte[] getMatrix() {return luminances;}private static Bitmap loadBitmap(String path) throws FileNotFoundException {Bitmap bitmap = BitmapFactory.decodeFile(path);if (bitmap == null) {throw new FileNotFoundException("Couldn't open " + path);}return bitmap;}}
Main. xml:
Download a QR code jar: core. jar from the Internet.