Java Code Examples for android.hardware.camera2.CaptureRequest#Builder

The following examples show how to use android.hardware.camera2.CaptureRequest#Builder . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Camera2BasicFragment.java    From Android-Camera2-Front-with-Face-Detection with Apache License 2.0 6 votes vote down vote up
/**
 * Capture a still picture. This method should be called when we get a response in
 * {@link #mCaptureCallback} from both {@link #lockFocus()}.
 */
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.
        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.capture(captureBuilder.build(), CaptureCallback, null);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}
 
Example 2
Source File: OneCameraImpl.java    From Camera2 with Apache License 2.0 6 votes vote down vote up
/**
 * Request preview capture stream with auto focus trigger cycle.
 */
private void sendAutoFocusTriggerCaptureRequest(Object tag)
{
    try
    {
        // Step 1: Request single frame CONTROL_AF_TRIGGER_START.
        CaptureRequest.Builder builder;
        builder = mDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
        builder.addTarget(mPreviewSurface);
        builder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO);
        mControlAFMode = CameraMetadata.CONTROL_AF_MODE_AUTO;
        addBaselineCaptureKeysToRequest(builder);
        builder.set(CaptureRequest.CONTROL_AF_TRIGGER, CaptureRequest.CONTROL_AF_TRIGGER_START);
        builder.setTag(tag);
        mCaptureSession.capture(builder.build(), mCaptureCallback, mCameraHandler);

        // Step 2: Call repeatingPreview to update mControlAFMode.
        repeatingPreview(tag);
        resumeContinuousAFAfterDelay(FOCUS_HOLD_MILLIS);
    } catch (CameraAccessException ex)
    {
        Log.e(TAG, "Could not execute preview request.", ex);
    }
}
 
Example 3
Source File: MediaSnippetsActivity.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 5 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void takePicture() {
  try {
    CaptureRequest.Builder takePictureBuilder = mCamera.createCaptureRequest(
      CameraDevice.TEMPLATE_STILL_CAPTURE);

    takePictureBuilder.addTarget(mImageReader.getSurface());

    mCaptureSession.capture(takePictureBuilder.build(),
      null,  // CaptureCallback
      null);      // optional Handler
  } catch (CameraAccessException e) {
    Log.e(TAG, "Error capturing the photo", e);
  }
}
 
Example 4
Source File: OneCameraZslImpl.java    From Camera2 with Apache License 2.0 5 votes vote down vote up
private boolean sendRepeatingBurstCaptureRequest()
{
    Log.v(TAG, "sendRepeatingBurstCaptureRequest()");
    try
    {
        CaptureRequest.Builder builder;
        builder = mDevice.createCaptureRequest(CameraDevice.TEMPLATE_VIDEO_SNAPSHOT);
        builder.addTarget(mPreviewSurface);

        if (ZSL_ENABLED)
        {
            builder.addTarget(mCaptureImageReader.getSurface());
        }

        builder.set(CaptureRequest.CONTROL_MODE, CaptureRequest.CONTROL_MODE_AUTO);
        builder.set(CaptureRequest.CONTROL_AF_MODE,
                CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_VIDEO);
        builder.set(CaptureRequest.CONTROL_AF_TRIGGER, CaptureRequest.CONTROL_AF_TRIGGER_IDLE);

        builder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON);
        builder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_OFF);

        addRegionsToCaptureRequestBuilder(builder);

        mCaptureSession.setRepeatingRequest(builder.build(), mCaptureManager, mCameraHandler);
        return true;
    } catch (CameraAccessException e)
    {
        Log.v(TAG, "Could not send repeating burst capture request.", e);
        return false;
    }
}
 
Example 5
Source File: Camera2Source.java    From Camera2Vision with Apache License 2.0 5 votes vote down vote up
/**
 * Capture a still picture. This method should be called when we get a response in
 * {@link #mCaptureCallback} from both {@link #lockFocus()}.
 */
