/** 开关灯 */ private void operateLight() { if (!mIsLightOpen) { cameraManager.openLight(); mIsLightOpen = true; mLightBtn.setImageResource(R.drawable.light_pressed); } else { cameraManager.offLight(); mIsLightOpen = false; mLightBtn.setImageResource(R.drawable.light_normal); } }
/** 关灯 */ private void offLight() { if (mIsLightOpen) { cameraManager.offLight(); mIsLightOpen = false; mLightBtn.setImageResource(R.drawable.light_normal); } }
private void initCamera(SurfaceHolder surfaceHolder) { if (surfaceHolder == null) { throw new IllegalStateException("No SurfaceHolder provided"); } if (cameraManager.isOpen()) { Log.w(LOG_TAG, "initCamera() while already open -- late SurfaceView callback?"); return; } try { cameraManager.openDriver(surfaceHolder); // Creating the handler starts the preview, which can also throw a // RuntimeException. if (handler == null) { handler = new CaptureActivityHandler(this, cameraManager, DecodeThread.ALL_MODE); } initCrop(); } catch (IOException ioe) { Log.w(LOG_TAG, ioe); displayFrameworkBugMessageAndExit(); } catch (RuntimeException e) { // Barcode Scanner has seen crashes in the wild of this variety: // java.?lang.?RuntimeException: Fail to connect to camera service Log.w(LOG_TAG, "Unexpected error initializing camera", e); displayFrameworkBugMessageAndExit(); } }
@Override protected void onPause() { if (handler != null) { handler.quitSynchronously(); handler = null; } inactivityTimer.onPause(); beepManager.close(); cameraManager.closeDriver(); if (!isHasSurface) { scanPreview.getHolder().removeCallback(this); } super.onPause(); }
/** 初始化截取的矩形区域 */ @SuppressWarnings("SuspiciousNameCombination") private void initCrop() { // http://skillcollege.github.io/2015/02/05/-打造极致二维码扫描系列-基于ZBar的Android平台解码/ // 预览图的高度,也即camera的分辨率宽高 int cameraWidth = cameraManager.getCameraResolution().y; int cameraHeight = cameraManager.getCameraResolution().x; /** 获取布局中扫描框的位置信息 */ int[] location = new int[2]; scanCropView.getLocationInWindow(location); // 布局文件中扫描框的左上角定点坐标 int cropLeft = location[0]; int cropTop = location[1] - getStatusBarHeight(); // 布局文件中扫描框的宽高 int cropWidth = scanCropView.getWidth(); int cropHeight = scanCropView.getHeight(); /** 获取布局容器的宽高 */ int containerWidth = scanContainer.getWidth(); int containerHeight = scanContainer.getHeight(); /** 计算最终截取的矩形的左上角顶点x坐标 */ int x = cropLeft * cameraWidth / containerWidth; /** 计算最终截取的矩形的左上角顶点y坐标 */ int y = cropTop * cameraHeight / containerHeight; /** 计算最终截取的矩形的宽度 */ int width = cropWidth * cameraWidth / containerWidth; /** 计算最终截取的矩形的高度 */ int height = cropHeight * cameraHeight / containerHeight; /** 生成最终的截取的矩形 */ mCropRect = new Rect(x, y, width + x, height + y); }