Java Code Examples for android.hardware.camera2.CaptureRequest

The following examples show how to use android.hardware.camera2.CaptureRequest. These examples are extracted from open source projects.
Example 1
Project: WeiXinRecordedDemo   File: CameraHelp2.java    License: MIT License 6 votes vote down vote up
public void shotPhoto(){

        try {
            CaptureRequest.Builder captureRequestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
            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);
            captureRequestBuilder.set(CaptureRequest.JPEG_ORIENTATION, mOrientation);
            CaptureRequest captureRequest = captureRequestBuilder.build();
            mCameraCaptureSession.capture(captureRequest, null, mHandler);
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
    }
 
Example 2
Project: TikTok   File: Camera2.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Updates the internal state of auto-focus to {@link #mAutoFocus}.
 */
void updateAutoFocus() {
    if (mAutoFocus) {
        int[] modes = mCameraCharacteristics.get(
                CameraCharacteristics.CONTROL_AF_AVAILABLE_MODES);
        // Auto focus is not supported
        if (modes == null || modes.length == 0 ||
                (modes.length == 1 && modes[0] == CameraCharacteristics.CONTROL_AF_MODE_OFF)) {
            mAutoFocus = false;
            mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE,
                    CaptureRequest.CONTROL_AF_MODE_OFF);
        } else {
            mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE,
                    CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
        }
    } else {
        mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE,
                CaptureRequest.CONTROL_AF_MODE_OFF);
    }
}
 
Example 3
Project: Camera2   File: CaptureDataSerializer.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Generate a human-readable string of the given capture request and write
 * it to the given file.
 */
public static void toFile(String title, CameraMetadata<?> metadata, File file)
{
    try
    {
        // Will append if the file already exists.
        FileWriter writer = new FileWriter(file, true);
        if (metadata instanceof CaptureRequest)
        {
            dumpMetadata(title, (CaptureRequest) metadata, writer);
        } else if (metadata instanceof CaptureResult)
        {
            dumpMetadata(title, (CaptureResult) metadata, writer);
        } else
        {
            writer.close();
            throw new IllegalArgumentException("Cannot generate debug data from type "
                    + metadata.getClass().getName());
        }
        writer.close();
    } catch (IOException ex)
    {
        Log.e(TAG, "Could not write capture data to file.", ex);
    }
}
 
Example 4
Project: android_9.0.0_r45   File: CameraCaptureSessionImpl.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public int setRepeatingBurst(List<CaptureRequest> requests,
        CaptureCallback callback, Handler handler) throws CameraAccessException {
    checkRepeatingRequests(requests);

    synchronized (mDeviceImpl.mInterfaceLock) {
        checkNotClosed();

        handler = checkHandler(handler, callback);

        if (DEBUG) {
            CaptureRequest[] requestArray = requests.toArray(new CaptureRequest[0]);
            Log.v(TAG, mIdString + "setRepeatingBurst - requests " +
                    Arrays.toString(requestArray) + ", callback " + callback +
                    " handler" + "" + handler);
        }

        return addPendingSequence(mDeviceImpl.setRepeatingBurst(requests,
                createCaptureCallbackProxy(handler, callback), mDeviceExecutor));
    }
}
 
Example 5
Project: Machine-Learning-Projects-for-Mobile-Applications   File: Camera2Source.java    License: MIT License 6 votes vote down vote up
/**
 * Unlock the focus. This method should be called when still image capture sequence is
 * finished.
 */
private void unlockFocus() {
    try {
        // Reset the auto-focus trigger
        mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_CANCEL);
        if (mFlashSupported) {
            mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, mFlashMode);
        }
        mCaptureSession.capture(mPreviewRequestBuilder.build(), mCaptureCallback, mBackgroundHandler);
        // After this, the camera will go back to the normal state of preview.
        mState = STATE_PREVIEW;
        mCaptureSession.setRepeatingRequest(mPreviewRequest, mCaptureCallback, mBackgroundHandler);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}
 
Example 6
Project: cameraview   File: Camera2.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Unlocks the auto-focus and restart camera preview. This is supposed to be called after
 * capturing a still picture.
 */
