Android app-QR code generation (QR code)

Source: Internet
Author: User

QR codes can be seen everywhere now. using Android code to generate a QR code based on the input string is actually very simple. You need to reference a Google open-source package-ZXing. The following example contains the generation and resolution of the barcode and QR code. The following describes the generation of the QR code. First, the following steps are provided: 1. first, enter the string content to be generated in the edit box. 2. click the button below 3. the ImageView control at the bottom of the button displays the generated QR code and the specific code is given below: 1. interface layout [html] <? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "fill_parent" android: layout_height = "fill_parent" android: background = "@ android: color/white" android: orientation = "vertical"> <Button android: id = "@ + id/btn_scan_barcode" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: layout_marginTop = "30dp" android: text = "Open camera"/> <Lin EarLayout www.2cto.com android: orientation = "horizontal" android: layout_marginTop = "10dp" android: layout_width = "fill_parent" android: layout_height = "wrap_content"> <TextView android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: textColor = "@ android: color/black" android: textSize = "18sp" android: text = "Scan result: "/> <TextView android: id =" @ + id/TV _scan_result "android: layout_wi Dth = "fill_parent" android: textSize = "18sp" android: textColor = "@ android: color/black" android: layout_height = "wrap_content"/> </LinearLayout> <EditText android: id = "@ + id/et_qr_string" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: layout_marginTop = "30dp" android: hint = "Input the text"/> <Button android: id = "@ + id/btn_add_qrcode" android: layout_width = "fill_parent" android: Layout_height = "wrap_content" android: text = "Generate QRcode"/> <ImageView android: id = "@ + id/iv_qr_image" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_marginTop = "10dp" android: layout_gravity = "center"/> </LinearLayout> 2. code for generating a QR code [java] package com. zxing. encoding; import java. util. hashtable; import android. graphics. bitmap; import com. google. zxing. barcodeForma T; import com. google. zxing. encodeHintType; import com. google. zxing. multiFormatWriter; import com. google. zxing. writerException; import com. google. zxing. common. bitMatrix;/*** @ author Ryan Tang **/public final class EncodingHandler {private static final int BLACK = 0xff000000; public static Bitmap createQRCode (String str, int widthAndHeight) throws WriterException {Hashtable <EncodeHintType, Stri Ng> hints = new Hashtable <EncodeHintType, String> (); hints. put (EncodeHintType. CHARACTER_SET, "UTF-8"); BitMatrix matrix = new MultiFormatWriter (). encode (str, BarcodeFormat. QR_CODE, widthAndHeight, widthAndHeight); int width = matrix. getWidth (); int height = matrix. getHeight (); int [] pixels = new int [width * height]; for (int y = 0; y

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.