The simplest scan generates a two-dimensional code
One Goal,one passion!
Now the app scan two-dimensional code is almost standard, and scanning two-dimensional code demo is a dime a mile. But almost all of them are big pieces of copy many classes. And it is very cumbersome to use. I've been very bad at it all the time. And sometimes it's not used. It finally simplifies one of the most simple demos I've ever seen in my own time.
1th Step:
Prepare for work-– of course, zxing is still necessary. It's just a simplified zxing.jar. 2nd Step:
This is also the core code:
A. This activity is the description of the scanning engineering and processing of the scan results.
public class Scanneractivity extends activity implements Ondecodecompletionlistener {private Scannerview Scannerview
;
private string string;
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Init ();
private void Init () {Scannerview = (Scannerview) Findviewbyid (R.id.scanner_view);
Scannerview.setondecodelistener (this); @Override public void Ondecodecompletion (string barcodeformat, String barcode, Bitmap Bitmap) {if (BA Rcode = = NULL | |
"". Equals (Barcode)) {Alertdialog builder = new Alertdialog.builder (scanneractivity.this).
Settitle ("Error"). Setmessage ("Not Found"). Show ();
else {string = barcode.substring (Barcode.indexof ("?") + 1, barcode.length ());
Mshowdialog (string);
}} @Override protected void Onresume () { Super.onresume ();
Scannerview.onresume ();
} @Override protected void OnPause () {super.onpause ();
Scannerview.onpause ();
} @Override protected void OnDestroy () {Super.ondestroy (); }/**/private void Mshowdialog (final String URL) {alertdialog alertdialog = new Alertdialog.builder (Sc anneractivity.this). Settitle ("the result"). Setmessage (URL). Setnegativebutton ("Cancel", new Dialoginterface.on Clicklistener () {@Override public void OnClick (Dialoginterface dialog, int which)
{Finish ();
}). Setpositivebutton ("OK", new Dialoginterface.onclicklistener () {@Override public void OnClick (Dialoginterface dialog, int which) {if (url.substring (0, 4). equ
ALS ("http")) {OpenURL (URL); } ELSE {}}}). Show ();
} private void OpenURL (String url) {uri uri = uri.parse (URL);
Intent Intent = new Intent (Intent.action_view, URI);
StartActivity (Intent);
Finish ();
}
}
B. The XML file used:
<?xml version= "1.0" encoding= "Utf-8"?> <relativelayout 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"
tools:context= "com.example.scanner2.MainActivity ">
<framelayout
android:layout_width=" match_parent "
android:layout_height=" Match_parent "
>
<com.covics.zxingscanner.scannerview
android:id= "@+id/scanner_view"
android: Layout_width= "Match_parent"
android:layout_height= "match_parent"
/>
</FrameLayout>
</RelativeLayout>
2nd Step:
Invoke run view in mainactivity:
public class Mainactivity extends activity {Button SCANNER_QR, CREATER_QR;
ImageView IV;
EditText ET_QR;
Activity Act;
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_scanner);
act = this;
Initview ();
private void Initview () {SCANNER_QR = (Button) Findviewbyid (R.ID.SCANNER_QR);
ET_QR = (edittext) Findviewbyid (R.ID.ET_QR);
CREATER_QR = (Button) Findviewbyid (R.ID.CREATER_QR);
IV = (ImageView) Findviewbyid (R.ID.IV);
Scanner_qr.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) {
Intent inten = new Intent (mainactivity.this, Scanneractivity.class);
StartActivity (Inten);
}
}); Creater_qr.setonclicklistener (New View.onclicklistener () {@Override publicvoid OnClick (View v) {String str = Et_qr.gettext (). toString (). Trim ();
if (Str.isempty ()) {Toast.maketext (mainactivity.this, "No input string", Toast.length_short). Show ();
else {final Bitmap qrcodebitmap = Createqrcodebitmap (str);
Act.runonuithread (New Runnable () {@Override public void run () { Converts the icon's bitmap into bitmapdrawable bitmapdrawable icondrawable = new Bitmapdrawa
BLE (QRCODEBITMAP);
Iv.setbackground (icondrawable);
}
});
}
}
});
}
The
method for generating two-dimensional code. Call this method directly to pass in the URL parameter to generate a two-dimensional code.
/** * Create QR two-dimensional code picture method */Private Bitmap createqrcodebitmap (String URL) {//To set QR two-dimensional code parameter Hash
Table<encodehinttype, object> qrparam = new Hashtable<encodehinttype, object> ();
Set the level of error correction for QR two-dimensional code--select highest H-level qrparam.put (Encodehinttype.error_correction, ErrorCorrectionLevel.H);
Set encoding mode Qrparam.put (Encodehinttype.character_set, "UTF-8");
Set the contents of the two-dimensional code, here I use my blog address//String cont = "HTTP://BLOG.CSDN.NET/FENGLTXX"; Generate QR two-dimensional code data--here just get an array of true and false: encoded content URL address, encoding type, generating picture width, generating picture height, setting parameter try {BITM
Atrix Bitmatrix = new Multiformatwriter (). Encode (URL, barcodeformat.qr_code, 180, 180, qrparam);
Start using two-dimensional code data to create bitmap pictures, respectively, set to black and white int w = bitmatrix.getwidth ();
int h = bitmatrix.getheight ();
int[] data = new INT[W * h]; for (int y = 0; y < h; y++) {for (int x = 0; x< W;
X + +) {if (Bitmatrix.get (x, y)) data[y * w + x] = 0xff000000;//Black
Else Data[y * w + x] = -1;//-1 equals 0xFFFFFFFF white}}
Create a Bitmap picture with the highest effect to display Bitmap Bitmap = Bitmap.createbitmap (W, H, Bitmap.Config.ARGB_8888);
The above two-dimensional code color array is passed in to generate the picture color bitmap.setpixels (data, 0, W, 0, 0, W, h);
return bitmap;
catch (Writerexception e) {e.printstacktrace ();
return null; }
}
Mainactivity XML file:
<?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_hei ght= "match_parent" android:orientation= "vertical" tools:context= "com.example.scanner2.ScannerActivity" > &L T Button android:id= "@+id/scanner_qr" android:layout_width= "wrap_content" android:layout_height= "Wra" P_content "android:text=" scan two-dimensional code "/> <edittext android:id=" @+id/et_qr "Android:layout_wid"
Th= "Match_parent" android:layout_height= "wrap_content" android:hint= "Please enter the string to generate the two-dimensional code"/> <button Android:id= "@+id/creater_qr" android:layout_width= "wrap_content" android:layout_height= "Wrap_conten" T "android:text=" generates two-dimensional code "/> <textview android:layout_width=" Wrap_content "android:layou
t_height= "Wrap_content"android:text= "Two-dimensional code image display"/> <imageview android:id= "@+id/iv" android:layout_width= "180DP" a
ndroid:layout_height= "180DP" android:background= "#8ff4f4"/> </LinearLayout>
Finally, don't forget to add the right to use the camera
<uses-permission android:name= "Android.permission.CAMERA" ></uses-permission>
<uses-feature Android:name= "Android.hardware.camera"/>
<uses-feature android:name= " Android.hardware.camera.autofocus "/>
<uses-permission android:name=" Android.permission.VIBRATE "/>"
<uses-permission android:name= "Android.permission.FLASHLIGHT"/>
Two-dimensional code generation scan demo case has been fully implemented. Zxing package has a lot of content, it is not too convenient to use, simplifying the jar package, the use of it is very convenient. No more than hundreds of lines of code to integrate zxing each time.
Download Zxing.jar Link: http://download.csdn.net/my
Demo Download: http://download.csdn.net/detail/fengltxx/9647285