Android QR code generation

Source: Internet
Author: User

1,activity

public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_erweima);

Initviews ();
}

private void Initviews () {
TODO auto-generated Method Stub
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);
}
}

2,activity_erweima.xml file

<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:orientation= "Vertical"
Android:focusable= "true"
Android:focusableintouchmode= "true"
>


<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/ic_launcher" ></ImageView>

</LinearLayout>

3, generating the class

Package cn.test.tools;

Import Java.util.EnumMap;
Import Java.util.Map;


Import Com.google.zxing.BarcodeFormat;
Import Com.google.zxing.EncodeHintType;
Import Com.google.zxing.MultiFormatWriter;
Import Com.google.zxing.common.BitMatrix;

Import Android.graphics.Bitmap;
Import Android.graphics.Canvas;
Import Android.graphics.Matrix;
Import Android.graphics.Paint;
Import Android.text.TextUtils;

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

4, Import jar Package

In the file of this blog

Reference: http://www.open-open.com/code/view/1426334688154

Android QR code generation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.