void unlockFocus() {
    mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER,
            CaptureRequest.CONTROL_AF_TRIGGER_CANCEL);
    try {
        mCaptureSession.capture(mPreviewRequestBuilder.build(), mCaptureCallback, null);
        updateAutoFocus();
        updateFlash();
        mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER,
                CaptureRequest.CONTROL_AF_TRIGGER_IDLE);
        mCaptureSession.setRepeatingRequest(mPreviewRequestBuilder.build(), mCaptureCallback,
                null);
        mCaptureCallback.setState(PictureCaptureCallback.STATE_PREVIEW);
    } catch (CameraAccessException e) {
        Log.e(TAG, "Failed to restart camera preview.", e);
    }
}
 
Example 7
Project: EvilsLive   File: CameraNew.java    License: MIT License 6 votes vote down vote up
private void doPreviewConfiguration() {
        if (mCamera == null) {
            return;
        }

        try {
//            mPreviewBuilder = mCamera.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
//            mPreviewBuilder.addTarget(surfaceView.getHolder().getSurface());
//            mPreviewBuilder.addTarget(mImageReader.getSurface());
            mPreviewBuilder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO);
//            mPreviewBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_AUTO);
//            mPreviewBuilder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_OFF);
//            mPreviewBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_OFF);

            mPreviewBuilder.set(CaptureRequest.CONTROL_AE_TARGET_FPS_RANGE, mFps[mFps.length -1 ]);
            mCaptureSession.setRepeatingRequest(mPreviewBuilder.build(), mPreCaptureCallback, mHandler);

        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
    }
 
Example 8
Project: LockDemo   File: Camera2.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Unlocks the auto-focus and restart camera preview. This is supposed to be called after
 * capturing a still picture.
 */
void unlockFocus() {
    mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER,
            CaptureRequest.CONTROL_AF_TRIGGER_CANCEL);
    try {
        mCaptureSession.capture(mPreviewRequestBuilder.build(), mCaptureCallback, null);
        updateAutoFocus();
        updateFlash();
        mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER,
                CaptureRequest.CONTROL_AF_TRIGGER_IDLE);
        mCaptureSession.setRepeatingRequest(mPreviewRequestBuilder.build(), mCaptureCallback,
                null);
        mCaptureCallback.setState(PictureCaptureCallback.STATE_PREVIEW);
    } catch (CameraAccessException e) {
        Log.e(TAG, "Failed to restart camera preview.", e);
    }
}
 
Example 9
Project: DeviceConnect-Android   File: CameraWrapper.java    License: MIT License 6 votes vote down vote up
private void setDefaultCaptureRequest(final CaptureRequest.Builder request,
                                      final boolean trigger) {
    if (hasAutoFocus()) {
        request.set(CaptureRequest.CONTROL_AF_MODE, mAutoFocusMode);
        if (trigger) {
            request.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_START);
        }
    }
    if (hasAutoExposure()) {
        request.set(CaptureRequest.CONTROL_AE_MODE, mAutoExposureMode);
    }
    setWhiteBalance(request);
    // Light が ON の場合は、CONTROL_AE_MODE を CONTROL_AE_MODE_ON にする。
    if (mIsTouchOn) {
        mUseTouch = true;
        request.set(CaptureRequest.CONTROL_AE_MODE, CameraMetadata.CONTROL_AE_MODE_ON);
        request.set(CaptureRequest.FLASH_MODE, CameraMetadata.FLASH_MODE_TORCH);
    }
}
 
Example 10
Project: libsoftwaresync   File: CaptureRequestFactory.java    License: Apache License 2.0 6 votes vote down vote up
public CaptureRequest.Builder makeFrameInjectionRequest(
    long desiredExposureTimeNs, List<Surface> imageSurfaces) throws CameraAccessException {
  CaptureRequest.Builder builder = device.createCaptureRequest(TEMPLATE_PREVIEW);
  builder.set(CONTROL_MODE, CONTROL_MODE_AUTO);
  builder.set(CONTROL_AE_MODE, CONTROL_AE_MODE_OFF);
  builder.set(SENSOR_EXPOSURE_TIME, desiredExposureTimeNs);
  // TODO: Inserting frame duration directly would be more accurate than inserting exposure since
  // {@code frame duration ~ exposure + variable overhead}. However setting frame duration may not
  // be supported on many android devices, so we use exposure time here.

  List<Integer> targetIndices = new ArrayList<>();
  for (int i = 0; i < imageSurfaces.size(); i++) {
    builder.addTarget(imageSurfaces.get(i));
    targetIndices.add(i);
  }
  builder.setTag(new CaptureRequestTag(targetIndices, PhaseAlignController.INJECT_FRAME));

  return builder;
}
 
