从相机预览android camera2中保存图像内的图像?

时间:2018-01-21 10:42:46

标签: java android android-camera2

我的相机预览框上有一个视点矩形。现在,我的想法是仅从视点捕获图像并将其保存在我的内部存储中。我用了captureBuilder.set(CaptureRequest.SCALER_CROP_REGION, cropRect); 但由于某些原因,当我指定矩形的坐标并保存图像时,看起来图像在保存时缩小了,因此,视点内的某些部分被截断。我尝试调整x,y位置,但它似乎不起作用。可能是什么问题呢?我正在使用camera2 API。这是我的代码

 private void captureStillPicture() {
        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_STILL_CAPTURE);
            captureBuilder.addTarget(mImageReader.getSurface());

            // Use the same AE and AF modes as the preview.
            CameraManager manager = (CameraManager) getActivity().getSystemService(Context.CAMERA_SERVICE);

            CameraCharacteristics characteristics = manager.getCameraCharacteristics(mCameraDevice.getId());







            Size[] jpegSizes = null;
            if (characteristics != null) {
                jpegSizes = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP).getOutputSizes(ImageFormat.JPEG);



            }
            int width = 640;
            int height = 480;
            if (jpegSizes != null && 0 < jpegSizes.length) {
                width = jpegSizes[2].getWidth();
                height = jpegSizes[2].getHeight();
            }





            int x = width/5;
            int y = height/6;
            int topX = x + (250 / 4) ;
            int topY = y + (250 / 5);
           Rect cropRect = new Rect(topX,topY,x,y);

            captureBuilder.set(CaptureRequest.SCALER_CROP_REGION, cropRect);

            captureBuilder.set(CaptureRequest.CONTROL_AF_MODE,
                    CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
            setAutoFlash(captureBuilder);

            // Orientation
            int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
            captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, getOrientation(rotation));

            CameraCaptureSession.CaptureCallback CaptureCallback
                    = new CameraCaptureSession.CaptureCallback() {

                @Override
                public void onCaptureCompleted(@NonNull CameraCaptureSession session,
                                               @NonNull CaptureRequest request,
                                               @NonNull TotalCaptureResult result) {
                    showToast("Saved: " + mFile);
                    Log.d(TAG, mFile.toString());
                    unlockFocus();
                }
            };

            mCaptureSession.stopRepeating();
            mCaptureSession.abortCaptures();
            mCaptureSession.capture(captureBuilder.build(), CaptureCallback, null);
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
    }

enter image description here Saved image

0 个答案:

没有答案