当前位置: 代码迷 >> Android >> 在Android上应用ZXing识别条形码/二维码
  详细解决方案

在Android上应用ZXing识别条形码/二维码

热度:117   发布时间:2016-05-01 19:23:38.0
在Android上使用ZXing识别条形码/二维码

本文来自http://blog.csdn.net/hellogv/?,引用必须注明出处!

?????? 越来越多的手机具备自动对焦的拍摄功能,这也意味着这些手机可以具备条码扫描的功能.......手机具备条码扫描的功能,可以优化购物流程,快速存储电子名片(二维码)等

????? 本文使用ZXing 1.6实现条码/二维码识别。ZXing是个很经典的条码/二维码识别的开源类库,long long ago,就有开发者在J2ME上使用ZXing了,不过要支持JSR-234规范(自动对焦)的手机才能发挥其威力,而目前已经有不少Android手机具备自动对焦的功能。

本文代码运行的结果如下,使用91手机助手截图时,无法截取SurfaceView的实时图像:

本文使用了ZXing1.6的core,即把/zxing-1.6/core/下的src复制覆盖工程的src;另外还要使用到/zxing-1.6/android/下的PlanarYUVLuminanceSource.java。?

PS:/zxing-1.6/android/ 是BarcodeScanner的源码,本文程序相当于BarcodeScanner的精简版,只保留最基本的识别功能。

本文源码工程的下载地址如下:http://www.pudn.com/downloads349/sourcecode/comm/android/detail1521939.html

源码目录结果如下图,ChecksumException.java下面还有很多源文件,截图尚未列出:

本文例子必须要开摄像头和自动对焦的权限,不然启动时会报异常,所用的权限如下:

view plainprint?
  1. <uses-permission?android:name="android.permission.CAMERA"></uses-permission>??
  2. <uses-permission?android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>??
  3. <uses-feature?android:name="android.hardware.camera"?/>??
  4. <uses-feature?android:name="android.hardware.camera.autofocus"?/>??

?

main.xml源码如下,main.xml必须要用到FrameLayout才能重叠控件实现“范围框”的效果:

view plainprint?
  1. <?xml?version="1.0"?encoding="utf-8"?>??
  2. <FrameLayout?android:id="@+id/FrameLayout01"??
  3. ????android:layout_width="fill_parent"?android:layout_height="fill_parent"??
  4. ????xmlns:android="http://schemas.android.com/apk/res/android">??
  5. ????<SurfaceView?android:layout_height="fill_parent"??
  6. ????????android:id="@+id/sfvCamera"?android:layout_width="fill_parent"></SurfaceView>??
  7. ????<RelativeLayout?android:id="@+id/RelativeLayout01"??
  8. ????????android:layout_height="fill_parent"?android:layout_width="fill_parent">??
  9. ????????<ImageView?android:id="@+id/ImageView01"??
  10. ????????????android:layout_height="100dip"?android:layout_width="160dip"></ImageView>??
  11. ????????<View?android:layout_centerVertical="true"??
  12. ????????????android:layout_centerHorizontal="true"?android:layout_width="300dip"??
  13. ????????????android:background="#55FF6666"?android:id="@+id/centerView"??
  14. ????????????android:layout_height="180dip"></View>??
  15. ????????<TextView?android:layout_centerHorizontal="true"??
  16. ????????????android:layout_width="wrap_content"?android:layout_below="@+id/centerView"??
  17. ????????????android:layout_height="wrap_content"?android:text="Scanning..."??
  18. ????????????android:id="@+id/txtScanResult"?android:textColor="#FF000000"></TextView>??
  19. ????</RelativeLayout>??
  20. </FrameLayout>??

?

testCamera.java是主类,负责控制Camera和对图像做解码,源码如下:

