QR Code This thing has been popular since it came out now, wherever you go, the QR code may appear in front of you. Now the smartphone basically has sweep sweep function, sweep is the QR code. So how do we generate a QR code on the phone? Android development in the generation of two-dimensional code is not difficult, with Google Zxing generated QR code is very simple, the following we have a simple operation has been, the most important code to paste out, but also please give us more advice!
The first step:
Determine the location of our QR code, that is, which page in our application where, simply speaking is to use a imageview to display the QR code, we will show the QR code in the following layout activity_qr_layout ImageView:
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:sample= "Http://schemas.android.com/apk/res-auto"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
Android:background= "#dadada"
Android:focusable= "true"
Android:focusableintouchmode= "true"
android:orientation= "Vertical" >
<imageview
Android:id= "@+id/qr_show"
Android:layout_gravity= "Center_horizontal"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:layout_marginbottom= "55DP"
Android:scaletype= "Fitxy"
Android:background= "@null"
android:src= "@drawable/default_pic"
/>
</LinearLayout>
The next step is to generate the QR code directly in the activity:
public class Myqractivity extends Activity {
Private ImageView qr_show;
Private Samplelistlinearlayout Lv_record;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (r.layout.activity_qr_layout);
initviews ();
}
protected void Initviews () {
Qr_show = (ImageView) Findviewbyid (r.id.qr_show);
Displaymetrics outmetrics = new Displaymetrics ();
Getwindowmanager (). Getdefaultdisplay (). Getmetrics (Outmetrics);
int w = outmetrics.widthpixels * 8/11;//Set width
Viewgroup.layoutparams layoutparams = Qr_show.getlayoutparams ();
Layoutparams.height = Layoutparams.width = w;//Set height
Qr_show.setlayoutparams (Layoutparams);
try {
Bitmap Bitmap = qrutils.encodetoqrwidth ("http://write.blog.csdn.net/", w);//To generate the content of the QR code, I'm just a URL.
Qr_show.setimagebitmap (bitmap);
}
catch (Exception e) {
E.printstacktrace ();
Toast.maketext (This, "Generate QR code failed", Toast.length_short);
}
}
}
Here is the tool class that generates the QR code, and don't forget to import the zxing related jar package in your project:
public class Qrutils {
private static final int white = 0xFFFFFFFF;
private static final int BLACK = 0xff000000;
/**
* Generate a two-dimensional code image with a specified size
*/
public static Bitmap ENCODETOQR (String contentstoencode, int dimension) throws exception{
if (Textutils.isempty (Contentstoencode))
return null;
Barcodeformat format = Barcodeformat.qr_code;
map<encodehinttype,object> hints = new enummap<encodehinttype,object> (encodehinttype.class);
Hints.put (Encodehinttype.character_set, "UTF-8");
Bitmatrix result = new Multiformatwriter (). Encode (contentstoencode, format, dimension, dimension, hints);
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, Bitmap.Config.ARGB_8888);
Bitmap.setpixels (pixels, 0, width, 0, 0, width, height);
return bitmap;
}
public static Bitmap Encodetoqrwidth (String contentstoencode, int dimension) throws exception{
if (Textutils.isempty (Contentstoencode))
return null;
Barcodeformat format = Barcodeformat.qr_code;
map<encodehinttype,object> hints = new enummap<encodehinttype,object> (encodehinttype.class);
Hints.put (Encodehinttype.character_set, "UTF-8");
Bitmatrix result = new Multiformatwriter (). Encode (contentstoencode, format, dimension, dimension, hints);
int width = result.getwidth ();
int height = result.getheight ();
Boolean isfirstblack = true;
int StartX = 0;
int starty = 0;
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;
if (Result.get (x, y) && isfirstblack) {
Isfirstblack = false;
StartX = x;
Starty = y;
}
}
}
Bitmap Bitmap = bitmap.createbitmap (width, height, Bitmap.Config.ARGB_8888);
Bitmap.setpixels (pixels, 0, width, 0, 0, width, height);
Matrix M = new Matrix ();
float SX = (width + 2f*startx)/width;
Float sy = (height + 2f*starty)/height;
M.postscale (SX, SY);
Bitmap Qrbitmap = bitmap.createbitmap (width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new canvas (QRBITMAP);
Canvas.translate (-startx,-starty);
Paint paint = new paint ();
Paint.setantialias (TRUE);
Canvas.drawbitmap (Bitmap, M, paint);
Canvas.save ();
return qrbitmap;
}
}
OK, take a sweep on it!
Android app development: generate QR Code