Example 11
Project: aurora-imui   File: CameraNew.java    License: MIT License 5 votes vote down vote up
/**
 * Run the precapture sequence for capturing a still image. This method should be called when
 * we get a response in {@link #mCaptureCallback} from {@link #lockFocus()}.
 */
private void runPrecaptureSequence() {
    try {
        // This is how to tell the camera to trigger.
        mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER,
                CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER_START);
        // Tell #mCaptureCallback to wait for the precapture sequence to be set.
        mState = STATE_WAITING_PRECAPTURE;
        mCaptureSession.capture(mPreviewRequestBuilder.build(), mCaptureCallback,
                mBackgroundHandler);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}
 
Example 12
Project: cameraview   File: Camera2.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Updates the internal state of flash to {@link #mFlash}.
 */
void updateFlash() {
    switch (mFlash) {
        case Constants.FLASH_OFF:
            mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE,
                    CaptureRequest.CONTROL_AE_MODE_ON);
            mPreviewRequestBuilder.set(CaptureRequest.FLASH_MODE,
                    CaptureRequest.FLASH_MODE_OFF);
            break;
        case Constants.FLASH_ON:
            mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE,
                    CaptureRequest.CONTROL_AE_MODE_ON_ALWAYS_FLASH);
            mPreviewRequestBuilder.set(CaptureRequest.FLASH_MODE,
                    CaptureRequest.FLASH_MODE_OFF);
            break;
        case Constants.FLASH_TORCH:
            mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE,
                    CaptureRequest.CONTROL_AE_MODE_ON);
            mPreviewRequestBuilder.set(CaptureRequest.FLASH_MODE,
                    CaptureRequest.FLASH_MODE_TORCH);
            break;
        case Constants.FLASH_AUTO:
            mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE,
                    CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);
            mPreviewRequestBuilder.set(CaptureRequest.FLASH_MODE,
                    CaptureRequest.FLASH_MODE_OFF);
            break;
        case Constants.FLASH_RED_EYE:
            mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE,
                    CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH_REDEYE);
            mPreviewRequestBuilder.set(CaptureRequest.FLASH_MODE,
                    CaptureRequest.FLASH_MODE_OFF);
            break;
    }
}
 
Example 13
Project: Camera2   File: ConvergedImageCaptureCommand.java    License: Apache License 2.0 5 votes vote down vote up
private void resetRepeating(FrameServer.Session session) throws InterruptedException,
        CameraCaptureSessionClosedException, CameraAccessException,
        ResourceAcquisitionFailedException
{
    RequestBuilder repeatingBuilder = mRepeatingRequestBuilder.create
            (mRepeatingRequestTemplate);
    session.submitRequest(Arrays.asList(repeatingBuilder.build()),
            FrameServer.RequestType.REPEATING);

    RequestBuilder triggerCancelBuilder = mRepeatingRequestBuilder
            .create(mRepeatingRequestTemplate);
    triggerCancelBuilder.setParam(CaptureRequest.CONTROL_AF_TRIGGER,
            CaptureRequest.CONTROL_AF_TRIGGER_CANCEL);
    session.submitRequest(Arrays.asList(triggerCancelBuilder.build()),
            FrameServer.RequestType.NON_REPEATING);

    // Some devices (e.g. N6) implicitly lock AE after sending an
    // AE_PRECAPTURE trigger. (see bug: 19265647)
    // The implicit lock is released when a request with
    // INTENT_STILL_CAPTURE is taken.

    // However, if we never get to that point (because the command was
    // interrupted before the request for a photo was sent), then we must be
    // sure to cancel this implicit AE lock to resume normal AE behavior.
    // Sending a request for an explicit AE lock (followed, implicitly, by a
    // request from the current repeating request, which has AE lock off)
    // fixes the issue and results in normal AE behavior.
    RequestBuilder hackAETriggerCancelBuilder = mRepeatingRequestBuilder.create
            (mRepeatingRequestTemplate);
    hackAETriggerCancelBuilder.setParam(CaptureRequest.CONTROL_AE_LOCK, true);

    session.submitRequest(Arrays.asList(hackAETriggerCancelBuilder.build()),
            FrameServer.RequestType.NON_REPEATING);
}
 
