Android QR code tool zxing use

Source: Internet
Author: User

二维码在我们生活中随处可见。在我眼里简直能够用“泛滥”来形容啦。那怎样在我们Android项目中扫描识别二维码或生成二维码图片呢?我们通常使用的开源框架是zxing。在github上的开源地址:https://github.com/zxing/zxing,眼下在做的项目中也用到这个框架,     所以自己做了个demo,方便学习及下次使用。

Identify QR code

 /** * Scan QR Code demo sample */ Public  class scancodeactivity extends Activity implements View. Onclicklistener {    PrivateButton Start_scan;PrivateTextView Result_tv;Private Final Static intRequest_code = -;@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_scan); This. Start_scan = (Button) Findviewbyid (R.id.start_scan); This. Result_tv = (TextView) Findviewbyid (R.ID.RESULT_TV); This. Start_scan.setonclicklistener ( This); }@Override     Public void OnClick(View v) {if(V.getid () = = R.id.start_scan) {The//zxing framework has helped us encapsulate the corresponding implementation class. Call Startactivityforresult directly toIntent Intent =NewIntent ( This, Captureactivity.class);        Startactivityforresult (Intent, Request_code); }    }/** * Processing data in Onactivityresult * @param requestcode * @param resultcode * @param 
       Data * /    @Override    protected void Onactivityresult(intRequestcode,intResultCode, Intent data) {if(ResultCode = = Result_ok && Requestcode = = Request_code)            {Bundle bundle = Data.getextras (); String Scanresult = bundle.getstring ("Result"); This. Result_tv.settext (Scanresult); }Super. Onactivityresult (Requestcode, ResultCode, data); }}

Generate two-dimensional code:

/** * Generate two-dimensional Code demo sample * / Public  class madecodeactivity extends Activity implements View . Onclicklistener {    PrivateEditText Code_edt;PrivateButton Made_code;PrivateImageView Result_iv;PrivateCheckBox LOGO_CB;@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_made); This. Code_edt = (EditText) Findviewbyid (R.id.code_edt); This. Made_code = (Button) Findviewbyid (R.id.made_code); This. Result_iv = (ImageView) Findviewbyid (R.ID.RESULT_IV); LOGO_CB = (CheckBox) Findviewbyid (R.ID.LOGO_CB); This. Made_code.setonclicklistener ( This); }@Override     Public void OnClick(View v) {if(V.getid () = = R.id.made_code) {String content = Code_edt.gettext (). toString (). Trim ();BooleanIsaddlogo = logo_cb.ischecked ();if(Textutils.isempty (content)) {Toast.maketext ( This,"Input cannot be empty", Toast.length_long). Show ();return;        } madecode (content, Isaddlogo); }    }/** * Generate QR Code * * @param content to generate text contents of a picture * @param Isaddlogo Whether to add logo image in QR code * /    Private void Madecode(String content,BooleanIsaddlogo) {String bitmappath;if(!isaddlogo) {//Without logoBitmappath = Encodeutil.createqrimage ( This, Content, -, -,NULL); }Else{//With Logo,r.mipmap.ic_launcher is the logo corresponding image, to joinBitmappath = Encodeutil.createqrimage ( This, Content, -, -, Bitmapfactory.decoderesource (Getresources (), r.mipmap.ic_launcher)); }if(!        Textutils.isempty (Bitmappath)) {Result_iv.setimagebitmap (Bitmapfactory.decodefile (BitmapPath)); }    }}

Completed project (Androidstudio project):
http://download.csdn.net/detail/true100/9487162

Android QR code tool zxing use

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.