Preparing the current Google Zxing jar package does not support the generation of Chinese code, so Chinese is not supported in this example. For friends who need Chinese support, please modify the Zxing.jar package and compile it yourself. Nonsense not to say, directly on:
The sample code is as follows:
PackageCom.test.createcode;Importandroid.app.Activity;ImportAndroid.graphics.Bitmap;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.EditText;ImportAndroid.widget.ImageView;ImportAndroid.widget.Toast;ImportCom.google.zxing.BarcodeFormat;ImportCom.google.zxing.MultiFormatWriter;Importcom.google.zxing.WriterException;ImportCom.google.zxing.common.BitMatrix;/*** for creating and scanning QR codes*/ Public classCreatecodeactivityextendsActivity {/*** will generate the content of QR code*/ PrivateEditText Codeedit; /*** Generate two-dimensional code codes*/ PrivateButton twocodebtn; /*** Used to show the imageview of generating QR code*/ PrivateImageView codeimg; /*** Generate one-dimensional code button*/ PrivateButton onecodebtn; /*** Interface Initialization event * *@paramSavedinstancestate Bundle Object*/@Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.main); Initview (); Setlistener (); } /*** View to initialize the interface display*/ Private voidInitview () {Codeedit=(EditText) Findviewbyid (R.id.code_edittext); TWOCODEBTN=(Button) Findviewbyid (R.ID.CODE_BTN); ONECODEBTN=(Button) Findviewbyid (R.id.btn_code); Codeimg=(ImageView) Findviewbyid (r.id.code_img); } /*** Set up events to generate QR code and scan QR code*/ Private voidSetlistener () {Twocodebtn.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {String str=Codeedit.gettext (). toString (). Trim (); Bitmap BMP=NULL; Try { if(str! =NULL&& "". Equals (str)) {BMP=Createtwodcode (str); } } Catch(writerexception e) {e.printstacktrace (); } if(BMP! =NULL) {codeimg.setimagebitmap (BMP); } } }); Onecodebtn.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (view view) {String str=Codeedit.gettext (). toString (). Trim (); intSize =str.length (); for(inti = 0; i < size; i++) { intc =Str.charat (i); if((19968 <= c && C < 40623) {toast.maketext (createcodeactivity). This, "The time to generate barcodes cannot be Chinese", Toast.length_short). Show (); return; }} Bitmap bmp=NULL; Try { if(str! =NULL&& "". Equals (str)) {BMP=Createonedcode (str); } } Catch(writerexception e) {e.printstacktrace (); } if(BMP! =NULL) {codeimg.setimagebitmap (BMP); } } }); } /*** Generate the specified content into a QR code * *@paramcontent will generate the contents of the QR code *@returnreturns the generated QR code event *@throwswriterexception writerexception Anomalies*/ PublicBitmap Createtwodcode (String content)throwswriterexception {//generate a two-dimensional matrix, specify the size of the encoding, do not generate a picture and then zoom, which will blur leading to recognition failureBitmatrix matrix =NewMultiformatwriter (). Encode (content, Barcodeformat.qr_code,300, 300); intwidth =matrix.getwidth (); intHeight =matrix.getheight (); //The two-dimensional matrix is converted into a one-dimensional pixel group, which is always lined up horizontally. int[] pixels =New int[Width *height]; for(inty = 0; Y < height; y++) { for(intx = 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 by pixel Group, reference APIBitmap.setpixels (pixels, 0, width, 0, 0, width, height); returnbitmap; } /*** Used to generate a given content into a one-dimensional code note: The current generation of content in Chinese will be directly error, to modify the contents of the underlying JAR package * *@paramcontent will generate the contents of a one-dimensional code *@returnreturns the generated one-dimensional code bitmap *@throwswriterexception writerexception Anomalies*/ PublicBitmap Createonedcode (String content)throwswriterexception {//generate a one-dimensional barcode, specify the size when encoding, do not generate a picture and then zoom, which will blur the recognition failureBitmatrix matrix =NewMultiformatwriter (). Encode (content, barcodeformat.code_128,500, 200); intwidth =matrix.getwidth (); intHeight =matrix.getheight (); int[] pixels =New int[Width *height]; for(inty = 0; Y < height; y++) { for(intx = 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 by pixel Group, reference APIBitmap.setpixels (pixels, 0, width, 0, 0, width, height); returnbitmap; }}
The Main.xml file is as follows:
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "vertical" android:layout_width= "fill_parent"Android:layout_height= "Fill_parent" android:background= "#ff999999" > <edittext android:layout_width= "fill_parent" android:id= "@+id /code_edittext "Android:layout_height= "Wrap_content" android:text= ""/> <linearlayout android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content" android:orientation= "Horizontal"Android:layout_margintop= "20dip" android:layout_gravity= "Center_horizontal" > <button android:id= "@+id/code_btn" Android:layout_widt H= "100dip"Android:layout_height= "40dip" android:text= "generate two-dimensional code"/> <button android:id= "@+id/btn_code" android:layout_width= "100dip" Android:la yout_height= "40dip"Android:text= "Generate one-dimensional code" ></Button> </LinearLayout> <imageview android:layout_width= "Wrap_content" Android Oid:id= "@+id/code_img"Android:layout_height= "Wrap_content" android:layout_margintop= "20dip"android:layout_gravity= "Center_horizontal"/> </LinearLayout>
The manifest file is as follows:
<?xml version= "1.0" encoding= "Utf-8"?> <manifest xmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Com.test.createcode"Android:versioncode= "1"Android:versionname= "1.0" > <uses-sdk android:minsdkversion= "ten"/> <Application Android:icon= "@drawable/ic_launcher"Android:label= "@string/app_name" > <Activity Android:name=". Createcodeactivity "Android:label= "@string/app_name" > <intent-filter> <action android:name= "Android.intent.actio N.main "/> <category android:name=" Android.intent.category.LAUNCHER "/> </inte nt-filter> </activity> </application> </manifest>
Finally, do not download a zxing.jar placed in the Libs folder, you can. (The Bitmatrix and Barcodeformat classes above are dependent on Zxing.jar.) )
:
http://code.google.com/p/zxing/
From: http://blog.csdn.net/chenshufei2/article/details/8682934
Android Barcode Generation Example