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