Android uses zxing to generate two-dimensional code with images

Source: Internet
Author: User

As follows:

The production process is very simple is to add a logn icon in the original two-dimensional code image, the code of the comments written in detail, it will not give you a long-winded

Package Com.example.day44_02_qrcodewithlogin;import Java.util.hashmap;import Java.util.map;import Android.app.activity;import Android.graphics.bitmap;import Android.graphics.bitmap.config;import Android.graphics.bitmapfactory;import Android.graphics.canvas;import Android.os.bundle;import Android.view.Menu; Import Android.widget.imageview;import Com.google.zxing.barcodeformat;import Com.google.zxing.EncodeHintType; Import Com.google.zxing.writerexception;import Com.google.zxing.common.bitmatrix;import Com.google.zxing.qrcode.qrcodewriter;public class Mainactivity extends Activity {private ImageView imageview_main; @Ov errideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); Initview ();} private void Initview () {Imageview_main = (ImageView) Findviewbyid (R.id.imageview_main);    String content = "We are graduating"; Createqrcode (content,500,500);} /** * Used to create a QR code * @param content * @param width * @param height */private void crEateqrcode (String content, int width, int height) {try {//1, creating instantiated object Qrcodewriter writer = new Qrcodewriter ();//2, setting character set map& Lt Encodehinttype, string> map = new Hashmap<encodehinttype, string> (); Map.put (Encodehinttype.character_set, "    UTF-8 ");//3, the content is written to the matrix object by encode method Bitmatrix matrix = writer.encode (content, Barcodeformat.qr_code, width, height,map);    4, define an array of pixel points of a two-dimensional code, fill the color of each pixel int[] pixels = new Int[width*height]; 5, fill in the color of each pixel (pixel no data is filled with black, no color fill, but generally with white) for (int i = 0; i < height; i++) {for (int j = 0; J < width; j + +) {if ( Matrix.get (J, i)) {pixels[i*width+j] = 0xff000000;} else {pixels[i*width+j] = 0xFFFFFFFF;}}}    6, create a blank Bitmap object that specifies the height and width Bitmap bm_qr = bitmap.createbitmap (width, height, config.argb_8888); 7, fill the color of each pixel into the bitmap object bm_qr.setpixels (pixels, 0, width, 0, 0, width, height);//8, create a bitmap object to use as its icon bitmap bm_login = Bitmapfactory.decoderesource (Getresources (), r.drawable.img_kf_qq);//9, create a method to add a picture to the QR code if (Addlogin (bm_qr,bm_ LOGIN) = null) {Imageview_maIn.setimagebitmap (Addlogin (Bm_qr,bm_login));}} catch (Writerexception e) {//TODO auto-generated catch Blocke.printstacktrace ();}} /** * Used to add a logn to the created QR code * @param BM_QR * @param bm_login * @return */private Bitmap addlogin (Bitmap bm_qr, Bitmap Bm_logi N) {if (BM_QR = = null) {return null;} if (Bm_login = = null) {return BM_QR;} Gets the height of the picture int bm_qr_width = Bm_qr.getwidth (); int bm_qr_height = Bm_qr.getheight (); int bm_login_width = Bm_login.getwidth (); int bm_login_height = Bm_login.getheight ();//Set Logn the size of the QR code for the overall size of 1/5float Scale_login = Bm_qr_width*1.0f/5/bm_login _width;     Bitmap Bitmap = Bitmap.createbitmap (Bm_qr_width, Bm_qr_height, Bitmap.Config.ARGB_8888);            try {Canvas canvas = new canvas (bitmap);            Canvas.drawbitmap (BM_QR, 0, 0, NULL);            Canvas.scale (Scale_login, Scale_login, BM_QR_WIDTH/2, BM_QR_HEIGHT/2);             Canvas.drawbitmap (Bm_login, (bm_qr_width-bm_login_width)/2, (Bm_qr_height-bm_login_height)/2, NULL); Canvas.save (CaNvas.            All_save_flag);        Canvas.restore ();            } catch (Exception e) {bitmap = null;        E.getstacktrace ();  } return bitmap; @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {getmenuinflater (). Inflate (R.menu.main, menu); return true;}}

  

Android uses zxing to generate two-dimensional code with images

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.