Creation of Android QR code

Source: Internet
Author: User
Tags vcard

Because of the introduction of the QR code into our lives, and more and more people are playing their own imagination to use it to facilitate our lives, I have heard a joke, when we die, Tombstone no longer have an epitaph, but will appear a record of your life information of the QR code, when people come to your tombstone, You can see your life's exploits with a sweep of your mobile phone. This is not very interesting, I think this will become a reality in the near future, Haha, the joke is over, let's learn how to get the QR code to serve us in Android development.

This article I will lead friends to achieve a record of personal basic information of the two-dimensional code design ideas, for the algorithm of the Daniel, here to let you down, for the two-dimensional code generation algorithm, I Caishuxueqian can not be shared, The implementation of this case we will use Core.jar implementation, for the download of this jar package, I provide a link for everyone to learn from: http://pan.baidu.com/s/1bnGZoF9

With our jar pack ready, we start today's design, the first step: Create the project, import the jar package

Create an Android project in our integrated development environment to pave the design for today's case. After creating the project, import the jar package we just downloaded into our project, CTRL + C our Jar package, find the Libs folder in our Engineering directory CTRL + V, and then what? is to import our jar packages into the project through an integrated development environment.

  

Step Two: Create our layout file:

<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:paddingbottom= "@dimen/activity_vertical_margin"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"Tools:context=". Mainactivity "> <LinearLayout android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"android:orientation= "Vertical"Android:id= "@+id/ll" > <!--used to show the QR code we created--<ImageView Android:id= "@+id/imgcode"Android:layout_width= "100dip"Android:layout_height= "100dip"android:layout_gravity= "Center_horizontal"/> <!--company--<LinearLayout android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"android:orientation= "Horizontal" > <TextView android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Company"/> <EditText Android:id= "@+id/etcompany"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:hint= "Optional"/> </LinearLayout> <!--phone---<LinearLayout android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"android:orientation= "Horizontal" > <TextView android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Phone"/> <EditText Android:id= "@+id/etphone"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:hint= "Optional"/> </LinearLayout> <!--mailbox--<LinearLayout android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"android:orientation= "Horizontal" > <TextView android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Mailbox"/> <EditText Android:id= "@+id/etemail"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:hint= "Optional"/> </LinearLayout> <!--URL--<LinearLayout android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"android:orientation= "Horizontal" > <TextView android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "url"/> <EditText Android:id= "@+id/etweb"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "/http"/> </LinearLayout> <button android:id= "@+id/but"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "Generate two-dimensional code"/> </LinearLayout></RelativeLayout>

Step three: Edit our activity:

 Public classMainactivityextendsActivity {PrivateEditText Etcompany; PrivateEditText Etphone; PrivateEditText Etemail; PrivateEditText Etweb; PrivateBitmap logo; Private Static Final intImage_halfwidth = 40;//width value, which affects the middle picture size@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); //Get a picture of a resource and change it to get a local picture or take a photo to get a pictureLogo=bitmapfactory.decoderesource (Super. Getresources (), r.drawable.ic_launcher); Etcompany=(EditText) Findviewbyid (R.id.etcompany); Etphone=(EditText) Findviewbyid (R.id.etphone); Etemail=(EditText) Findviewbyid (R.id.etemail); Etweb=(EditText) Findviewbyid (R.id.etweb); Findviewbyid (r.id.but). Setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {String Company=Etcompany.gettext (). toString (). Trim (); String Phone=Etphone. GetText (). toString (). Trim (); String Email=Etemail.gettext (). toString (). Trim (); String Web=Etweb.gettext (). toString (). Trim (); //text information contained in a QR codeString contents= "begin:vcard\nversion:3.0\norg:" +company+ "\ntel:" +phone+ "\nurl:" +web+ "\nemail:" +email+ "\nEND: VCARD "; Try {                //Call method Createcode generate two-dimensional codeBitmap bm=Createcode (contents, logo, Barcodeformat.qr_code); ImageView img=(ImageView) Findviewbyid (R.id.imgcode); //Display the QR code in the interfaceImg.setimagebitmap (BM); } Catch(writerexception e) {e.printstacktrace ();            }                             }        }); }    /*** Generate QR Code *@paramtext information contained in a string QR code *@paramMbitmap logo image *@paramFormat Encoding *@returnBitmap Bitmap *@throwswriterexception*/     PublicBitmap Createcode (String string,bitmap mbitmap, barcodeformat format)throwswriterexception {Matrix m=NewMatrix (); floatSX = (float) 2 * Image_halfwidth/mbitmap.getwidth (); floatSY = (float) 2 *Image_halfwidth/mbitmap.getheight (); M.setscale (SX, SY);//Set Scaling information//Zoom the logo image to the information set by MartixMbitmap = Bitmap.createbitmap (mbitmap, 0, 0, Mbitmap.getwidth (), Mbitmap.getheight (), M,false); Multiformatwriter writer=NewMultiformatwriter ();//Hashtable<encodehinttype, string> HST =NewHashtable<encodehinttype, string>(); Hst.put (Encodehinttype.character_set,"UTF-8");//Set character encodingBitmatrix matrix = Writer.encode (string, format, n, I, HST);//generate two-dimensional code matrix information        intwidth = Matrix.getwidth ();//Matrix Height        intHeight = matrix.getheight ();//Matrix Width        intHALFW = WIDTH/2; intHALFH = HEIGHT/2; int[] pixels =New int[Width * height];//defines the array length as the matrix height * Matrix width for recording pixel information in the matrix         for(inty = 0; Y < height; y++) {//start Iteration matrix from line             for(intx = 0; x < width; X + +) {//Iterate Columns                if(x > Halfw-image_halfwidth && x < HALFW +Image_halfwidth&& y > HALFH-Image_halfwidth&& y < HALFH + image_halfwidth) {//secondary location for picture informationPixels[y * width + x] = Mbitmap.getpixel (X-HALFW+ image_halfwidth, Y-HALFH + image_halfwidth);//record pictures per pixel information}Else {                    if(Matrix.get (x, y)) {//If there is a black dot, record the informationPixels[y * width + x] = 0xff000000;//Log Black block information}}}} Bitmap Bitmap=bitmap.createbitmap (width, height, Bitmap.Config.ARGB_8888); //generating bitmap through the array of pixelsBitmap.setpixels (pixels, 0, width, 0, 0, width, height); returnbitmap; }    }

The above code comment has been very detailed, here I say a few words, this part of the code is divided into two parts, the upper part is we often use, the next part is our two-dimensional code to create the focus. OK here we have the effect is realized, the last one:

  

Creation of Android QR code

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.