Example 14
Project: sandriosCamera   File: Camera2Manager.java    License: MIT License 5 votes vote down vote up
private void unlockFocus() {
    try {
        previewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_CANCEL);
        captureSession.capture(previewRequestBuilder.build(), captureCallback, backgroundHandler);
        previewState = STATE_PREVIEW;
        captureSession.setRepeatingRequest(previewRequest, captureCallback, backgroundHandler);
    } catch (Exception e) {
        Log.e(TAG, "Error during focus unlocking");
    }
}
 
Example 15
Project: 361Camera   File: Camera2Fragment.java    License: Apache License 2.0 5 votes vote down vote up
private boolean sendRepeatPreviewRequest() {
    try {
        CaptureRequest.Builder builder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
        builder.addTarget(mPreviewSurface);
        builder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO);
        builder.set(CaptureRequest.CONTROL_AF_MODE, mControlAFMode.switchToCamera2FocusMode());

        mCaptureSession.setRepeatingRequest(builder.build(), mPreCaptureCallback, mBackgroundHandler);
        return true;
    } catch (CameraAccessException e) {
        e.printStackTrace();
        return false;
    }
}
 
Example 16
Project: android_9.0.0_r45   File: RequestHolder.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Construct a new {@link Builder} to generate {@link RequestHolder} objects.
 *
 * @param requestId the ID to set in {@link RequestHolder} objects.
 * @param subsequenceId the sequence ID to set in {@link RequestHolder} objects.
 * @param request the original {@link CaptureRequest} to set in {@link RequestHolder}
 *                objects.
 * @param repeating {@code true} if the request is repeating.
 */
public Builder(int requestId, int subsequenceId, CaptureRequest request,
               boolean repeating, Collection<Long> jpegSurfaceIds) {
    checkNotNull(request, "request must not be null");
    mRequestId = requestId;
    mSubsequenceId = subsequenceId;
    mRequest = request;
    mRepeating = repeating;
    mJpegSurfaceIds = jpegSurfaceIds;
    mNumJpegTargets = numJpegTargets(mRequest);
    mNumPreviewTargets = numPreviewTargets(mRequest);
}
 
Example 17
Project: MobileInfo   File: CameraInfo.java    License: Apache License 2.0 5 votes vote down vote up
private static String getAvailableOisDataModes(int availableOisDataModes) {
    switch (availableOisDataModes) {
        case CaptureRequest.STATISTICS_OIS_DATA_MODE_ON:
            return "ON";
        case CaptureRequest.STATISTICS_OIS_DATA_MODE_OFF:
            return "OFF";
        default:
            return UNKNOWN + "-" + availableOisDataModes;

    }
}
 
