Android Application Development: generate a QR code

Source: Internet
Author: User

Android Application Development: generate a QR code

The QR code has been popular since it came out. wherever it goes, the QR code can appear in front of you. Currently, smart phones basically have the scan function, scanning the QR code. How can we generate a QR code on our mobile phone? It is not difficult to generate a QR code in Android development. It is very easy to generate a QR code using Google ZXing. Below we will continue with simple operations and post the most important code. Please give me more advice!

Step 1:

Determine the location of our QR code, that is, the location of the page on which we apply the QR code. Simply put, we use an ImageView to display the QR code, we will display the QR code on the ImageView in activity_qr_layout:

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">

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

Next, you can directly generate a QR code 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 the width.
ViewGroup. LayoutParams layoutParams = qr_show.getLayoutParams ();
LayoutParams. height = layoutParams. width = w; // set the 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 am a web site
Qr_show.setImageBitmap (bitmap );
}
Catch (Exception e ){
E. printStackTrace ();
Toast. makeText (this, "failed to generate the QR code", Toast. LENGTH_SHORT );
}
}
}

The following is a tool class for generating QR codes. Do not forget to import ZXing jar packages in the project:

Public class QRUtils {
Private static final int WHITE = 0 xFFFFFFFF;
Private static final int BLACK = 0xFF000000;
/**
* Generate a QR code Image Based on the specified size of the string.
*/
Public static Bitmap encodeToQR (String contentsToEncode, int dimension) throws Exception {
If (TextUtils. isEmpty (contentsToEncode ))
Return null;

BarcodeFormat format = BarcodeFormat. QR_CODE;
Map Hints = new EnumMap (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 Int offset = y * width;
For (int x = 0; x <width; x ++ ){
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 Hints = new EnumMap (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 Int offset = y * width;
For (int x = 0; x <width; x ++ ){
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. Just scan it!

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.