二维码
-
扫描区域的绘制
-
布局实现
<SurfaceView
android:id="@+id/preview_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent" /><com.google.zxing.client.android.decode.ViewfinderView
android:id="@+id/viewfinder_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
-
-
初始化相机管理器
cameraManager = new CameraManager(getApplication()); viewfinderView = (ViewfinderView) findViewById(R.id.viewfinder_view); viewfinderView.setCameraManager(cameraManager); handler = null; SurfaceView surfaceView = (SurfaceView) findViewById(R.id.preview_view); SurfaceHolder surfaceHolder = surfaceView.getHolder(); if (hasSurface) { // The activity was paused but not stopped, so the surface still // exists. Therefore // surfaceCreated() won't be called, so init the camera here. initCamera(surfaceHolder); } else { // Install the callback and wait for surfaceCreated() to init the // camera. surfaceHolder.addCallback(this); surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); }
-
解析扫描结果
public void handleDecode(Result rawResult, Bitmap barcode) { inactivityTimer.onActivity(); ResultHandler resultHandler = ResultHandlerFactory.makeResultHandler( this, rawResult); boolean fromLiveScan = barcode != null; if (fromLiveScan) { // Then not from history, so beep/vibrate and we have an image to // draw on beepManager.playBeepSoundAndVibrate(); // drawResultPoints(barcode, rawResult); viewfinderView.drawResultBitmap(barcode); } String text = rawResult.getText(); Toast.makeText(this, "扫描结果:" + text, Toast.LENGTH_LONG).show(); Log.d(TAG, "result-->" + text);
}
网友评论