Example 18
Project: android_9.0.0_r45   File: LegacyRequest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Create a new legacy request; the parameters are copied.
 *
 * @param characteristics immutable static camera characteristics for this camera
 * @param captureRequest immutable user-defined capture request
 * @param previewSize immutable internal preview size used for {@link Camera#setPreviewSurface}
 * @param parameters the initial camera1 parameter state; (copied) can be mutated
 */
public LegacyRequest(CameraCharacteristics characteristics, CaptureRequest captureRequest,
        Size previewSize, Camera.Parameters parameters) {
    this.characteristics = checkNotNull(characteristics, "characteristics must not be null");
    this.captureRequest = checkNotNull(captureRequest, "captureRequest must not be null");
    this.previewSize = checkNotNull(previewSize, "previewSize must not be null");
    checkNotNull(parameters, "parameters must not be null");

    this.parameters = Camera.getParametersCopy(parameters);
}
 
Example 19
Project: EvilsLive   File: CameraNew.java    License: MIT License 5 votes vote down vote up
private void reconfigureCamera() {
    if(mCamera != null) {
        try {
            mCaptureSession.stopRepeating();

            mPreviewBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_CANCEL);
            mCaptureSession.capture(mPreviewBuilder.build(), mPreCaptureCallback, mHandler);

            doPreviewConfiguration();
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
    }
}
 
Example 20
Project: Camera2   File: AndroidCameraCaptureSessionProxy.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void onCaptureStarted(CameraCaptureSession session, CaptureRequest request,
                             long timestamp, long frameNumber)
{
    mCallback.onCaptureStarted(AndroidCameraCaptureSessionProxy.this, request, timestamp,
            frameNumber);
}
 
Example 21
Project: android_9.0.0_r45   File: ParamsUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Return the value set by the key, or the {@code defaultValue} if no value was set.
 *
 * @throws NullPointerException if any of the args were {@code null}
 */
public static <T> T getOrDefault(CaptureRequest r, CaptureRequest.Key<T> key, T defaultValue) {
    checkNotNull(r, "r must not be null");
    checkNotNull(key, "key must not be null");
    checkNotNull(defaultValue, "defaultValue must not be null");

    T value = r.get(key);
    if (value == null) {
        return defaultValue;
    } else {
        return value;
    }
}
 
Example 22
Project: DeviceConnect-Android   File: CameraWrapper.java    License: MIT License 5 votes vote down vote up
/**
 * カメラのライトをONにする.
 *
 * @param listener 実際にライトがONになったタイミングで実行されるリスナー
 * @param handler リスナーを実行するハンドラー. リスナーを指定する場合は必須
 * @throws CameraWrapperException カメラに何らかの致命的なエラーが発生した場合
 */
public synchronized void turnOnTorch(final @Nullable TorchOnListener listener,
                                     final @Nullable Handler handler) throws CameraWrapperException {
    if (listener != null && handler == null) {
        throw new IllegalArgumentException("handler is mandatory if listener is specified.");
    }
    if (mIsTouchOn) {
        notifyTorchOnEvent(listener, handler);
        return;
    }
    try {
        CameraDevice cameraDevice = openCamera();
        mCaptureSession = createCaptureSession(cameraDevice);
        final CaptureRequest.Builder requestBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
        if (mIsPreview) {
            if (mPreviewSurface != null) {
                requestBuilder.addTarget(mPreviewSurface);
            }
            if (mTargetSurface != null) {
                requestBuilder.addTarget(mTargetSurface);
            }
        }
        if (mIsRecording) {
            requestBuilder.addTarget(mRecordingSurface);
        }

        if (!mIsPreview && !mIsRecording) {
            requestBuilder.addTarget(mDummyPreviewReader.getSurface());
        }
        requestBuilder.set(CaptureRequest.FLASH_MODE, CameraMetadata.FLASH_MODE_TORCH);
        mCaptureSession.setRepeatingRequest(requestBuilder.build(), null, mBackgroundHandler);
        mUseTouch = true;
        mIsTouchOn = true;
        notifyTorchOnEvent(listener, handler);
    } catch (CameraAccessException e) {
        throw new CameraWrapperException(e);
    }
}
 
Example 23
Project: rtmp-rtsp-stream-client-java   File: Camera2ApiManager.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void onCaptureCompleted(@NonNull CameraCaptureSession session,
    @NonNull CaptureRequest request, @NonNull TotalCaptureResult result) {
  Face[] faces = result.get(CaptureResult.STATISTICS_FACES);
  if (faceDetectorCallback != null) {
    faceDetectorCallback.onGetFaces(faces);
  }
}
 
Example 24
Project: DeviceConnect-Android   File: Camera2Wrapper.java    License: MIT License 5 votes vote down vote up
@Override
public void onCaptureResult(CaptureResult result, boolean isCompleted) {
    Integer aeState = result.get(CaptureResult.CONTROL_AE_STATE);
    if (DEBUG) {
        Log.d(TAG, "aeState: " + Camera2Helper.debugAEState(aeState) + " isCompleted: " + isCompleted);
    }
    boolean isAeReady = aeState == null
            || aeState == CaptureResult.CONTROL_AE_STATE_CONVERGED
            || aeState == CaptureRequest.CONTROL_AE_STATE_FLASH_REQUIRED;
    boolean timeout = (System.currentTimeMillis() - mStartTime) > 5000;
    if (isAeReady || timeout) {
        nextState(mTakePictureState);
    }
}
 
Example 25
Project: PHONK   File: CameraTexture2.java    License: 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 26
Project: 361Camera   File: Camera2Fragment.java    License: Apache License 2.0 5 votes vote down vote up
private void triggerFocusArea(float x, float y) {
    CameraManager manager = (CameraManager) getActivity().getSystemService(Context.CAMERA_SERVICE);
    try {
        CameraCharacteristics characteristics
                = manager.getCameraCharacteristics(mCameraId);
        Integer sensorOrientation = characteristics.get(
                CameraCharacteristics.SENSOR_ORIENTATION);

        sensorOrientation = sensorOrientation == null ? 0 : sensorOrientation;

        Rect cropRegion = AutoFocusHelper.cropRegionForZoom(characteristics, 1f);
        mAERegions = AutoFocusHelper.aeRegionsForNormalizedCoord(x, y, cropRegion, sensorOrientation);
        mAFRegions = AutoFocusHelper.afRegionsForNormalizedCoord(x, y, cropRegion, sensorOrientation);

        // Step 1: Request single frame CONTROL_AF_TRIGGER_START.
        CaptureRequest.Builder builder;
        builder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
        builder.addTarget(mPreviewSurface);
        builder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO);

        mControlAFMode = AutoFocusMode.AUTO;

        builder.set(CaptureRequest.CONTROL_AF_MODE, mControlAFMode.switchToCamera2FocusMode());
        builder.set(CaptureRequest.CONTROL_AF_TRIGGER, CaptureRequest.CONTROL_AF_TRIGGER_START);
        mCaptureSession.capture(builder.build(), mPreCaptureCallback, mBackgroundHandler);

        // Step 2: Call repeatingPreview to update mControlAFMode.
        sendRepeatPreviewRequest();
        resumeContinuousAFAfterDelay(DELAY_TIME_RESUME_CONTINUOUS_AF);
    } catch (CameraAccessException ex) {
        Log.e(TAG, "Could not execute preview request.", ex);
    }
}
 
