Reprint Please specify source: http://blog.csdn.net/xiaanming/article/details/14450809
Sometimes we have such a demand, need to scan the phone has two-dimensional code of the picture, so today is the realization of the two-dimensional code image on the phone to scan, I am here directly in the original project above this function, the following simple introduction of the implementation of this small function, First, I added a ImageButton on the interface, the picture is still used, the following is the title of the scanning interface
<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:background= "@drawable/mmtitle_bg_alpha" > <Button Android:id= "@+id/button_back"Android:layout_width= "75.0dip"Android:layout_height= "Wrap_content"Android:layout_alignparentleft= "true"Android:background= "@drawable/mm_title_back_btn"Android:text= "Back"Android:textcolor= "@android: Color/white"/> <TextView Android:id= "@+id/textview_title"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_centerhorizontal= "true"android:layout_centervertical= "true"android:gravity= "Center_vertical"Android:text= "QR Code Scan"Android:textcolor= "@android: Color/white"android:textsize= "18sp"/> <ImageButton Android:id= "@+id/button_function"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_alignparentright= "true"Android:layout_marginright= "2dip"Android:background= "@drawable/mm_title_right_btn"Android:minwidth= "70dip"android:src= "@drawable/mm_title_btn_menu_normal"/></relativelayout>
View Code
In the scanning interface mipcaactivitycapture to ImageButton click on it, click ImageButton Select a picture from the phone
// Open the album in your phone New // "Android.intent.action.GET_CONTENT" Innerintent.settype ("image/*"); = Intent.createchooser (Innerintent, "Select the QR code image"); this. Startactivityforresult (wrapperintent, Request_code);
View Code
Here we use the Startactivityforresult to jump the interface, when we select the image containing the QR code will be called back to the Mipcaactivitycapture Onactivityresult method, We need to parse the two-dimensional code in the image in the Onactivityresult method.
@Overrideprotected voidOnactivityresult (intRequestcode,intResultCode, Intent data) { Super. Onactivityresult (Requestcode, ResultCode, data); if(ResultCode = =RESULT_OK) { Switch(requestcode) { CaseRequest_code://get the path to the selected picturecursor cursor = getcontentresolver (). Query (Data.getdata (),NULL,NULL,NULL,NULL); if(Cursor.movetofirst ()) {Photo_path=cursor.getstring (Cursor.getcolumnindex (MediaStore.Images.Media.DATA)); } cursor.close (); Mprogress=NewProgressDialog (mipcaactivitycapture. This); Mprogress.setmessage ("Scanning ..."); Mprogress.setcancelable (false); Mprogress.show (); NewThread (NewRunnable () {@Override Public voidRun () {result result=scanningimage (Photo_path); if(Result! =NULL) {Message m=Mhandler.obtainmessage (); M.what=Parse_barcode_suc; M.obj=Result.gettext (); Mhandler.sendmessage (m); } Else{Message m=Mhandler.obtainmessage (); M.what=Parse_barcode_fail; M.obj= "Scan failed!"; Mhandler.sendmessage (m); }}). Start (); Break; } } }View Code
We first get the path of the image by the URI of the image, and then scan the image of the QR code content according to the path of the image, this will decode the picture in a sub-thread, mainly to prevent the occurrence of the ARN because the parsing is too long
Next look at the Scanningimage (String path) method, Zxing.jar provides class Qrcodereader.java for parsing the QR code, using decode (binarybitmap image, map< Decodehinttype,?> hints) method can parse the image of the two-dimensional code information, the following is the path through the image to resolve the contents of the two-dimensional code inside
/*** method of scanning QR code image *@paramPath *@return */ PublicResult scanningimage (String path) {if(Textutils.isempty (path)) {return NULL; } Hashtable<decodehinttype, string> hints =NewHashtable<decodehinttype, string>(); Hints.put (Decodehinttype.character_set,"UTF8");//set the encoding of the QR code contentbitmapfactory.options Options=Newbitmapfactory.options (); Options.injustdecodebounds=true;//get the original size firstScanbitmap =bitmapfactory.decodefile (path, options); Options.injustdecodebounds=false;//get the new size intSampleSize = (int) (Options.outheight/(float) 200); if(samplesize <= 0) SampleSize= 1; Options.insamplesize=samplesize; Scanbitmap=bitmapfactory.decodefile (path, options); Rgbluminancesource Source=NewRgbluminancesource (SCANBITMAP); Binarybitmap Bitmap1=NewBinarybitmap (NewHybridbinarizer (source)); Qrcodereader Reader=NewQrcodereader (); Try { returnReader.decode (BITMAP1, hints); } Catch(notfoundexception e) {e.printstacktrace (); } Catch(checksumexception e) {e.printstacktrace (); } Catch(FormatException e) {e.printstacktrace (); } return NULL; }View Code
Result is encapsulated in the decoded barcode image of the results, we only need to pass result of the GetText () method can take out the inside of the QR code content, this way we took care of scanning the phone QR code image of the small function, next we run the project, see the effect
A friend in doubt can leave a message below, I will answer for everyone, the source is in the effect of the new added features, interested friends can download the source to see
A lot of friends under the demo found conversion to Dalvik format failed:unable to execute dex:multiple Dex files define Lcom/google/zxing/barco Deformat: This mistake, because I put two jars in the beginning, delete one on the line, we modify
SOURCE download
Android scans the QR code in your phone based on Google zxing