Android QR code generation

Source: Internet
Author: User

To download a class library core. jar, click the open link in my resource file.

Import java. io. File;
Import java. io. FileNotFoundException;
Import java. io. FileOutputStream;
Import java. io. IOException;
Import java. io. UnsupportedEncodingException;

Import android. app. Activity;
Import android. graphics. Bitmap;
Import android. OS. Bundle;
Import android. OS. Environment;
Import android. view. SurfaceHolder;
Import android. view. SurfaceView;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Import android. widget. ImageView;
Import android. widget. TextView;

Import com. google. zxing. BarcodeFormat;
Import com. google. zxing. MultiFormatWriter;
Import com. google. zxing. WriterException;
Import com. Google. zxing. Common. bitmatrix;

Public class erweimashezhiactivity extends activity implements surfaceholder. Callback {
Button zuotiananniu, youbiananniu, fenxiangwodemingpian, jinruwodevzu;
Textview zhongjianzifu;
Private surfaceview msurfaceview01;
Private surfaceholder msurfaceholder01;
Final string imagefilename = "2dcode.png ";
Fileoutputstream Fos = NULL;
@ Override
Protected void oncreate (bundle savedinstancestate ){
// Todo auto-generated method stub
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. activity_erweimashezhi );



String Xinxi = "mobile phone number:" + "123456789" + "\ n mailbox:" + "a@a.com" + "\ n company:" + "MFO"
+ "\ N address:" + "xxxxxxx12345" + "\ n fixed address:" + "123456" + "\ n zip code:" + "123456 ";
System. Out. println (Xinxi );
Try {
Bitmap bitmap = create2dcode (Xinxi );
If (null! = Bitmap) {// Save the QR code image to a file
File file = new File (Environment. getExternalStorageDirectory (), imageFileName );
Fos = new FileOutputStream (file );
Bitmap. compress (Bitmap. CompressFormat. PNG, 0, fos );
}
ImageView imagview = (ImageView) findViewById (R. id. erweimatupian );
Imagview. setImageBitmap (bitmap );
// Canvas canvas = new Canvas (bitmap );
} Catch (WriterException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
} Catch (UnsupportedEncodingException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
} Catch (FileNotFoundException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
} Finally {
Try {
Fos. close ();
} Catch (IOException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
}
Public Bitmap Create2DCode (String str) throws WriterException, UnsupportedEncodingException {
// Generate a two-dimensional matrix with the specified size during encoding. Do not scale the image after it is generated. This will result in Fuzzy Identification failure.
Str = new String (str. getBytes ("UTF-8"), "ISO-8859-1 ");//
BitMatrix matrix = new MultiFormatWriter (). encode (str, BarcodeFormat. QR_CODE, 300,300 );
Int width = matrix. getWidth ();
Int height = matrix. getHeight ();
// Convert the two-dimensional matrix into a one-dimensional pixel array, that is, it is always horizontally arranged
Int [] pixels = new int [width * height];
For (int y = 0; y For (int x = 0; x <width; x ++ ){
If (matrix. get (x, y )){
Pixels [y * width + x] = 0xff000000;
}

}
}
Bitmap bitmap = Bitmap. createBitmap (width, height, Bitmap. Config. ARGB_8888 );
// Generate bitmap Using pixel arrays. For details, refer to the api
Bitmap. setpixels (pixels, 0, width, 0, 0, width, height );
Return bitmap;
}
Public void surfacechanged (surfaceholder holder, int format, int width,
Int height ){
// Todo auto-generated method stub

}
Public void surfacecreated (surfaceholder holder ){
// Todo auto-generated method stub

}
Public void surfaceDestroyed (SurfaceHolder holder ){
// TODO Auto-generated method stub

}
}

Related Article

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.