Camera preview image data processing with Android L and Camera2 API

前端 未结 5 552
失恋的感觉
失恋的感觉 2020-11-27 11:46

I\'m working on an android app that is processing the input image from the camera and displays it to the user. This is fairly simple, I register a PreviewCallback

5条回答
  •  被撕碎了的回忆
    2020-11-27 12:30

    I needed the same thing, so I used their example and added a call to a new function when the camera is in preview state.

    private CameraCaptureSession.CaptureCallback mCaptureCallback
                = new CameraCaptureSession.CaptureCallback()
        private void process(CaptureResult result) {
            switch (mState) {
                case STATE_PREVIEW: {
                        if (buttonPressed){
                            savePreviewShot();
                        }
                    break;
                }
    

    The savePreviewShot() is simply a recycled version of the original captureStillPicture() adapted to use the preview template.

       private void savePreviewShot(){
            try {
                final Activity activity = getActivity();
                if (null == activity || null == mCameraDevice) {
                    return;
                }
                // This is the CaptureRequest.Builder that we use to take a picture.
                final CaptureRequest.Builder captureBuilder =
                        mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
                captureBuilder.addTarget(mImageReader.getSurface());
    
                // Orientation
                int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
                captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, ORIENTATIONS.get(rotation));
    
                CameraCaptureSession.CaptureCallback CaptureCallback
                        = new CameraCaptureSession.CaptureCallback() {
    
                    @Override
                    public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request,
                                                   TotalCaptureResult result) {
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss:SSS");
                        Date resultdate = new Date(System.currentTimeMillis());
                        String mFileName = sdf.format(resultdate);
                        mFile = new File(getActivity().getExternalFilesDir(null), "pic "+mFileName+" preview.jpg");
    
                        Log.i("Saved file", ""+mFile.toString());
                        unlockFocus();
                    }
                };
    
                mCaptureSession.stopRepeating();
                mCaptureSession.capture(captureBuilder.build(), CaptureCallback, null);
            } catch (Exception e) {
                e.printStackTrace();
            }
        };
    

提交回复
热议问题