Start the camera in Android service and Android service camera

Source: Internet
Author: User

Start the camera in Android service and Android service camera

I have encountered a project in development before. I need to call a mobile phone to take a photo and upload the photo. However, when taking the photo, the mobile phone cannot let the user see the camera open.

The idea is to start a service and implement a surfaceview in the service. surfaceview is a pixel point, which prevents users from seeing the camera open.

First, let's look at the implementation of 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;    }


After surfaceview is created, the Camera is called to open the Camera. The Camera is opened by the static function Camera. open () of Camera ()


After turning on camera, you can set the fps, size, and type of previe.


The implementation is as follows:


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;}


The callback of the camera is as follows: In this callback, you can get the camera data and process the response as needed.


Private previewcallbackmcallpreviewcallback = newCamera. PreviewCallback (){

PublicvoidonPreviewFrame (byte [] data, Camera camera ){

}





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.