private void captureStillPicture() {
    try {
        if (null == mCameraDevice) {
            return;
        }
        if(mShutterCallback != null) {
            mShutterCallback.onShutter();
        }
        // This is the CaptureRequest.Builder that we use to take a picture.
        final CaptureRequest.Builder captureBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
        captureBuilder.addTarget(mImageReaderStill.getSurface());

        // Use the same AE and AF modes as the preview.
        captureBuilder.set(CaptureRequest.CONTROL_AF_MODE, mFocusMode);
        if(mFlashSupported) {
            captureBuilder.set(CaptureRequest.CONTROL_AE_MODE, mFlashMode);
        }

        // Orientation
        captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, getOrientation(mDisplayOrientation));

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

            @Override
            public void onCaptureCompleted(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, @NonNull TotalCaptureResult result) {
                unlockFocus();
            }
        };

        mCaptureSession.stopRepeating();
        mCaptureSession.capture(captureBuilder.build(), CaptureCallback, null);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}
 
Example 6
Source File: CameraOps.java    From android-HdrViewfinder with Apache License 2.0 5 votes vote down vote up
/**
 * Get a request builder for the current camera.
 */
public CaptureRequest.Builder createCaptureRequest(int template) throws CameraAccessException {
    CameraDevice device = mCameraDevice;
    if (device == null) {
        throw new IllegalStateException("Can't get requests when no camera is open");
    }
    return device.createCaptureRequest(template);
}
 
Example 7
Source File: CameraTexture2.java    From PHONK with GNU General Public License v3.0 5 votes vote down vote up
private void captureStillPicture() {
    MLog.d(TAG, "captureStillPicture");

    try {
        if (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.
        captureBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
        // setAutoFlash(captureBuilder);

        // Orientation
        captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, 0);

        CameraCaptureSession.CaptureCallback CaptureCallback = new CameraCaptureSession.CaptureCallback() {
            @Override
            public void onCaptureCompleted(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, @NonNull TotalCaptureResult result) {
                Log.d(TAG, "saved to: "); // + mFile.toString());
                unlockFocus();
            }
        };

        mCaptureSession.stopRepeating();
        mCaptureSession.capture(captureBuilder.build(), CaptureCallback, null);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}
 
Example 8
Source File: OpenStrategy.java    From CameraDemo with Apache License 2.0 5 votes vote down vote up
@SuppressLint("NewApi")
@Override
public void setCaptureRequest(CaptureRequest.Builder requestBuilder, CameraCaptureSession cameraCaptureSession, Handler handler) {
    requestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_ALWAYS_FLASH);
    requestBuilder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_OFF);

    try {
        cameraCaptureSession.setRepeatingRequest(requestBuilder.build(),null,handler);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}
 
Example 9
Source File: CameraHandler.java    From androidthings-imageclassifier with Apache License 2.0 5 votes vote down vote up
/**
 * Execute a new capture request within the active session
 */
private void triggerImageCapture() {
    try {
        final CaptureRequest.Builder captureBuilder =
                mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
        captureBuilder.addTarget(mImageReader.getSurface());
        captureBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON);
        Log.d(TAG, "Capture request created.");
        mCaptureSession.capture(captureBuilder.build(), mCaptureCallback, null);
    } catch (CameraAccessException cae) {
        Log.e(TAG, "Cannot trigger a capture request");
    }
}
 
Example 10
Source File: Camera2Input.java    From EZFilter with MIT License 5 votes vote down vote up
@Override
public void takePhoto(boolean isFront, int degree, PhotoTakenCallback callback) {
    mIsFront = isFront;
    mDegree = degree;
    mPhotoTakenCallback = callback;

    CaptureRequest.Builder captureRequestBuilder;
    try {
        captureRequestBuilder = mCamera.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
        // 将ImageReader的surface作为CaptureRequest.Builder的目标
        captureRequestBuilder.addTarget(mImageReader.getSurface());
        // 自动对焦
        captureRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
        // 自动曝光
        captureRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);
        // 设置照片方向
        if (mIsFront) {
            captureRequestBuilder.set(CaptureRequest.JPEG_ORIENTATION, 270);
        } else {
            captureRequestBuilder.set(CaptureRequest.JPEG_ORIENTATION, 90);
        }
        // 拍照
        CaptureRequest captureRequest = captureRequestBuilder.build();
        mCameraCaptureSession.capture(captureRequest, null, mPreviewHandler);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}
 
Example 11
Source File: OneCameraImpl.java    From Camera2 with Apache License 2.0 5 votes vote down vote up
/**
 * Adds current regions to CaptureRequest and base AF mode +
 * AF_TRIGGER_IDLE.
 *
 * @param builder Build for the CaptureRequest
 */
