Generation and Analysis of QR codes for Android

Source: Internet
Author: User

Go directly to the Code. The Code has specific resolutions on it and provides jar for download: character code jarpack.rar

Generate the corresponding QR code based on the text:

// Generate the QR map private void createimage () {try {// you need to introduce the core package qrcodewriter writer = new qrcodewriter (); string text = qr_text.gettext (). tostring (); log. I (TAG, "generated text:" + text); If (text = NULL | "". equals (text) | text. length () <1) {return ;}// convert the input text into a two-dimensional code bitmatrix martix = writer. encode (text, barcodeformat. qr_code, qr_width, qr_height); system. out. println ("W:" + martix. getwidth () + "H:" + martix. getheight (); hashtable <encodehinttype, string> hints = new hashtable <encodehinttype, string> (); hints. put (encodehinttype. character_set, "UTF-8"); bitmatrix = new qrcodewriter (). encode (text, barcodeformat. qr_code, qr_width, qr_height, hints); 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 = bitmap. createbitmap (qr_width, qr_height, bitmap. config. argb_8888); bitmap. setpixels (pixels, 0, qr_width, 0, 0, qr_width, qr_height); qr_image.setimagebitmap (Bitmap);} catch (writerexception e) {e. printstacktrace ();}}


Read content based on the QR code:

// Parse the QR image private void scanningimage () {Map <decodehinttype, string> hints = new hashmap <decodehinttype, string> (); hints. put (decodehinttype. character_set, "UTF-8"); // obtain the image to be parsed. Bitmap bitmap = (bitmapdrawable) qr_image.getdrawable ()). getbitmap (); rgbluminancesource source = new rgbluminancesource (Bitmap); binarybitmap bitmap1 = new binarybitmap (new region (source); qrcodereader reader = new qrcodereader (); result; try {result = reader. decode (bitmap1, hints); // obtain the parsed text qr_result.settext (result. gettext ();} catch (notfoundexception e) {e. printstacktrace ();} catch (checksumexception e) {e. printstacktrace ();} catch (formatexception e) {e. printstacktrace ();}}

 

Related Article

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.