This article uses zxing-android-embedded This open source project to achieve QR code scanning/generation function;
Development tools: Android Studio
1. How to add zxing-android-embedded to in our project
1.1 Add arr Dependency Package
Add the following code to the build.gradle file.
Repositories { mavencentral () maven { url "Https://dl.bintray.com/journeyapps/maven" }}dependencies { // Supports Android 4.0.3 and later (api level 15) compile ' Com.journeyapps: Zxing-android-embedded:[email protected] ' // supports android 2.1 and later (api level 7), but not optimal for later Android versions. // if you only plan on supporting android 4.0.3 and up, you don ' t need to include this. compile ' com.journeyapps:zxing-android-legacy:[email protected] ' // convenience library to launch the scanning activities. // it automatically Picks the best scanning library from the above two, depending on the // Android version and what is available. compile ' com.journeyapps:zxing-android-integration:[email Protected] ' // Version 3.0.x of zxing core contains Some code that is not compatible on android 2.2 and earlier. // this mostly affects encoding, but you should test if you plan to support these versions. // Older versions e.g. 2.2 may also work if you need Support for older android versions. compile ' com.google.zxing:core:3.2.0 ' }
Note: The build.gradle file is in the app directory
650) this.width=650; "alt=" Android QR code scanning and generation (built-in)-Jiatao_zhou-jiatao_zhou's Blog "src=" http://img0.ph.126.net/8o Au1tfekpbavsdmu4wbxw==/6630629060560175226.png "style=" border:0px;height:auto;margin:0px 10px 0px 0px;width:300px; " />
1.2 Sync your project with Gradle
Click "Sync Project with gradlefiles" and Android Studio will be networked to download the necessary files.
650) this.width=650; "alt=" Android QR code scanning and generation (built-in)-Jiatao_zhou-jiatao_zhou's Blog "src=" http://img1.ph.126.net/ Y1h3kwh5qpxslzl_h2_kyw==/6630523507443606550.png "style=" border:0px;height:auto;margin:0px 10px 0px 0px;width : 500px; "/>
By this step, our project is ready to use the zxing-android-embedded code!
2, scanning QR code
2.1 Start QR Code scanning interface
In Activity , starting with the default option, you can use the following code:
New Intentintegrator (this). Initiatescan (); ' This ' is the current activity
In Fragment , starting with the default option, you can use the following code :
Intentintegrator.forsupportfragment (This). Initiatescan (); ' This ' is the current fragment
2.1.1 custom options, refer to Intentintegrator
2.2 receiving scan results
the scan is complete and will call your Onactivityresult method .
So we need to rewrite the onactivityresult method to get the scan results.
@Overridepublic void onactivityresult (int requestcode, int resultcode, intent intent) {super.onactivityresult (requestcode, resultcode, intent); if (RESULTCODE == ACTIVITY.RESULT_OK) { String contents = intent.getstringextra ("Scan_result");//image content string format = intent.getstringextra ("Scan_result_format"); //image Format // handle successful scan         LOG.V ("Qr-code", contents + "# # #" + format); tv_content.settext (contents); } else if (resultcode == activity.result_canceled) { // handle cancel }}
In this step, the QR code scanning function is completed.
If you scan this image of the following two-dimensional code, you will get this output statement "www.google.com### #QR_CODE" in logcat
650) this.width=650; "alt=" Android QR code scanning and generation (built-in)-Jiatao_zhou-jiatao_zhou's Blog "src=" http://img0.ph.126.net/1_ Tl1wb3prfjbhyp04dtjg==/6630664244932264602.png "style=" border:0px;height:auto;margin:0px 10px 0px 0px;width:300px; " />
3, generate two-dimensional code
If you need to convert a piece of text into a QR code bitmap, refer to the following code
Private bitmap generateqrcode (string qrcodestring) { bitmap bmp = null; //Two-dimensional code image QRCodeWriter writer = New qrcodewriter (); try { Bitmatrix bitmatrix = writer.encode (qrcodestring, barcodeformat.qr_code, 512, //parameters are represented as: barcode text content, barcode format, width, height int width = bitmatrix.getwidth (); int height = Bitmatrix.getheight (); bmp = bitmap.createbitmap ( width, height, bitmap.config.rgb_565); //plots each pixel for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { bmp.setpixel (X, y, bitMatrix.get (x , y) ? color.black : color.white); } } } catch (writerexception e) { e.printstacktrace (); } return bmp;}
This article is from "Happy coding ..." Blog, be sure to keep this source http://jiataozhou.blog.51cto.com/7355162/1651712
Android QR code scan and build (built-in)