Example 27
Project: Machine-Learning-Projects-for-Mobile-Applications   File: Camera2Source.java    License: MIT License 5 votes vote down vote up
/**
 * Run the precapture sequence for capturing a still image. This method should be called when
 * we get a response in {@link #mCaptureCallback} from {@link #lockFocus()}.
 */
private void runPrecaptureSequence() {
    try {
        // This is how to tell the camera to trigger.
        mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER, CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER_START);
        // Tell #mCaptureCallback to wait for the precapture sequence to be set.
        mState = STATE_WAITING_PRECAPTURE;
        mCaptureSession.capture(mPreviewRequestBuilder.build(), mCaptureCallback, mBackgroundHandler);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}
 
Example 28
Project: SimpleVideoEditor   File: Camera2.java    License: Apache License 2.0 5 votes vote down vote up
private void updateAutoFocus() {
    if (mConfig.isAutoFocus()) {
        CameraView.MODE mode = mConfig.getMode();
        if (mode == CameraView.MODE.PICTURE) {
            mCaptureRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE,
                    CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
        } else if (mode == CameraView.MODE.VIDEO) {
            mCaptureRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE,
                    CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_VIDEO);
        }
    } else {
        mCaptureRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE,
                CaptureRequest.CONTROL_AF_MODE_MACRO);
    }
}
 
