Android generates two-dimensional code

Source: Internet
Author: User

New Project

The layout is as follows (an input box, a button, a imageview), enter what you want (text, numbers, website links, etc.) and then click the button to generate the QR code, which can then be scanned for recognition.

First you need a jar package from Google

Activity's Code

Private EditText et1;private Button btn1;private ImageView iv1;private static final int image_halfwidth = 35;//Picture Width value size

  

Find the input box, and the button, ImageView

                Et1 = (EditText) Findviewbyid (R.ID.EDITTEXT1); btn1 = (Button) Findviewbyid (r.id.button1); iv1 = (ImageView) Findviewbyid (R.ID.IMAGEVIEW1);

button click event

Btn1.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {//TODO auto-generated method Stubstring tomakepic_string = Et1.gettext (). toString (). Trim (); Bitmap logo= Bitmapfactory.decoderesource (getresources (), r.drawable._netcfg); try {Bitmap Bm=createcode (toMakePic_ String,logo,barcodeformat.qr_code); A method is iv1.setimagebitmap below (BM);} catch (Writerexception e) {e.printstacktrace ();}}});

  

Createcode method returns bitmap
    Public Bitmap Createcode (String string,bitmap mbitmap, Barcodeformat format) throws Writerexception {//matrix, Chinese call moment    Array, in the aspect of image processing, is mainly used for the scaling, translation, rotation and other operations of the plane.    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//Martix the logo image by the information set mbitmap = Bitmap.createbitmap (mbitmap, 0, 0,mbitmap.getwidth (), Mbitmap.getheight (), m, false); Multiformatwriter writer = new Multiformatwriter (); Hashtable HST = new Hashtable () hst.put (Encodehinttype.character_set, "UTF-8");//Set character encoding//generate two-dimensional code matrix information Bitmatrix Matrix = Writer.encode (string, format, +, n, HST); 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];//defines array length as matrix height * Matrix width for recording pixel information in matrix for (int y = 0; y < height; y++) {//starting from line iteration matrix for (int x = 0; x < width; + +) {//Iterate column if (x > Halfw-image_halfwidth && x < HALFW +Image_halfwidth && y > Halfh-image_halfwidth && y < HALFH + image_halfwidth) {//This location is used to store picture information//record pictures per Pixel Information Pixels[y * width + x] = Mbitmap.getpixel (x-halfw+ image_halfwidth, Y-HALFH + image_halfwidth); }else {if (Matrix.get (x, y)) {//If there is a black point, record information pixels[y * width + x] = 0xff000000;//record black block information}}}}bitmap Bitmap = Bitmap.createbit Map (width, height,bitmap.config.argb_8888);//Generate bitmapbitmap.setpixels (pixels, 0, width, 0, 0, width, height) through the array of pixels; return bitmap;}

  

Complete (write whatever you Can) ""

Android generates 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.