The latest in learning adnroid, encountered two-dimensional code generation and scanning here to do a collation
The tool class of the QR code generation
/**
* QR code generation tool Class
*/
public class Qrcodeutil {
/**
* Generate QR Code address or string to be converted, can be in Chinese
* @param URL
* @param width
* @param height
* @return
*/
public static Bitmap createqrimage (String URL, final int width, final int height) {
try {
Determine URL legitimacy
if (url = = NULL | | ". Equals (URL) | | Url.length () < 1) {
return null;
}
Hashtable<encodehinttype, string> hints = new Hashtable<encodehinttype, string> ();
Hints.put (Encodehinttype.character_set, "utf-8");
Conversion of image data using matrix conversion
Bitmatrix Bitmatrix = new Qrcodewriter (). Encode (URL,
Barcodeformat.qr_code, width, height, hints);
int[] pixels = new int[width * height];
Here according to the two-dimensional code algorithm, one by one to generate two-dimensional code of the picture,
Two for loop is the result of the picture Heng Lie scan
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; + +) {
if (Bitmatrix.get (x, y)) {
Pixels[y * width + x] = 0xff000000;
} else {
Pixels[y * width + x] = 0xFFFFFFFF;
}
}
}
Create a QR code image format, using argb_8888
Bitmap Bitmap = Bitmap.createbitmap (width, height,config.argb_8888);
Bitmap.setpixels (pixels, 0, width, 0, 0, width, height);
return bitmap;
} catch (Writerexception e) {
E.printstacktrace ();
}
return null;
}
/**
* Barcode Generation
*
* @param context
* @param contents
* Content that needs to be generated
* @param desiredwidth
* Broadband for barcode generation
* @param desiredheight
* The height of the barcode is generated
* @param displaycode
* Whether the content is displayed below the barcode
* @return
*/
public static Bitmap Creatbarcode (context context, String contents,int desiredwidth, int desiredheight, Boolean Displayco DE) {
Bitmap ruseltbitmap = null;
/**
* Blank width reserved at both ends of the picture
*/
int marginw = 20;
/**
* Barcode Encoding Type
*/
Barcodeformat Barcodeformat = barcodeformat.code_128;
if (Displaycode) {
Bitmap Barcodebitmap = encodeasbitmap (contents, barcodeformat,desiredwidth, desiredheight);
Bitmap Codebitmap = creatcodebitmap (contents, Desiredwidth + 2 * marginw, desiredheight, context);
Ruseltbitmap = Mixturebitmap (Barcodebitmap, Codebitmap, New PointF (0, desiredheight));
}else{
Ruseltbitmap = Encodeasbitmap (contents, barcodeformat,desiredwidth, desiredheight);
}
return ruseltbitmap;
}
/**
* Bitmap to generate barcodes
*
* @param contents
* Content that needs to be generated
* @param format
* Encoding Format
* @param desiredwidth
* @param desiredheight
* @return
* @throws writerexception
*/
protected static Bitmap Encodeasbitmap (String contents,barcodeformat format, int desiredwidth, int desiredheight) {
Final int white = 0xFFFFFFFF;
Final int BLACK = 0xff000000;
Multiformatwriter writer = new Multiformatwriter ();
Bitmatrix result = null;
try {
result = Writer.encode (contents, format, Desiredwidth,
Desiredheight, NULL);
} catch (Writerexception e) {
E.printstacktrace ();
}
int width = result.getwidth ();
int height = result.getheight ();
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
int offset = y * width;
for (int x = 0; x < width; + +) {
Pixels[offset + x] = Result.get (x, y)? Black:white;
}
}
Bitmap Bitmap = Bitmap.createbitmap (width, height,config.argb_8888);
Bitmap.setpixels (pixels, 0, width, 0, 0, width, height);
return bitmap;
}
/**
* Generate display encoding for bitmap
* @param contents
* @param width
* @param height
* @param context
* @return
*/
protected static Bitmap Creatcodebitmap (String contents, int width,int height, context context) {
TextView TV = new TextView (context);
Layoutparams layoutparams = new Layoutparams (layoutparams.match_parent, layoutparams.wrap_content);
Tv.setlayoutparams (Layoutparams);
Tv.settext (contents);
Tv.setheight (height);
Tv.setgravity (gravity.center_horizontal);
Tv.setwidth (width);
Tv.setdrawingcacheenabled (TRUE);
Tv.settextcolor (Color.Black);
Tv.measure (Measurespec.makemeasurespec (0, measurespec.unspecified),
Measurespec.makemeasurespec (0, measurespec.unspecified));
Tv.layout (0, 0, tv.getmeasuredwidth (), Tv.getmeasuredheight ());
Tv.builddrawingcache ();
Bitmap Bitmapcode = Tv.getdrawingcache ();
return bitmapcode;
}
/**
* Merge two bitmap into one
*
* @param First
* @param second
* @param frompoint
* The start position of the second bitmap start drawing (relative to the first bitmap)
* @return
*/
protected static Bitmap Mixturebitmap (Bitmap First, Bitmap second,pointf frompoint) {
if (first = = NULL | | second = = NULL | | frompoint = = NULL) {
return null;
}
int marginw = 20;
Bitmap Newbitmap = Bitmap.createbitmap (first.getwidth () + second.getwidth () + marginw,first.getheight () + Second.getheight (), config.argb_4444);
Canvas CV = new canvas (NEWBITMAP);
Cv.drawbitmap (First, MARGINW, 0, NULL);
Cv.drawbitmap (second, frompoint.x, frompoint.y, NULL);
Cv.save (Canvas.all_save_flag);
Cv.restore ();
return newbitmap;
}
}
You can use it directly in the activity.
First parameter to generate a string of two-dimensional code
Bitmap Qrbitmap = qrcodeutil.createqrimage ("Revolve", 400, 400);
IV indicates the ID of ImageView in XML
Iv.setimagebitmap (qrbitmap);
Android QR code generation