Example 29
Project: opencv-documentscanner-android   File: Camera2BasicFragment.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Run the precapture sequence for capturing a still image. This method should be called when
 * we get a response in {@link #mCaptureCallback} from {@link #lockFocus()}.
 */
private void runPrecaptureSequence() {
    try {
        // This is how to tell the camera to trigger.
        mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER,
                CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER_START);
        // Tell #mCaptureCallback to wait for the precapture sequence to be set.
        mState = STATE_WAITING_PRECAPTURE;
        mCaptureSession.capture(mPreviewRequestBuilder.build(), mCaptureCallback,
                mBackgroundHandler);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}
 
Example 30
Project: EZFilter   File: Camera2Input.java    License: 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 31
Project: android_9.0.0_r45   File: CameraConstrainedHighSpeedCaptureSessionImpl.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public int captureBurstRequests(List<CaptureRequest> requests, Executor executor,
        CaptureCallback listener) throws CameraAccessException {
    if (!isConstrainedHighSpeedRequestList(requests)) {
        throw new IllegalArgumentException(
            "Only request lists created by createHighSpeedRequestList() can be submitted to " +
            "a constrained high speed capture session");
    }
    return mSessionImpl.captureBurstRequests(requests, executor, listener);
}
 
Example 32
Project: CameraRecorder-android   File: CameraThread.java    License: MIT License 5 votes vote down vote up
/**
 * stop camera preview
 */
void stopPreview() {
    Log.v(TAG, "stopPreview:");
    isFlashTorch = false;
    if (requestBuilder != null) {
        requestBuilder.set(CaptureRequest.FLASH_MODE, CameraMetadata.FLASH_MODE_OFF);
        try {
            cameraCaptureSession.setRepeatingRequest(requestBuilder.build(), null, null);
            cameraDevice.close();
            Log.v(TAG, "stopPreview: cameraDevice.close()");
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
    }
}
 
Example 33
Project: android_9.0.0_r45   File: CameraConstrainedHighSpeedCaptureSessionImpl.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public int setRepeatingBurst(List<CaptureRequest> requests, CaptureCallback listener,
        Handler handler) throws CameraAccessException {
    if (!isConstrainedHighSpeedRequestList(requests)) {
        throw new IllegalArgumentException(
            "Only request lists created by createHighSpeedRequestList() can be submitted to " +
            "a constrained high speed capture session");
    }
    return mSessionImpl.setRepeatingBurst(requests, listener, handler);
}
 
Example 34
Project: Camera2   File: AndroidCameraCaptureSessionProxy.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void onCaptureProgressed(CameraCaptureSession session, CaptureRequest request,
                                CaptureResult partialResult)
{
    mCallback.onCaptureProgressed(AndroidCameraCaptureSessionProxy.this, request,
            partialResult);
}
 
Example 35
Project: Android-9-Development-Cookbook   File: MainActivity.java    License: MIT License 5 votes vote down vote up
private void startPreview(CameraCaptureSession session) {
    mCameraCaptureSession = session;
    mCaptureRequestBuilder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO);
    HandlerThread backgroundThread = new HandlerThread("CameraPreview");
    backgroundThread.start();
    Handler backgroundHandler = new Handler(backgroundThread. getLooper());
    try {
        mCameraCaptureSession
                .setRepeatingRequest(mCaptureRequestBuilder.build(), null, backgroundHandler);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}
 
Example 36
Project: WeiXinRecordedDemo   File: CameraHelp2.java    License: MIT License 5 votes vote down vote up
public void openFlash(){
    try {
        previewRequestBuilder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_TORCH);
        CaptureRequest previewRequest = previewRequestBuilder.build();
        mCameraCaptureSession.setRepeatingRequest(previewRequest, null, mHandler);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}
 
Example 37
Project: CameraDemo   File: AutoStrategy.java    License: 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_AUTO_FLASH);
    requestBuilder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_OFF);

    try {
        cameraCaptureSession.setRepeatingRequest(requestBuilder.build(),null,handler);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}
 
Example 38
Project: Augendiagnose   File: Camera2Handler.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Lock the focus as the first step for a still image capture.
 */
private void lockFocus() {
	try {
		// This is how to tell the camera to lock focus.
		mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_START);
		// Tell mCaptureCallback to wait for the lock.
		mState = CameraState.STATE_WAITING_LOCK;

		mCaptureSession.capture(mPreviewRequestBuilder.build(), mCaptureCallback, mBackgroundHandler);
	}
	catch (CameraAccessException | IllegalStateException e) {
		mCameraCallback.onCameraError("Failed to lock focus", "loc1", e);
	}
}
 
Example 39
Project: TikTok   File: Camera2.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Updates the internal state of flash to {@link #mFlash}.
 */
void updateFlash() {
    switch (mFlash) {
        case Constants.FLASH_OFF:
            mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE,
                    CaptureRequest.CONTROL_AE_MODE_ON);
            mPreviewRequestBuilder.set(CaptureRequest.FLASH_MODE,
                    CaptureRequest.FLASH_MODE_OFF);
            break;
        case Constants.FLASH_ON:
            mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE,
                    CaptureRequest.CONTROL_AE_MODE_ON_ALWAYS_FLASH);
            mPreviewRequestBuilder.set(CaptureRequest.FLASH_MODE,
                    CaptureRequest.FLASH_MODE_OFF);
            break;
        case Constants.FLASH_TORCH:
            mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE,
                    CaptureRequest.CONTROL_AE_MODE_ON);
            mPreviewRequestBuilder.set(CaptureRequest.FLASH_MODE,
                    CaptureRequest.FLASH_MODE_TORCH);
            break;
        case Constants.FLASH_AUTO:
            mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE,
                    CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);
            mPreviewRequestBuilder.set(CaptureRequest.FLASH_MODE,
                    CaptureRequest.FLASH_MODE_OFF);
            break;
        case Constants.FLASH_RED_EYE:
            mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE,
                    CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH_REDEYE);
            mPreviewRequestBuilder.set(CaptureRequest.FLASH_MODE,
                    CaptureRequest.FLASH_MODE_OFF);
            break;
    }
}
 
Example 40
Project: Camera2   File: Camera2CaptureCallbackSplitter.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request,
                               TotalCaptureResult result) {
    for (CaptureCallback target : mRecipients) {
        target.onCaptureCompleted(session, request, result);
    }
}