private void addBaselineCaptureKeysToRequest(CaptureRequest.Builder builder)
{
    builder.set(CaptureRequest.CONTROL_AF_REGIONS, mAFRegions);
    builder.set(CaptureRequest.CONTROL_AE_REGIONS, mAERegions);
    builder.set(CaptureRequest.SCALER_CROP_REGION, mCropRegion);
    builder.set(CaptureRequest.CONTROL_AF_MODE, mControlAFMode);
    builder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_IDLE);
    // Enable face detection
    builder.set(CaptureRequest.STATISTICS_FACE_DETECT_MODE, CaptureRequest.STATISTICS_FACE_DETECT_MODE_FULL);
    builder.set(CaptureRequest.CONTROL_SCENE_MODE, CaptureRequest.CONTROL_SCENE_MODE_FACE_PRIORITY);
}
 
Example 12
Source File: Camera2Wrapper.java    From DeviceConnect-Android with MIT License 4 votes vote down vote up
/**
 * 自動フラッシュの設定を CaptureRequest.Builder に設定します.
 *
 * @param requestBuilder 自動フラッシュ設定を行う CaptureRequest.Builder
 */
private void setAutoFlash(CaptureRequest.Builder requestBuilder) {
    if (mSettings.isAutoFlash()) {
        requestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);
    }
}
 
Example 13
Source File: Camera2Controller.java    From pixelvisualcorecamera with Apache License 2.0 4 votes vote down vote up
/** Trigger a precapture metering sequence. */
private void setAePrecaptureTriggerStart(CaptureRequest.Builder builder) {
  builder.set(
      CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER,
      CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER_START);
}
 
Example 14
Source File: Camera2VideoFragment.java    From android-Camera2Video with Apache License 2.0 4 votes vote down vote up
private void setUpCaptureRequestBuilder(CaptureRequest.Builder builder) {
    builder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO);
}
 
Example 15
Source File: Camera2Controller.java    From pixelvisualcorecamera with Apache License 2.0 4 votes vote down vote up
/** Returns the AF system to an idle state. */
private void setAfTriggerIdle(CaptureRequest.Builder builder) {
  builder.set(
      CaptureRequest.CONTROL_AF_TRIGGER,
      CameraMetadata.CONTROL_AF_TRIGGER_IDLE);
}
 
Example 16
Source File: CameraWrapper.java    From DeviceConnect-Android with MIT License 4 votes vote down vote up
private void setDefaultCaptureRequest(final CaptureRequest.Builder request) {
    setDefaultCaptureRequest(request, false);
}
 
Example 17
Source File: Camera2Controller.java    From pixelvisualcorecamera with Apache License 2.0 4 votes vote down vote up
/**
 *  Once request queue has been flushed (onReady), kick off the capture
 *  to avoid capturing previews.
 */
private void startStillCapture() {
  try {
    if (null == cameraDevice) {
      return;
    }

    // HDR+: The app must target api 26+ in order to pick up the set of default
    // parameters in TEMPLATE_STILL_CAPTURE that will enable HDR+ shots.
    CaptureRequest.Builder captureBuilder =
        cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);

    // HDR+: The ImageReader is configured for JPEG output. HDR+ shots are enabled when
    // the outputs consist solely of JPEG and/or YUV surfaces.
    captureBuilder.addTarget(imageReader.getSurface());

    // Use the same AE and AF modes as the preview.
    commonCaptureSessionConfig(captureBuilder);
    setCaptureSessionOrientation(captureBuilder);

    // HDR+: CaptureRequest.CONTROL_ENABLE_ZSL is set to true for HDR+ shots.
    Log.d(TAG, "non-hdr+ shot pending: " + nonHdrPlusShotPending);
    captureBuilder.set(CaptureRequest.CONTROL_ENABLE_ZSL, !nonHdrPlusShotPending);
    nonHdrPlusShotPending = false;
    Log.d(TAG, "CONTROL_ENABLE_ZSL = " + captureBuilder.get(CaptureRequest.CONTROL_ENABLE_ZSL));

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

      @Override
      public void onCaptureCompleted(
          @NonNull CameraCaptureSession session,
          @NonNull CaptureRequest request,
          @NonNull TotalCaptureResult result) {
        Log.d(TAG, "onCaptureCompleted, double shot pending = " + doubleShotPending);
        if (doubleShotPending) {
          doubleShotPending = false;
          nonHdrPlusShotPending = true;
          lockFocus();
        } else {
          unlockFocus();
        }
      }
    };
    captureSession.capture(captureBuilder.build(), captureCallback, null);
  } catch (CameraAccessException e) {
    Log.w(TAG, e);
  }
}
 
