Android_demo generation of two-dimensional code

Source: Internet
Author: User

Today we will learn an automatic generation of two-dimensional code of the wording. We can often see a variety of two-dimensional code, such as the public number of the QR code, URL, plus friends, pay the QR code and so on. In fact, every two-dimensional code is only used in the form of pictures to show, is actually some strings. And this string can be displayed by our tool that identifies the QR code. The so-called generation of two-dimensional code is simply to show the string in the form of pictures. What really is powerful is the tool of identification. Of course, I will only generate such a two-dimensional code image, let's learn it together.

First we have to write a layout first.

Activity_main.xml

1<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"2Xmlns:tools= "Http://schemas.android.com/tools"3Android:layout_width= "Match_parent"4android:layout_height= "Match_parent"5android:paddingbottom= "@dimen/activity_vertical_margin"6android:paddingleft= "@dimen/activity_horizontal_margin"7android:paddingright= "@dimen/activity_horizontal_margin"8android:paddingtop= "@dimen/activity_vertical_margin"9tools:context= "Com.example.testzxing.MainActivity" >Ten  One<EditText AAndroid:id= "@+id/edittext1" -Android:layout_width= "Wrap_content" -android:layout_height= "Wrap_content" theAndroid:layout_alignparentleft= "true" -Android:layout_alignparenttop= "true" -android:layout_marginleft= "38DP" -android:layout_margintop= "33DP" +Android:ems= "Ten" > -  +<requestfocus/> A</EditText> at  -<Button -Android:id= "@+id/button1" -Android:layout_width= "Wrap_content" -android:layout_height= "Wrap_content" -android:layout_alignright= "@+id/edittext1" inandroid:layout_below= "@+id/edittext1" -android:layout_marginright= "20DP" toandroid:layout_margintop= "22DP" +android:text= "button"/> -  the<ImageView *Android:id= "@+id/imageview1" $Android:layout_width= "Wrap_content"Panax Notoginsengandroid:layout_height= "Wrap_content" -android:layout_alignleft= "@+id/edittext1" theandroid:layout_below= "@+id/button1" +android:layout_marginleft= "14DP" Aandroid:layout_margintop= "47DP"/> the  +</RelativeLayout>

Then write the function code in the Mainactivity.class. The notes are clearly written.

 Packagecom.example.testzxing;Importjava.util.Hashtable;ImportCom.google.zxing.BarcodeFormat;ImportCom.google.zxing.EncodeHintType;ImportCom.google.zxing.MultiFormatWriter;Importcom.google.zxing.WriterException;ImportCom.google.zxing.common.BitMatrix;Importandroid.app.Activity;ImportAndroid.graphics.Bitmap;Importandroid.graphics.BitmapFactory;ImportAndroid.graphics.Matrix;ImportAndroid.os.Bundle;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.EditText;ImportAndroid.widget.ImageView; Public classMainactivityextendsActivity {PrivateEditText Et1; PrivateButton btn1; PrivateImageView Iv1; Private Static Final intImage_halfwidth = 45;//Image Width Value size@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Et1=(EditText) Findviewbyid (R.ID.EDITTEXT1); BTN1=(Button) Findviewbyid (R.id.button1); Iv1=(ImageView) Findviewbyid (R.ID.IMAGEVIEW1); Btn1.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {//Remove StringString tomakepic_string =Et1.gettext (). toString (). Trim (); //Two-dimensional code in the middle of the pictureBitmap logo=Bitmapfactory.decoderesource (Getresources (), r.drawable.center); Try{Bitmap BM=Createcode (Tomakepic_string,logo,barcodeformat.qr_code);                Iv1.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 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 HST=NewHashtable (); Hst.put (Encodehinttype.character_set,"UTF-8");//Set character encoding//generate two-dimensional code matrix informationBitmatrix matrix = Writer.encode (string, Format, 800, 800, HST); 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 && Amp Y < HALFH +image_halfwidth) {                    //This location is used to store picture information//record pictures per pixel informationPixels[y * width + x] = Mbitmap.getpixel (x-halfw+ image_halfwidth, Y-HALFH +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; }}

This is what happens when we run it.

The QR code can be used to identify the tool.

Of course, if this is simply not possible. Because there is a very important jar package, no reference can be made. Cannot generate code.

Be sure to add this jar package Oh.

Today is probably the content, very simple is not, many of the above code are written by the Ox people, so we quote is, have the ability to consider writing their own set of code to generate two-dimensional codes.

Finally, do not use the QR code to do bad things yo. We are a level program APE!!!

Android_demo generation of two-dimensional 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.