Android image QR code identification and preservation (i)

Source: Internet
Author: User

The latest business development QR code recognition function, this function, in many applications, such as long press image identification QR code, if there is a recognizable QR code in the picture, you can add an option to identify the two-dimensional code. So how to implement this function. Here is actually very simple, the first two-dimensional code to identify the image, the recognition results returned when the judge whether there are two-dimensional code, there is an increase in the identification of two-dimensional code options.

For the identification of two-dimensional code, the current mainstream is zxing and Zbar, for the selection of the two, generally mobile smartphones more choice zxing, because zxing more suitable, zbar suitable for embedded hardware, both for the QR graphics code ability is similar, but zxing more information significantly. You have to adjust according to your own situation.

The two-dimensional barcode/two-D code (2-dimensional bar code) is a black-and-white graphic that distributes data symbols in a plane (two-dimensional direction) in a certain geometric pattern.
The concept of "0" and "1" bitstream, which is the basis of the internal logic of the computer, is skillfully used to represent the literal numerical information using several geometric shapes corresponding to the binary. Automatic reading through image input devices or photoelectric scanning devices to automate information processing: It has some common features of bar code technology: Each code system has its own specific character set.
Each character occupies a certain width, has a certain calibration function and so on. At the same time, it also has the function of information automatic recognition for different lines and the change point of processing graphics rotation.

The greater the amount of information on the two-dimensional code graphics point, more to a certain degree of identification is difficult, two-dimensional code has a certain ability to correct, but not omnipotent.

Firstly, the realization of recognition QR code is given, and the two-dimensional code identifies a set of data.

 /*** encode the QR code image decoding result * *@paramSTR *@return     */     Public Staticstring Recode (String str) {string Formart= ""; Try {            BooleanISO = Charset.forname ("Iso-8859-1"). Newencoder (). Canencode (str); if(ISO) {Formart=NewString (Str.getbytes ("iso-8859-1"), "UTF-8"); } Else{Formart=str; }        } Catch(unsupportedencodingexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(Exception e) {}returnFormart; }

Secondly, the code generated by the QR code is as follows:

 Public StaticResult Handleqrcodeformbitmap (Bitmap Bitmap) {Map<decodehinttype, object> hints =NewEnummap<> (Decodehinttype.class); Hints.put (Decodehinttype.character_set,"Utf-8");        Hints.put (Decodehinttype.try_harder, boolean.true);        Hints.put (Decodehinttype.possible_formats, Barcodeformat.qr_code); Rgbluminancesource Source=NewRgbluminancesource (bitmap); Binarybitmap Bitmap1=NewBinarybitmap (NewHybridbinarizer (source)); Qrcodereader Reader2=NewQrcodereader (); Result result=NULL; Try{result=Reader2.decode (BITMAP1, hints); } Catch(Exception e) {e.printstacktrace (); if(Source! =NULL) {Binarybitmap bitmap2=NewBinarybitmap (NewGlobalhistogrambinarizer (source)); Try{result=Reader2.decode (BITMAP2, hints); } Catch(Exception E1) {e1.printstacktrace (); }            }        }        returnresult; }

The returned result is processed and if it is not recognized, no QR code option is identified in the box.

But for the picture will have the function of saving, the picture hand carries on the name, then sets the save path, creates the good path, finally saves the picture to the file, according to need to compress, may set the correlation parameter. Save the notification system to update the picture library later.

The code to save the picture is as follows:

 Public Static voidSaveimagetogallery (context context, Bitmap bmp) {String fileName= System.currenttimemillis () + ". jpg"; String FilePath= Fileutils.getfilepath (Context, "/pictures/qrcode/"); //Save the picture firstFile Appdir =NewFile (FilePath); if(!appdir.exists ())        {Appdir.mkdir (); } File File=NewFile (Appdir, fileName); Try{FileOutputStream fos=Newfileoutputstream (file); Bmp.compress (Bitmap.CompressFormat.JPEG,100, FOS);            Fos.flush ();        Fos.close (); } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } Catch(Exception e) {}//then insert the file into the system GalleryContentvalues values =Newcontentvalues ();        Values.put (MediaStore.Images.Media.DATA, File.getabsolutepath ()); Values.put (MediaStore.Images.Media.MIME_TYPE,"Image/jpeg"); Uri URI=context.getcontentresolver (). Insert (MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); //finally notify the gallery updateContext.sendbroadcast (NewIntent (Intent.action_media_scanner_scan_file, Uri.fromfile (FILE))); }

Here the QR code identification and image preservation functions are basically completed.

However, the actual test found that this approach is functional, does not take into account performance, multiple operations resulting in memory usage increased by 30%, the next article will describe the analysis process and solutions.

Android image QR code identification and preservation (i)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.