view plainprint?
  1. package?com.testCamera;??
  2. import?java.util.Timer;??
  3. import?java.util.TimerTask;??
  4. import?com.google.zxing.BinaryBitmap;??
  5. import?com.google.zxing.MultiFormatReader;??
  6. import?com.google.zxing.Result;??
  7. import?com.google.zxing.Android.PlanarYUVLuminanceSource;??
  8. import?com.google.zxing.common.HybridBinarizer;??
  9. import?android.app.Activity;??
  10. import?android.graphics.Bitmap;??
  11. import?android.hardware.Camera;??
  12. import?android.os.Bundle;??
  13. import?android.view.SurfaceView;??
  14. import?android.view.View;??
  15. import?android.widget.ImageView;??
  16. import?android.widget.TextView;??
  17. public?class?testCamera?extends?Activity?{??
  18. ????/**?Called?when?the?activity?is?first?created.?*/??
  19. ????private?SurfaceView?sfvCamera;??
  20. ????private?SFHCamera?sfhCamera;??
  21. ????private?ImageView?imgView;??
  22. ????private?View?centerView;??
  23. ????private?TextView?txtScanResult;??
  24. ????private?Timer?mTimer;??
  25. ????private?MyTimerTask?mTimerTask;??
  26. ????//?按照标准HVGA??
  27. ????final?static?int?width?=?480;??
  28. ????final?static?int?height?=?320;??
  29. ????int?dstLeft,?dstTop,?dstWidth,?dstHeight;??
  30. ????@Override??
  31. ????public?void?onCreate(Bundle?savedInstanceState)?{??
  32. ????????super.onCreate(savedInstanceState);??
  33. ????????setContentView(R.layout.main);??
  34. ????????this.setTitle("Android条码/二维码识别Demo-----hellogv");??
  35. ????????imgView?=?(ImageView)?this.findViewById(R.id.ImageView01);??
  36. ????????centerView?=?(View)?this.findViewById(R.id.centerView);??
  37. ????????sfvCamera?=?(SurfaceView)?this.findViewById(R.id.sfvCamera);??
  38. ????????sfhCamera?=?new?SFHCamera(sfvCamera.getHolder(),?width,?height,??
  39. ????????????????previewCallback);??
  40. ????????txtScanResult=(TextView)this.findViewById(R.id.txtScanResult);??
  41. ????????//?初始化定时器??
  42. ????????mTimer?=?new?Timer();??
  43. ????????mTimerTask?=?new?MyTimerTask();??
  44. ????????mTimer.schedule(mTimerTask,?0,?80);??
  45. ????}??
  46. ??????
  47. ????class?MyTimerTask?extends?TimerTask?{??
  48. ????????@Override??
  49. ????????public?void?run()?{??
  50. ????????????if?(dstLeft?==?0)?{//只赋值一次??
  51. ????????????????dstLeft?=?centerView.getLeft()?*?width??
  52. ????????????????????????/?getWindowManager().getDefaultDisplay().getWidth();??
  53. ????????????????dstTop?=?centerView.getTop()?*?height??
  54. ????????????????????????/?getWindowManager().getDefaultDisplay().getHeight();??
  55. ????????????????dstWidth?=?(centerView.getRight()?-?centerView.getLeft())*?width??
  56. ????????????????????????/?getWindowManager().getDefaultDisplay().getWidth();??
  57. ????????????????dstHeight?=?(centerView.getBottom()?-?centerView.getTop())*?height??
  58. ????????????????????????/?getWindowManager().getDefaultDisplay().getHeight();??
  59. ????????????}??
  60. ????????????sfhCamera.AutoFocusAndPreviewCallback();??
  61. ????????}??
  62. ????}??
  63. ????/**?
  64. ?????*??自动对焦后输出图片?
  65. ?????*/??
  66. ????private?Camera.PreviewCallback?previewCallback?=?new?Camera.PreviewCallback()?{??
  67. ????????@Override??
  68. ????????public?void?onPreviewFrame(byte[]?data,?Camera?arg1)?{??
  69. ????????????//取得指定范围的帧的数据??
  70. ????????????PlanarYUVLuminanceSource?source?=?new?PlanarYUVLuminanceSource(??
  71. ????????????????????data,?width,?height,?dstLeft,?dstTop,?dstWidth,?dstHeight);??
  72. ????????????//取得灰度图??
  73. ????????????Bitmap?mBitmap?=?source.renderCroppedGreyscaleBitmap();??
  74. ????????????//显示灰度图??
  75. ????????????imgView.setImageBitmap(mBitmap);??
  76. ????????????BinaryBitmap?bitmap?=?new?BinaryBitmap(new?HybridBinarizer(source));??
  77. ????????????MultiFormatReader?reader?=?new?MultiFormatReader();??
  78. ????????????try?{??
  79. ????????????????Result?result?=?reader.decode(bitmap);??
  80. ????????????????String?strResult?=?"BarcodeFormat:"??
  81. ????????????????????????+?result.getBarcodeFormat().toString()?+?"??text:"??
  82. ????????????????????????+?result.getText();??
  83. ????????????????txtScanResult.setText(strResult);??
  84. ????????????}?catch?(Exception?e)?{??
  85. ????????????????txtScanResult.setText("Scanning");??
  86. ????????????}??
  87. ????????}??
  88. ????};??
  89. }??

