Here is a brief introduction to the Zxing library. Zxing is an open source, Java-implemented 1d/2d barcode image processing library, which contains ports that are contacted in other languages. Zxing can achieve the use of mobile phone's built-in camera to complete the barcode scanning and decoding. The project can be implemented by barcode coding and decoding. The following formats are currently supported: UPC-A,UPC-E, EAN-8,EAN-13, 39 yards, 93 yards. Zxing is a very classic barcode/two-D code recognition of open source class library, previously on the function machine, there are developers using J2ME to use the zxing, but to support the JSR-234 specification (AF) mobile phone can play its power.
Zxinggithub Address
Effect Chart:
Main implementation steps:
Import libzxing This module
The zxing source code is very big, the function is also many, here just extracts some of the code to integrate together
Scanning
Add a button to the main_activity and a TextView click the button to start the camera function, scan the two-dimensional code
TextView will show the results after the scan
<button
android:text= "Strat Scan android:layout_width=" match_parent "android:layout_height="
Wrap_content "
android:onclick=" Scan "/>
<textview android:id=
" @+id/tv_showresult "
Android : layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:text= "Hello world!" />
Initialize the two controls in Activitymain
Private TextView Mtextview;
mtextview= (TextView) This.findviewbyid (r.id.tv_showresult);
Scan two-dimensional code
//https://cli.im/text?2dd0d2b267ea882d797f03abf5b97d88 two-dimensional code generation Web site public
Void Scan (view view) {
Startactivityforresult (The new Intent (this, captureactivity.class), 0);
}
@Override
protected void onactivityresult (int requestcode, int resultcode, Intent data) {
Super.onactivityresult (Requestcode, ResultCode, data);
if (RESULTCODE==RESULT_OK) {
Bundle Bundle = Data.getextras ();
if (bundle!= null) {
String result=bundle.getstring ("result");
Mtextview.settext (result);}}
Generate a two-dimensional code
This is where the entire project's XML files are posted, plus the previous scan
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/" Android "xmlns:tools=" Http://schemas.android.com/tools "android:layout_width=" Match_parent "android:layout_height = "Match_parent" android:paddingbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_ Horizontal_margin "android:paddingright=" @dimen/activity_horizontal_margin "android:paddingtop=" @dimen/activity_ Vertical_margin "android:orientation=" vertical "tools:context=" com.example.hfs.zxingdemo.MainActivity "> < Button android:text= "Strat Scan" android:layout_width= "match_parent" android:layout_height= "Wrap_content" Android:o nclick= "Scan"/> <textview android:id= "@+id/tv_showresult" android:layout_width= "Wrap_content" android:layout _height= "Wrap_content" android:text= "Hello world!" /> <edittext android:id= "@+id/et_text" android:hint= "imput" android:layout_width= "Match_parent" Android: layout_height= "Wrap_content"/> <button android:Layout_width= "Match_parent" android:layout_height= "wrap_content" android:onclick= "make" android:text= "Make QRCode" /> <checkbox android:id= "@+id/cb_logo" android:layout_width= "Wrap_content" android:text= "logo" Android:layout _height= "Wrap_content"/> <imageview android:id= "@+id/img_shouw" android:layout_width= "Wrap_content" Android: layout_gravity= "center" android:background= "@mipmap/ic_launcher" android:layout_height= "wrap_content"/> </ Linearlayout>
mainactivity Code
Package Com.example.hfs.zxingdemo;
Import android.content.Intent;
Import Android.graphics.Bitmap;
Import Android.graphics.BitmapFactory;
Import Android.hardware.camera2.CaptureRequest;
Import android.support.v7.app.AppCompatActivity;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.widget.CheckBox;
Import Android.widget.EditText;
Import Android.widget.ImageView;
Import Android.widget.TextView;
Import Android.widget.Toast;
Import com.xys.libzxing.zxing.activity.CaptureActivity;
Import Com.xys.libzxing.zxing.encoding.EncodingUtils; public class Mainactivity extends Appcompatactivity {private TextView mtextview; private edittext medittext; private imag
Eview Mimageview;
Private CheckBox Mcheckbox; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (
R.layout.activity_main);
Initview (); private void Initview () {mtextview= (TextView) This.findviewbyid (R.id.tv_showresult); medittext= (EditText) This.findviewbyId (R.id.et_text);
mimageview= (ImageView) This.findviewbyid (R.id.img_shouw);
mcheckbox= (CheckBox) This.findviewbyid (R.id.cb_logo);
///Scan two-dimensional code//https://cli.im/text?2dd0d2b267ea882d797f03abf5b97d88 two-dimensional code generation Web site public void Scan (view view) {
Startactivityforresult (The new Intent (this, captureactivity.class), 0); @Override protected void Onactivityresult (int requestcode, int resultcode, Intent data) {Super.onactivityresult (reques
Tcode, ResultCode, data); if (RESULTCODE==RESULT_OK) {Bundle Bundle = Data.getextras (); if (Bundle!= null) {String result=bundle.getstring ("Result
");
Mtextview.settext (result);
}}//Generate two-dimensional code can be set logo public void make (view view) {String input = Medittext.gettext (). toString (); if (Input.equals (")) {
Toast.maketext (This, "Enter cannot be empty", Toast.length_short). Show (); }else{Bitmap qrcode = Encodingutils.createqrcode (input, M, mcheckbox.ischecked ()?
Bitmapfactory.decoderesource (Getresources (), r.mipmap.ic_launcher): null);//checkbox Select to set logo
Mimageview.setimagebitmap (QRCode);
}}
}
All right, we're done here.
Project Address
The above is a small set of Android to introduce two-dimensional code scanning and generation of simple method, hope to help everyone, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!