The use of open source Zxing library, on Android, two-dimensional code simple coding and decoding __ two-dimensional code

Source: Internet
Author: User
Tags addall

The first is simple coding, the code is as follows

/**
	 * Two-dimensional encoding based on strings *
	 @param str   needs to encode the string
	 * @param widthandheight the  height of the bitmap that needs to be generated
	 @return
	 *
	/Public Bitmap enCode (String str,int widthandheight) {
	
			
			if (!str.equals ("") &&str!=null)
				try {return
					encodinghandler.createqrcode (str, widthandheight);
				} catch (Writerexception e) {
					//TODO Auto-generated Catch block
					e.printstacktrace ();
				}
			else
				try {return
					Encodinghandler.createqrcode ("null", Widthandheight);
				} catch (Writerexception e) { c19/>//TODO auto-generated Catch block
					e.printstacktrace ();
				}
			return null;
	}

Again, is the simple decoding of the code as follows

	Public String DeCode (Bitmap Bitmap) {if (Bitmap ==null) {log.i ("DeCode", "-----------------------null");
		return "NULL";
		} hashtable<decodehinttype, object> hints = null;
		Inithints (hints, NULL, "UTF8");
		Multiformatreader Multiformatreader = new Multiformatreader ();


		Multiformatreader.sethints (hints);
		
		Luminancesource Source = new Bitmapluance (bitmap);
	
		Binarybitmap bit = new Binarybitmap (new Hybridbinarizer (source));

		try {return multiformatreader.decodewithstate (bit). GetText ();
		The catch (Readerexception re) {//Continue} finally {Multiformatreader.reset ();
	return null; } public void Inithints (Hashtable<decodehinttype, object> hints, vector<barcodeformat> decodeFormats, STR
		ing code_style) {hints = new Hashtable<decodehinttype, object> (2);
			if (decodeformats = null | | decodeformats.isempty ()) {decodeformats = new vector<barcodeformat> (); Decodeformats.addall (mydecodeformatmanager.one_d_forMATS);
			Decodeformats.addall (Mydecodeformatmanager.qr_code_formats);
		Decodeformats.addall (Mydecodeformatmanager.data_matrix_formats);
		} hints.put (Decodehinttype.possible_formats, decodeformats);
		if (Code_style!= null) {//hints.put (Decodehinttype.character_set, Code_style);
		} private Bitmap Filetobitmap (String imgpath) {Bitmap BM = bitmapfactory.decodefile (Imgpath);
	return BM; }

Click here to download the sample source code

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: info-contact@alibabacloud.com 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.