?

SFHCamera.java是Camera控制类,源码如下:

view plainprint?
  1. package?com.testCamera;??
  2. import?java.io.IOException;??
  3. import?android.graphics.PixelFormat;??
  4. import?android.hardware.Camera;??
  5. import?android.util.Log;??
  6. import?android.view.SurfaceHolder;??
  7. public?class?SFHCamera?implements?SurfaceHolder.Callback{??
  8. ????private?SurfaceHolder?holder?=?null;??
  9. ????private?Camera?mCamera;??
  10. ????private?int?width,height;??
  11. ????private?Camera.PreviewCallback?previewCallback;??
  12. ??????
  13. ????public?SFHCamera(SurfaceHolder?holder,int?w,int?h,Camera.PreviewCallback?previewCallback)?{??
  14. ????????this.holder?=?holder;????
  15. ????????this.holder.addCallback(this);????
  16. ????????this.holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);??
  17. ????????width=w;??
  18. ????????height=h;??
  19. ????????this.previewCallback=previewCallback;??
  20. ????}??
  21. ??????
  22. ????@Override??
  23. ????public?void?surfaceChanged(SurfaceHolder?arg0,?int?arg1,?int?arg2,?int?arg3)?{??
  24. ????????Camera.Parameters?parameters?=?mCamera.getParameters();????
  25. ????????parameters.setPreviewSize(width,?height);//设置尺寸????
  26. ????????parameters.setPictureFormat(PixelFormat.JPEG);??
  27. ????????mCamera.setParameters(parameters);????
  28. ????????mCamera.startPreview();//开始预览??
  29. ????????Log.e("Camera","surfaceChanged");??
  30. ????}??
  31. ????@Override??
  32. ????public?void?surfaceCreated(SurfaceHolder?arg0)?{??
  33. ????????mCamera?=?Camera.open();//启动服务????
  34. ????????try?{????
  35. ????????????mCamera.setPreviewDisplay(holder);//设置预览???
  36. ????????????Log.e("Camera","surfaceCreated");??
  37. ????????}?catch?(IOException?e)?{????
  38. ????????????mCamera.release();//释放????
  39. ????????????mCamera?=?null;????
  40. ????????}??
  41. ??????????
  42. ????}??
  43. ????@Override??
  44. ????public?void?surfaceDestroyed(SurfaceHolder?arg0)?{??
  45. ????????mCamera.setPreviewCallback(null);??
  46. ????????mCamera.stopPreview();//停止预览????
  47. ????????mCamera?=?null;??
  48. ????????Log.e("Camera","surfaceDestroyed");??
  49. ????}??
  50. ????/**?
  51. ?????*?自动对焦并回调Camera.PreviewCallback?
  52. ?????*/??
  53. ????public?void?AutoFocusAndPreviewCallback()??
  54. ????{??
  55. ????????if(mCamera!=null)??
  56. ????????????mCamera.autoFocus(mAutoFocusCallBack);??
  57. ????}??
  58. ??????
  59. ????/**?
  60. ?????*?自动对焦?
  61. ?????*/??
  62. ????private?Camera.AutoFocusCallback?mAutoFocusCallBack?=?new?Camera.AutoFocusCallback()?{????
  63. ????????????
  64. ????????@Override????
  65. ????????public?void?onAutoFocus(boolean?success,?Camera?camera)?{????????
  66. ????????????if?(success)?{??//对焦成功,回调Camera.PreviewCallback??
  67. ????????????????mCamera.setOneShotPreviewCallback(previewCallback);???
  68. ????????????}????
  69. ????????}????
  70. ????};??
  71. ??????
  72. ??
  73. }??

?

其中testCamera.java的Camera.PreviewCallback previewCallback?是整个程序的逻辑核心,作为回调函数给SFHCamera.java的内部Camera类调用。

  相关解决方案