Android ZBar bar code reader二维码

今天研究了一下,Android中怎么实现二维码的扫描功能,在网上查了一下资料,找到一个简单方便的库ZBar bar code reader。首先要添加ZBar bar库zbar.jar,如图所示

Android ZBar bar code reader二维码_第1张图片

  添加权限:

[html]  view plain  copy
  1. <uses-permission android:name="android.permission.CAMERA" />  
  2.     <uses-feature android:name="android.hardware.camera" />  
  3.     <uses-feature android:name="android.hardware.camera.autofocus" />  


  定义一个类继承SurfaceView来预览二维码,代码:

[java]  view plain  copy
  1. /** A basic Camera preview class */  
  2. public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {  
  3.     private SurfaceHolder mHolder;  
  4.     private Camera mCamera;  
  5.     private PreviewCallback previewCallback;  
  6.     private AutoFocusCallback autoFocusCallback;  
  7.   
  8.     public CameraPreview(Context context, Camera camera,  
  9.                          PreviewCallback previewCb,  
  10.                          AutoFocusCallback autoFocusCb) {  
  11.         super(context);  
  12.         mCamera = camera;  
  13.         previewCallback = previewCb;  
  14.         autoFocusCallback = autoFocusCb;  
  15.   
  16.         /*  
  17.          * Set camera to continuous focus if supported, otherwise use 
  18.          * software auto-focus. Only works for API level >=9. 
  19.          */  
  20.         /* 
  21.         Camera.Parameters parameters = camera.getParameters(); 
  22.         for (String f : parameters.getSupportedFocusModes()) { 
  23.             if (f == Parameters.FOCUS_MODE_CONTINUOUS_PICTURE) { 
  24.                 mCamera.setFocusMode(Parameters.FOCUS_MODE_CONTINUOUS_PICTURE); 
  25.                 autoFocusCallback = null; 
  26.                 break; 
  27.             } 
  28.         } 
  29.         */  
  30.   
  31.         // Install a SurfaceHolder.Callback so we get notified when the  
  32.         // underlying surface is created and destroyed.  
  33.         mHolder = getHolder();  
  34.         mHolder.addCallback(this);  
  35.   
  36.         // deprecated setting, but required on Android versions prior to 3.0  
  37.         mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);  
  38.     }  
  39.   
  40.     public void surfaceCreated(SurfaceHolder holder) {  
  41.         // The Surface has been created, now tell the camera where to draw the preview.  
  42.         try {  
  43.             mCamera.setPreviewDisplay(holder);  
  44.         } catch (IOException e) {  
  45.             Log.d("DBG""Error setting camera preview: " + e.getMessage());  
  46.         }  
  47.     }  
  48.   
  49.     public void surfaceDestroyed(SurfaceHolder holder) {  
  50.         // Camera preview released in activity  
  51.     }  
  52.   
  53.     public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {  
  54.         /* 
  55.          * If your preview can change or rotate, take care of those events here. 
  56.          * Make sure to stop the preview before resizing or reformatting it. 
  57.          */  
  58.         if (mHolder.getSurface() == null){  
  59.           // preview surface does not exist  
  60.           return;  
  61.         }  
  62.   
  63.         // stop preview before making changes  
  64.         try {  
  65.             mCamera.stopPreview();  
  66.         } catch (Exception e){  
  67.           // ignore: tried to stop a non-existent preview  
  68.         }  
  69.   
  70.         try {  
  71.             // Hard code camera surface rotation 90 degs to match Activity view in portrait  
  72.             mCamera.setDisplayOrientation(90);  
  73.   
  74.             mCamera.setPreviewDisplay(mHolder);  
  75.             mCamera.setPreviewCallback(previewCallback);  
  76.             mCamera.startPreview();  
  77.             mCamera.autoFocus(autoFocusCallback);  
  78.         } catch (Exception e){  
  79.             Log.d("DBG""Error starting camera preview: " + e.getMessage());  
  80.         }  
  81.     }  
  82. }  

  主Activity的代码:

