QR Codes are a great way to share information with mobile phones. Originally used to track car parts, QR codes are now used to quickly and easily spread information to mobile users. For example a company can put a QR code in a magazine that contains the URL to their website. Instead of the user having to type in a long URL on their phone they can simply take a picture of the QR Code with their camera phone and have their decoding software open the link for them. For more uses of QR Codes check out this blog.

I am going to go through a quick example of encoding and decoding QR codes in Android.

Encoding

There are a number of services and libraries available for encoding QR Codes. The actual encoding process is a bit more involved than I would like to go through for this entry and we generally would not want to do the actual encoding on the client. Instead we will harness the power of the “cloud” to do the heavy lifting for us. In this case we will use the Google Charts API which just recently expanded to include QR codes. The API allows for a number of options such as size, error correction type, encoding type, and margins (the general rule for QR Codes is to have a 4 column/row white border around the barcode so that the decoding software can get a better “lock” on it)

The code below makes an http request to the Google Charts API with the URL encoded text we entered in the EditText view and populates our ImageView with the resulting image.

view plain copy to clipboard print ?
  1. encodeButton.setOnClickListener(newOnClickListener(){
  2. publicvoidonClick(Viewarg0){
  3. img.setImageBitmap(QR.this.encodeString(edit.getText().toString()));
  4. }
  5. });
  6. privateBitmapencodeString(Stringinput){
  7. URLaURL;
  8. try{
  9. aURL=newURL("http://chart.apis.google.com/chart?chs=300x300&cht=qr&choe=UTF-8chl="+URLEncoder.encode(input,"UTF-8"));
  10. URLConnectionconn=aURL.openConnection();
  11. conn.connect();
  12. InputStreamis=conn.getInputStream();
  13. BufferedInputStreambis=newBufferedInputStream(is);
  14. Bitmapbm=BitmapFactory.decodeStream(bis);
  15. bis.close();
  16. is.close();
  17. returnbm;
  18. }catch(MalformedURLExceptione){
  19. e.printStackTrace();
  20. }catch(IOExceptione){
  21. e.printStackTrace();
  22. }
  23. returnnull;
  24. }

Decoding

The ZXing library is an open source Java library for decoding numerous 1D and 2D barcodes. We will be using the core library but there is also an Android specific application that provides hooks into the camera. Since the emulator does not support capturing images just yet we will settle for using the image we just encoded. We will need one class specific to the Android ZXing library, RGBMonochromeBitmapSource which lets us convert Android bitmaps to the format that ZXing uses.

The code below takes the bitmap image from the ImageView and pushes it through the decoder populating a TextView.

view plain copy to clipboard print ?
  1. decodeButton.setOnClickListener(newOnClickListener(){
  2. publicvoidonClick(Viewarg0){
  3. try{
  4. tv.setText(((QR.this.decode(img.getDrawingCache()))));
  5. }catch(ReaderExceptione){
  6. e.printStackTrace();
  7. }
  8. }
  9. });
  10. privateStringdecode(Bitmapbm)throwsReaderException{
  11. QRCodeReaderreader=newQRCodeReader();
  12. Resultr=reader.decode(newRGBMonochromeBitmapSource(bm));
  13. returnr.getText();
  14. }

We can use our friend Linkify to automatically make any text of the correct format (http://, mailto:, and even map locations) into a link that launches the appropriate application.

view plain copy to clipboard print ?
  1. textView.setAutoLinkMask(Linkify.ALL);


QR Codes Made Easy In Android

I have even been experimenting with integrating the decoder with the built in WebView class which lets us decode images directly from the web browser. As you can see in this example I have clicked on a QR Code and a dialog pops up with the decoded information.

QR Codes Made Easy In AndroidQR Codes Made Easy In Android

The code to do this is a little hackish but involves using the WebView.requestImageRef(msg) mechanism and calling decode from the handler that receives that message.

I know of a number of decoders being developed for android but hopefully there will be a standard one that can be called using Intents so that developers can just fire off an intent in their application and get text back, not having to worry about dealing with the camera or the decoder libraries.

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. Android(安卓)RecyclerView(和SnapHelper
  2. Android中WebView使用html,且实现android
  3. The method setOnClickListener(View.OnC
  4. android2.0+通讯簿查询详解
  5. android中获得屏幕、视图、任务栏、状态
  6. Ubuntu通过MTP访问Android设备
  7. APK安装路径移动至外部存储设备
  8. android -------- 流式布局,支持单选、多
  9. Android中如何将dp,dip,sp与px相互转化
  10. Android之对TabActivity的见解,个人觉得不