QR Code (Android)

Source: Internet
Author: User

We all know that the average two microcode is displayed in a variety of forms, some login, some pages, in fact, the QR code is just a string. Here we have a question that the QR code is so judged to be a website or login function?

In fact, these parts of the judgment, the QR code generator has already been completed for you, we just import a Core-3.2.1.jar package on the line. Then let's do a simple little case to learn more about the QR code.

First, the new project

1, we build a project called "Weima".

2, in the Project Libs import a Core-3.2.1.jar package (Note:: Http://pan.baidu.com/s/1i5FiYXZ), you can also search in Baidu.

3, we have found a common two-dimensional code in the middle of a small picture, so we first download a picture into the res drawable-hdpi place.

Second, the layout file

1, first we add a edittext input box and a button, a ImageView control to display the two-dimensional code icon.

2. When we enter the number or URL in the input box, when we click the button, a QR code will be generated in the ImageView control.

3, when we add edittext, may be due to the version issue will appear unable to click the interface, the following issues.

4, EditText due to the version of the issue so to adjust the version to 19, the revision of the upper right corner of the Android robot icon.

Activity_main.xml file:

<Relativelayoutxmlns: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= "Com.example.weima.MainActivity" >     <EditTextAndroid:id= "@+id/edittext1"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_alignparentleft= "true"Android:layout_alignparenttop= "true"Android:layout_marginleft= "38DP"Android:layout_margintop= "33DP"Android:ems= "Ten" >        <Requestfocus/>    </EditText>       <ButtonAndroid:id= "@+id/button1"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_alignright= "@+id/edittext1"Android:layout_below= "@+id/edittext1"Android:layout_marginright= "20DP"Android:layout_margintop= "22DP"Android:text= "button" />    <ImageViewAndroid:id= "@+id/imageview1"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_alignleft= "@+id/edittext1"Android:layout_below= "@+id/button1"Android:layout_marginleft= "14DP"Android:layout_margintop= "47DP" />    </Relativelayout>

Third, the Mainactivity code

Mainactivity.java file:

 Public classMainactivityextendsActivity {PrivateEditText ent; PrivateButton btn; PrivateImageView img; Private Static Final intImage_halfwidth = 35;//Image Width Value size@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);                Setcontentview (R.layout.activity_main); ENT=(EditText) Findviewbyid (R.ID.EDITTEXT1); IMG=(ImageView) Findviewbyid (R.ID.IMAGEVIEW1); BTN=(Button) Findviewbyid (R.id.button1); Btn.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {//TODO auto-generated Method Stub//Remove StringString enttext=Ent.gettext (). toString (); Bitmap Log=Bitmapfactory.decoderesource (Getresources (), R.drawable.weixintubiao); Try{Bitmap BM=Createcode (Enttext,log,barcodeformat.qr_code);                Img.setimagebitmap (BM); } Catch(writerexception e) {e.printstacktrace ();    }            }}); }     PublicBitmap Createcode (String string,bitmap mbitmap, Barcodeformat format)throwswriterexception {//Matrix, the Chinese language called matrices, in the image processing, mainly for the plane of the scaling, translation, rotation and other operations. Matrix mat=NewMatrix (); floatImgx= (float) 2*image_halfwidth/mbitmap.getwidth (); floatImgy= (float) 2*image_halfwidth/mbitmap.getheight (); Mat.setscale (imgx,imgy);//Set Scaling information//Zoom the logo image to the information set by MartixMbitmap = Bitmap.createbitmap (mbitmap, 0, 0,mbitmap.getwidth (), Mbitmap.getheight (), Mat,false); Multiformatwriter writer=NewMultiformatwriter (); Hashtable htable=NewHashtable (); Htable.put (Encodehinttype.character_set,"UTF-8");//Set character encoding//generate two-dimensional code matrix informationBitmatrix matrix = Writer.encode (string, Format, 800, 800, htable); intWidth=matrix.getwidth ();//Matrix Width        intHeight=matrix.getheight ();//Matrix Height        intMatw=width/2; intMath=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//start Iteration matrix from line         for(intx=0;x<width;x++)//Iterate Columns        {            if(x > Matw-image_halfwidth && x < MATW + image_halfwidth && y > Math-image_halfwidth &&amp ; Y < MatH +image_halfwidth) {                //This location is used to store picture information//record pictures per pixel informationPixels[y * width + x] = Mbitmap.getpixel (x-matw+ image_halfwidth, Y-math +image_halfwidth); }            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; }   }

1, first we define three control variables, the R file to find the control ID for related operations, for the button control to add the relevant onclick click event.

2, by invoking the system's bitmap class pair to the picture to carry on the related operation.

3. Generate two-dimensional code and embed the icon by Createcode.

Eventually:

QR Code (Android)

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.