Android QR code creation, android

Source: Internet
Author: User
Tags vcard

Android QR code creation, android

The QR code has entered our lives, and more people are using it to make our lives easier. I have heard of a joke, when we die, there will no longer be any inscription on the tombstone, but there will be a QR code that records your life's information. When people come to your tombstone, scan your cell phone to see the great achievements of your life. This is not very interesting. I think it will become a reality in the near future. Haha, the joke is over. Let's take a look at how to make the QR code serve us in Android development.

In this article, I will show my friends how to design a QR code that records basic personal information. For those who have developed algorithms, I would like to disappoint you here. For the algorithm generated by QR codes, I am not easy to learn and can't share it with you. We will use core for the implementation of this example. jar implementation, for the download of this jar package, I provide you with a link, so that you can learn to use: http://pan.baidu.com/s/1bnGZoF9

After preparing our jar package, let's start today's design. Step 1: Create a project and import the jar package

In our integrated development environment, create an Android project to pave the way for the design of today's cases. After creating the project, import the downloaded jar package to our project, Ctrl + c our jar package, and find the libs folder Ctrl + v under our project directory, what then? It means to import our jar package to the project through the integrated development environment.

  

Step 2: Create a 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/plugin" android: paddingRight = "@ dimen/plugin" android: paddingTop = "@ dimen/activity_v Ertical_margin "tools: context = ". mainActivity "> <LinearLayout android: layout_width =" fill_parent "android: layout_height =" wrap_content "android: orientation =" vertical "android: id =" @ + id/ll "> <! -- Used to display the created QR code --> <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 = ""/> <EditText android: id = "@ + id/etCompany" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: hint = "optional"/> </LinearLayout> <! -- Telephone --> <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 = ""/> <EditText android: id = "@ + id/etPhone" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: hint = "optional"/> </LinearLayout> <! -- Email --> <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 = ""/> <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 = ""/> <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 = ""/> </LinearLayout> </RelativeLayout>

Step 3: edit our Activity:

Public class MainActivity extends Activity {private EditText etCompany; private EditText etPhone; private EditText etEmail; private EditText etWeb; private Bitmap logo; private static final int IMAGE_HALFWIDTH = 40; // width value, affected intermediate image size @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // obtain the resource image. You can change it to get a local image or take a photo to get the image logo = 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 (new OnClickListener () {@ Override public void onClick (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 the QR code String contents = "BEGIN: VCARD \ nVERSION: 3.0 \ nORG:" + company + "\ nTEL: "+ phone +" \ nURL: "+ web +" \ nEMAIL: "+ email +" \ nEND: VCARD "; try {// call the createCode method to generate the QR code Bitmap bm = createCode (contents, logo, BarcodeFormat. QR_CODE); ImageView img = (ImageView) findViewById (R. id. imgCode); // display the img on the QR code page. setImageBitmap (bm);} catch (WriterException e) {e. printStackTrace ();}}});} /*** generate a QR code ** @ param string the text information contained in the QR code * @ param mBitmap logo image * @ param format encoding format * @ return Bitmap * @ throws WriterException */public bitmap createCode (String string, bitmap mBitmap, BarcodeFormat format) throws WriterException {Matrix m = new Matrix (); float sx = (float) 2 * IMAGE_HALFWIDTH/mBitmap. getWidth (); float sy = (float) 2 * IMAGE_HALFWIDTH/mBitmap. getHeight (); m. setScale (sx, sy); // set the scaling information // scale the logo image to the information set by martix mBitmap = Bitmap. createBitmap (mBitmap, 0, 0, mBitmap. getWidth (), mBitmap. getHeight (), m, false); MultiFormatWriter writer = new MultiFormatWriter (); // Hashtable <EncodeHintType, String> hst = new Hashtable <EncodeHintType, String> (); hst. put (EncodeHintType. CHARACTER_SET, "UTF-8"); // sets the character encoding BitMatrix matrix = writer. encode (string, format, 400,400, hst); // generate a two-dimensional code matrix. int width = matrix. getWidth (); // matrix height int height = matrix. getHeight (); // matrix width int halfW = width/2; int halfH = height/2; int [] pixels = new int [width * height]; // define the length of the array as the height of the matrix * the width of the matrix, used to record the pixel information in the matrix for (int y = 0; y 

The above code comments have been very detailed. Here I will simply say a few more words. This part of the code is divided into the upper and lower sections, which are frequently used, the lower part is the focus of QR code creation. Okay, here we have achieved the effect. The last one is:

  

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.