标签:
首先,我们先把zxing的源代码给下载下来,这个网上有很多,我下载的是2.3的,不得不说这个谷歌提供的包包含的功能还是很全面的。
public static Camera open() { // 获取摄像头的数量 int numCameras = Camera.getNumberOfCameras(); //如果没有找到相机则退出 if (numCameras == 0) { Log.w(TAG, "No cameras!"); return null; } int index = 0; while (index < numCameras) { //初始化相机信息类 Camera.CameraInfo cameraInfo = new Camera.CameraInfo(); //获取相机的信息 Camera.getCameraInfo(index, cameraInfo); //判断是否是后置摄像头 if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK) { break; } index++; } //打开相机 Camera camera; if (index < numCameras) { Log.i(TAG, "Opening camera #" + index); camera = Camera.open(index); } else { Log.i(TAG, "No camera facing back; returning camera #0"); camera = Camera.open(0); } return camera; }
从这个方法中把摄像头打开
void initFromCameraParameters(Camera camera) { //获取相机的参数 Camera.Parameters parameters = camera.getParameters(); WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); Display display = manager.getDefaultDisplay(); //构造一个点 Point theScreenResolution = new Point(); //给点进行赋值,屏幕的宽和高 Resolution分辨率的意思 display.getSize(theScreenResolution); screenResolution = theScreenResolution; Log.i(TAG, "Screen resolution: " + screenResolution); cameraResolution = findBestPreviewSizeValue(parameters, screenResolution); Log.i(TAG, "Camera resolution: " + cameraResolution); }
public synchronized void openDriver(SurfaceHolder holder) throws IOException { Camera theCamera = camera; if (theCamera == null) { theCamera = OpenCameraInterface.open(); if (theCamera == null) { throw new IOException(); } camera = theCamera; } //设置摄像头预览功能 theCamera.setPreviewDisplay(holder); //初始化执行的操作 if (!initialized) { initialized = true; //初始化相机的参数,选择最佳的预览分辨率 configManager.initFromCameraParameters(theCamera); if (requestedFramingRectWidth > 0 && requestedFramingRectHeight > 0) { setManualFramingRect(requestedFramingRectWidth, requestedFramingRectHeight); requestedFramingRectWidth = 0; requestedFramingRectHeight = 0; } } Camera.Parameters parameters = theCamera.getParameters(); String parametersFlattened = parameters == null ? null : parameters.flatten(); // Save these, temporarily try { //设置必要的参数,包括焦点,闪光灯等 configManager.setDesiredCameraParameters(theCamera, false); } catch (RuntimeException re) { // Driver failed Log.w(TAG, "Camera rejected parameters. Setting only minimal safe-mode parameters"); Log.i(TAG, "Resetting to saved camera params: " + parametersFlattened); // Reset: if (parametersFlattened != null) { parameters = theCamera.getParameters(); parameters.unflatten(parametersFlattened); try { theCamera.setParameters(parameters); configManager.setDesiredCameraParameters(theCamera, true); } catch (RuntimeException re2) { // Well, darn. Give up Log.w(TAG, "Camera rejected even safe-mode parameters! No configuration"); } } } }
<SurfaceView android:id="@+id/preview_view" android:layout_width="fill_parent" android:layout_height="fill_parent"/> <com.google.zxing.client.android.ViewfinderView android:id="@+id/viewfinder_view" android:layout_width="fill_parent" android:layout_height="fill_parent" android:visibility="gone" />
标签:
原文地址:http://www.cnblogs.com/zhangjin055/p/4692609.html