Example 18
Source File: OneCameraZslImpl.java    From Camera2 with Apache License 2.0 4 votes vote down vote up
/**
 * Request a single image.
 *
 * @return true if successful, false if there was an error submitting the
 * capture request.
 */
private boolean sendSingleRequest(OneCamera.PhotoCaptureParameters params)
{
    Log.v(TAG, "sendSingleRequest()");
    try
    {
        CaptureRequest.Builder builder;
        builder = mDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);

        builder.addTarget(mPreviewSurface);

        // Always add this surface for single image capture requests.
        builder.addTarget(mCaptureImageReader.getSurface());

        builder.set(CaptureRequest.CONTROL_MODE, CaptureRequest.CONTROL_MODE_AUTO);

        Flash flashMode = Flash.OFF;
        addFlashToCaptureRequestBuilder(builder, flashMode);
        addRegionsToCaptureRequestBuilder(builder);

        builder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_AUTO);
        builder.set(CaptureRequest.CONTROL_AF_TRIGGER, CaptureRequest.CONTROL_AF_TRIGGER_IDLE);

        // Tag this as a special request which should be saved.
        builder.setTag(RequestTag.EXPLICIT_CAPTURE);

        if (sCaptureImageFormat == ImageFormat.JPEG)
        {
            builder.set(CaptureRequest.JPEG_QUALITY, (byte) (JPEG_QUALITY));
            builder.set(CaptureRequest.JPEG_ORIENTATION,
                    CameraUtil.getJpegRotation(params.orientation, mCharacteristics));
        }

        mCaptureSession.capture(builder.build(), mCaptureManager, mCameraHandler);
        return true;
    } catch (CameraAccessException e)
    {
        Log.v(TAG, "Could not execute single still capture request.", e);
        return false;
    }
}
 
Example 19
Source File: Camera2RawFragment.java    From android-Camera2Raw with Apache License 2.0 4 votes vote down vote up
/**
 * Send a capture request to the camera device that initiates a capture targeting the JPEG and
 * RAW outputs.
 * <p/>
 * Call this only with {@link #mCameraStateLock} held.
 */
private void captureStillPictureLocked() {
    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(mJpegImageReader.get().getSurface());
        captureBuilder.addTarget(mRawImageReader.get().getSurface());

        // Use the same AE and AF modes as the preview.
        setup3AControlsLocked(captureBuilder);

        // Set orientation.
        int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
        captureBuilder.set(CaptureRequest.JPEG_ORIENTATION,
                sensorToDeviceRotation(mCharacteristics, rotation));

        // Set request tag to easily track results in callbacks.
        captureBuilder.setTag(mRequestCounter.getAndIncrement());

        CaptureRequest request = captureBuilder.build();

        // Create an ImageSaverBuilder in which to collect results, and add it to the queue
        // of active requests.
        ImageSaver.ImageSaverBuilder jpegBuilder = new ImageSaver.ImageSaverBuilder(activity)
                .setCharacteristics(mCharacteristics);
        ImageSaver.ImageSaverBuilder rawBuilder = new ImageSaver.ImageSaverBuilder(activity)
                .setCharacteristics(mCharacteristics);

        mJpegResultQueue.put((int) request.getTag(), jpegBuilder);
        mRawResultQueue.put((int) request.getTag(), rawBuilder);

        mCaptureSession.capture(request, mCaptureCallback, mBackgroundHandler);

    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}
 
Example 20
Source File: AndroidCameraDeviceProxy.java    From Camera2 with Apache License 2.0 4 votes vote down vote up
@Override
public CaptureRequest.Builder createCaptureRequest(int i) throws CameraAccessException
{
    return mCameraDevice.createCaptureRequest(i);
}