标签:
以前在开发中遇到过一个项目,需要调用手机的拍照,并把照片传过去。但是拍照时手机不能让用户看到相机打开。
思路是启动一个service,service中实现一个surfaceview,surfaceview是一个像素的点,这样就防止了用户看到相机被打开。
先看surfaceview的实现:
private void makeAndAddSurfaceView() { SurfaceView dummyView = new SurfaceView(this.mApplication); SurfaceHolder holder = dummyView.getHolder(); holder.addCallback(this); holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); WindowManager wm = (WindowManager)this.mApplication.getSystemService(Context.WINDOW_SERVICE); WindowManager.LayoutParams params = new WindowManager.LayoutParams(1, 1, WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE, PixelFormat.TRANSPARENT); params.gravity = Gravity.TOP | Gravity.RIGHT; params.alpha = PixelFormat.TRANSPARENT; params.x = params.y = mApplication.getResources().getDimensionPixelOffset( R.dimen.preview_surface_offset); wm.addView(dummyView, params); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { dummyView.setAlpha(PixelFormat.TRANSPARENT); } //dummyView.getBackground().setAlpha(PixelFormat.TRANSPARENT); mDummySurfaceView = dummyView; }
等surfaceview创建成功后,调用相机的打开,相机的打开是Camera的静态函数 Camera.open()
打开camera后,可以设置previe的fps,大小,类型等。
实现如下:
private void startRecorder() throws IOException { // if (checkCameraHardware(this.mApplication)) { if (mCameraId < 0) { findFirstBackFacingCamera(); } if (mCamera == null || !isStarted()) { try { mCamera = Camera.open(mCameraId); } catch (RuntimeException exception) { synchronized (cameraLock) { mRecordingState = CameraState.NOT_RECORDING; cameraLock.notifyAll(); } return; } } // } if (mCamera == null) { return;// throw new // UnsupportedOperationException("No camera present"); } mCamera.setPreviewDisplay(mDummySurfaceHolder); Camera.Parameters params = mCamera.getParameters(); List<Size> supportSizes = mCamera.getParameters() .getSupportedPictureSizes(); int format = params.getPreviewFormat(); Size size = params.getPreviewSize(); int[] CurPreRange = new int[2]; params.getPreviewFpsRange(CurPreRange); params.setPreviewFpsRange(CurPreRange[0], CurPreRange[1]); // params.setPictureFormat(ImageFormat.JPEG); if (isBackCamera && DEFAULT_ORIENTATION != -1) { params.setRotation(DEFAULT_ORIENTATION); } else if (DEFAULT_ORIENTATION != -1) { params.setRotation(360 - DEFAULT_ORIENTATION); } Size targetSize = getTargetPreviewSize(supportSizes); if (targetSize != null) { params.setPictureSize(targetSize.width, targetSize.height); } try { mCamera.setParameters(params); } catch (Exception e) { logger.fine(e.getMessage()); } int[] frameRates = new int[2]; Log.w(TAG, "Camera properties: bpp=" + ImageFormat.getBitsPerPixel(format) + "; format=" + format + "; size=" + size.width + "x" + size.height + "; frameRates=" + frameRates[0] + "-" + frameRates[1]); mCamera.setPreviewCallback(mJpegPreviewCallback); try { mCamera.startPreview(); // Recording is now started } catch (RuntimeException e) { stopCameraInternal(); throw e; } mRecordingState = CameraState.RECORDING; }
相机的回调如下:这回调里,就可以拿到相机的数据,根据需要响应的处理了。
private PreviewCallback mJpegPreviewCallback = new Camera.PreviewCallback() {
public void onPreviewFrame(byte[] data, Camera camera) {
}
标签:
原文地址:http://blog.csdn.net/lianchen/article/details/43741335