[java]  view plain  copy
  1. /* Import ZBar Class files */  
  2.   
  3. public class CameraTestActivity extends Activity  
  4. {  
  5.     private Camera mCamera;  
  6.     private CameraPreview mPreview;  
  7.     private Handler autoFocusHandler;  
  8.   
  9.     TextView scanText;  
  10.     Button scanButton;  
  11.   
  12.     ImageScanner scanner;  
  13.   
  14.     private boolean barcodeScanned = false;  
  15.     private boolean previewing = true;  
  16.   
  17.     static {  
  18.         System.loadLibrary("iconv");  
  19.     }   
  20.   
  21.     public void onCreate(Bundle savedInstanceState) {  
  22.         super.onCreate(savedInstanceState);  
  23.   
  24.         setContentView(R.layout.main);  
  25.   
  26.         setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);  
  27.   
  28.         autoFocusHandler = new Handler();  
  29.         mCamera = getCameraInstance();  
  30.   
  31.         /* Instance barcode scanner */  
  32.         scanner = new ImageScanner();  
  33.         scanner.setConfig(0, Config.X_DENSITY, 3);  
  34.         scanner.setConfig(0, Config.Y_DENSITY, 3);  
  35.   
  36.         mPreview = new CameraPreview(this, mCamera, previewCb, autoFocusCB);  
  37.         FrameLayout preview = (FrameLayout)findViewById(R.id.cameraPreview);  
  38.         preview.addView(mPreview);  
  39.   
  40.         scanText = (TextView)findViewById(R.id.scanText);  
  41.   
  42.         scanButton = (Button)findViewById(R.id.ScanButton);  
  43.   
  44.         scanButton.setOnClickListener(new OnClickListener() {  
  45.                 public void onClick(View v) {  
  46.                     if (barcodeScanned) {  
  47.                         barcodeScanned = false;  
  48.                         scanText.setText("Scanning...");  
  49.                         mCamera.setPreviewCallback(previewCb);  
  50.                         mCamera.startPreview();  
  51.                         previewing = true;  
  52.                         mCamera.autoFocus(autoFocusCB);  
  53.                     }  
  54.                 }  
  55.             });  
  56.     }  
  57.   
  58.     public void onPause() {  
  59.         super.onPause();  
  60.         releaseCamera();  
  61.     }  
  62.   
  63.     /** A safe way to get an instance of the Camera object. */  
  64.     public static Camera getCameraInstance(){  
  65.         Camera c = null;  
  66.         try {  
  67.             c = Camera.open();  
  68.         } catch (Exception e){  
  69.         }  
  70.         return c;  
  71.     }  
  72.   
  73.     private void releaseCamera() {  
  74.         if (mCamera != null) {  
  75.             previewing = false;  
  76.             mCamera.setPreviewCallback(null);  
  77.             mCamera.release();  
  78.             mCamera = null;  
  79.         }  
  80.     }  
  81.   
  82.     private Runnable doAutoFocus = new Runnable() {  
  83.             public void run() {  
  84.                 if (previewing)  
  85.                     mCamera.autoFocus(autoFocusCB);  
  86.             }  
  87.         };  
  88.   
  89.     PreviewCallback previewCb = new PreviewCallback() {  
  90.             public void onPreviewFrame(byte[] data, Camera camera) {  
  91.                 Camera.Parameters parameters = camera.getParameters();  
  92.                 Size size = parameters.getPreviewSize();  
  93.   
  94.                 Image barcode = new Image(size.width, size.height, "Y800");  
  95.                 barcode.setData(data);  
  96.   
  97.                 int result = scanner.scanImage(barcode);  
  98.                   
  99.                 if (result != 0) {  
  100.                     previewing = false;  
  101.                     mCamera.setPreviewCallback(null);  
  102.                     mCamera.stopPreview();  
  103.                       
  104.                     SymbolSet syms = scanner.getResults();  
  105.                     for (Symbol sym : syms) {  
  106.                         scanText.setText("barcode result " + sym.getData());  
  107.                         barcodeScanned = true;  
  108.                     }  
  109.                 }  
  110.             }  
  111.         };  
  112.   
  113.     // Mimic continuous auto-focusing  
  114.     AutoFocusCallback autoFocusCB = new AutoFocusCallback() {  
  115.             public void onAutoFocus(boolean success, Camera camera) {  
  116.                 autoFocusHandler.postDelayed(doAutoFocus, 1000);  
  117.             }  
  118.         };  
  119. }  

  以上就是使用ZBar库来扫描二维码。

 详细资料:http://sourceforge.net/p/zbar/news/2012/03/zbar-android-sdk-version-01-released/。

你可能感兴趣的:(android,二维码)