码迷,mamicode.com
首页 > 其他好文 > 详细

CameraService

时间:2015-02-11 21:58:29      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:

以前在开发中遇到过一个项目,需要调用手机的拍照,并把照片传过去。但是拍照时手机不能让用户看到相机打开。

思路是启动一个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) {

}





CameraService

标签:

原文地址:http://blog.csdn.net/lianchen/article/details/43741335

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!