Android: Use ZXing to generate a QR code (Logo design can be added)

Source: Internet
Author: User

Android: Use ZXing to generate a QR code (Logo design can be added)

ZXing is an open-source library of Google that can be used to generate and scan QR codes. This article introduces the first part.

First:

ZXing-related files official: https://github.com/zxing/zxing/releases

Or download it here (only the jar package used in this project, version: 3.2.0): Link: http://pan.baidu.com/s/1hq3s5EW password: mvg7

1. Tool class for generating QR codes

 

/*** QR code generation tool class */public class QRCodeUtil {/*** generates a QR code Bitmap ** @ param content * @ param widthPix Image Width * @ param heightPix Image Height *@ logo icon in the param logoBm QR code Center (can be null) * @ param filePath: Path of the file used to store the QR code image * @ return: generate the QR code and whether the file is saved successfully */public static boolean createQRImage (String content, int widthPix, int heightPix, Bitmap logoBm, string filePath) {try {if (content = null | "". equals (content) {return false;} // configure the Map Parameter
 
  
Hints = new HashMap <> (); hints. put (EncodeHintType. CHARACTER_SET, "UTF-8"); // fault tolerance level hints. put (EncodeHintType. ERROR_CORRECTION, ErrorCorrectionLevel. h); // set the width of the blank margin // hints. put (EncodeHintType. MARGIN, 2); // default is 4 // image data conversion, using the matrix conversion BitMatrix bitMatrix = new QRCodeWriter (). encode (content, BarcodeFormat. QR_CODE, widthPix, heightPix, hints); int [] pixels = new int [widthPix * heightPix]; // follow the QR code below Generates two-dimensional images one by one. // two for loops are the results of horizontal scanning of images for (int y = 0; y 
2. Use in Activity:

 

 

/*** Generate the QR code */public class MainActivity extends ActionBarActivity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // content final EditText contentET = (EditText) findViewById (R. id. create_qr_content); // display the QR code image final ImageView imageView = (ImageView) findViewById (R. id. create_qr_iv); // whether to add the Logo final CheckBox addL OgoCB = (CheckBox) findViewById (R. id. create_qr_addLogo); Button createQrBtn = (Button) findViewById (R. id. create_qr_btn); createQrBtn. setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {final String filePath = getFileRoot (MainActivity. this) + File. separator + "qr _" + System. currentTimeMillis () + ". jpg "; // when the QR code image is large, it may take a long time to generate and save the image. Therefore, the new Thread (new Runnable () {@ Override public void run () {boolean success = QRCodeUtil. createQRImage (contentET. getText (). toString (). trim (), 800,800, addLogoCB. isChecked ()? BitmapFactory. decodeResource (getResources (), R. mipmap. qr_logo): null, filePath); if (success) {runOnUiThread (new Runnable () {@ Override public void run () {imageView. setImageBitmap (BitmapFactory. decodeFile (filePath ));}});}}}). start () ;}}) ;}// file storage root directory private String getFileRoot (Context context) {if (Environment. getExternalStorageState (). equals (Environment. MEDIA_MOUNTED) {File external = Context. getExternalFilesDir (null); if (external! = Null) {return external. getAbsolutePath () ;}} return context. getFilesDir (). getAbsolutePath ();}}

3. Save the image file in

 

context.getExternalFilesDir(null)
Directory. According to the official api documentation, from KitKat (Android 4.4), saving files to this directory does not require the SD card read/write permission. However, tests show that on redmi Note and meizu MX3 (the system is android 4.4.4), permissions are not required, but on my Huawei P6 (Android 4.4.2 ), you must declare the permission to successfully save the file, that is, you must add the following content to manifest:

 

 

 
  
 
Therefore, I guess that the so-called no permission is from Android 4.4.4.


 

 


 

 

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.