Android 33: Guide to android. hardware. camera2

Source: Internet
Author: User

Android 33: Guide to android. hardware. camera2

In API 21, the original camera API is discarded and the new camera2 API is recommended. This is a big action because the new API architecture makes it more difficult for developers to use it.
Let's take a look at the camera2 package architecture:

The concept of pipelines is referenced here to connect Android devices and cameras. The system sends a Capture request to the camera, and the camera returns CameraMetadata. All of this is based on a session called CameraCaptureSession.

The following are the main classes in the camera2 package:

CameraManager is the manager of all camera devices (CameraDevice) standing on the height, and each CameraDevice is responsible for establishing CameraCaptureSession and CaptureRequest. CameraCharacteristics is the attribute description class of CameraDevice. If you have to make a comparison, it is similar to the original CameraInfo.
The class diagram has three important callback. Although this increases the difficulty of reading the code, you must get used to it because it is the style of the new package. Among them, CameraCaptureSession. CaptureCallback will process the preview and Image taking work, which must be focused on.

How do these classes work together? The following is a simple flowchart.

I use SurfaceView as the Display object (of course, TextureView can also be used for display. For details, refer to the project in reference)
The core code is as follows:

        mCameraManager = (CameraManager) this.getSystemService(Context.CAMERA_SERVICE);        mSurfaceView = (SurfaceView)findViewById(R.id.surfaceview);        mSurfaceHolder = mSurfaceView.getHolder();        mSurfaceHolder.addCallback(new SurfaceHolder.Callback() {            @Override            public void surfaceCreated(SurfaceHolder holder) {                initCameraAndPreview();            }        });
    private void initCameraAndPreview() {        Log.d(linc,init camera and preview);        HandlerThread handlerThread = new HandlerThread(Camera2);        handlerThread.start();        mHandler = new Handler(handlerThread.getLooper());        try {            mCameraId = +CameraCharacteristics.LENS_FACING_FRONT;            mImageReader = ImageReader.newInstance(mSurfaceView.getWidth(), mSurfaceView.getHeight(),                    ImageFormat.JPEG,/*maxImages*/7);            mImageReader.setOnImageAvailableListener(mOnImageAvailableListener, mHandler);            mCameraManager.openCamera(mCameraId, DeviceStateCallback, mHandler);        } catch (CameraAccessException e) {            Log.e(linc, open camera failed. + e.getMessage());        }    }
private CameraDevice.StateCallback DeviceStateCallback = new CameraDevice.StateCallback() {        @Override        public void onOpened(CameraDevice camera) {            Log.d(linc,DeviceStateCallback:camera was opend.);            mCameraOpenCloseLock.release();            mCameraDevice = camera;            try {                createCameraCaptureSession();            } catch (CameraAccessException e) {                e.printStackTrace();            }        }    };
    private void createCameraCaptureSession() throws CameraAccessException {        Log.d(linc,createCameraCaptureSession);        mPreviewBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);        mPreviewBuilder.addTarget(mSurfaceHolder.getSurface());        mState = STATE_PREVIEW;        mCameraDevice.createCaptureSession(                Arrays.asList(mSurfaceHolder.getSurface(), mImageReader.getSurface()),                mSessionPreviewStateCallback, mHandler);    }
private CameraCaptureSession.StateCallback mSessionPreviewStateCallback = new            CameraCaptureSession.StateCallback() {        @Override        public void onConfigured(CameraCaptureSession session) {            Log.d(linc,mSessionPreviewStateCallback onConfigured);            mSession = session;            try {                mPreviewBuilder.set(CaptureRequest.CONTROL_AF_MODE,                        CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);                mPreviewBuilder.set(CaptureRequest.CONTROL_AE_MODE,                        CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);                session.setRepeatingRequest(mPreviewBuilder.build(), mSessionCaptureCallback, mHandler);            } catch (CameraAccessException e) {                e.printStackTrace();                Log.e(linc,set preview builder failed.+e.getMessage());            }        }    };
private CameraCaptureSession.CaptureCallback mSessionCaptureCallback =            new CameraCaptureSession.CaptureCallback() {        @Override        public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request,                                       TotalCaptureResult result) {//            Log.d(linc,mSessionCaptureCallback, onCaptureCompleted);            mSession = session;            checkState(result);        }        @Override        public void onCaptureProgressed(CameraCaptureSession session, CaptureRequest request,                                        CaptureResult partialResult) {            Log.d(linc,mSessionCaptureCallback, onCaptureProgressed);            mSession = session;            checkState(partialResult);        }        private void checkState(CaptureResult result) {            switch (mState) {                case STATE_PREVIEW:                    // NOTHING                    break;                case STATE_WAITING_CAPTURE:                    int afState = result.get(CaptureResult.CONTROL_AF_STATE);                    if (CaptureResult.CONTROL_AF_STATE_FOCUSED_LOCKED == afState ||                            CaptureResult.CONTROL_AF_STATE_NOT_FOCUSED_LOCKED == afState                            || CaptureResult.CONTROL_AF_STATE_PASSIVE_FOCUSED == afState                            || CaptureResult.CONTROL_AF_STATE_PASSIVE_UNFOCUSED == afState) {                        //do something like save picture                    }                    break;            }        }    };

Press the capture button:

    public void onCapture(View view) {        try {            Log.i(linc, take picture);            mState = STATE_WAITING_CAPTURE;            mSession.setRepeatingRequest(mPreviewBuilder.build(), mSessionCaptureCallback, mHandler);        } catch (CameraAccessException e) {            e.printStackTrace();        }    }

This test uses the genemotion simulator to directly call the camera of the notebook.
The configuration diagram is as follows:

The demo interface is shown in the following figure:

 

Related Article

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.