Example usage for android.hardware.camera2 CaptureResult FLASH_MODE

List of usage examples for android.hardware.camera2 CaptureResult FLASH_MODE

Introduction

In this page you can find the example usage for android.hardware.camera2 CaptureResult FLASH_MODE.

Prototype

Key FLASH_MODE

To view the source code for android.hardware.camera2 CaptureResult FLASH_MODE.

Click Source Link

Document

The desired mode for for the camera device's flash control.

This control is only effective when flash unit is available ( CameraCharacteristics#FLASH_INFO_AVAILABLE android.flash.info.available == true).

When this control is used, the CaptureRequest#CONTROL_AE_MODE android.control.aeMode must be set to ON or OFF.

Usage

From source file:com.obviousengine.android.focus.ZslFocusCamera.java

/**
 * Take a picture.//from   w ww .jav a2  s  .  com
 */
@Override
public void takePicture(final PhotoCaptureParameters params, final CaptureSession session) {
    params.checkSanity();

    readyStateManager.setInput(ReadyStateRequirement.CAPTURE_NOT_IN_PROGRESS, false);

    boolean useZSL = ZSL_ENABLED;

    // We will only capture images from the zsl ring-buffer which satisfy
    // this constraint.
    ArrayList<ImageCaptureManager.CapturedImageConstraint> zslConstraints = new ArrayList<>();
    zslConstraints.add(new ImageCaptureManager.CapturedImageConstraint() {
        @Override
        public boolean satisfiesConstraint(TotalCaptureResult captureResult) {
            Long timestamp = captureResult.get(CaptureResult.SENSOR_TIMESTAMP);
            Integer lensState = captureResult.get(CaptureResult.LENS_STATE);
            Integer flashState = captureResult.get(CaptureResult.FLASH_STATE);
            Integer flashMode = captureResult.get(CaptureResult.FLASH_MODE);
            Integer aeState = captureResult.get(CaptureResult.CONTROL_AE_STATE);
            Integer afState = captureResult.get(CaptureResult.CONTROL_AF_STATE);
            Integer awbState = captureResult.get(CaptureResult.CONTROL_AWB_STATE);

            if (timestamp <= lastCapturedImageTimestamp.get()) {
                // Don't save frames older than the most
                // recently-captured frame.
                // TODO This technically has a race condition in which
                // duplicate frames may be saved, but if a user is
                // tapping at >30Hz, duplicate images may be what they
                // expect.
                return false;
            }

            if (lensState == CaptureResult.LENS_STATE_MOVING) {
                // If we know the lens was moving, don't use this image.
                return false;
            }

            if (aeState == CaptureResult.CONTROL_AE_STATE_SEARCHING
                    || aeState == CaptureResult.CONTROL_AE_STATE_PRECAPTURE) {
                return false;
            }
            switch (params.flashMode) {
            case OFF:
                break;
            case ON:
                if (flashState != CaptureResult.FLASH_STATE_FIRED
                        || flashMode != CaptureResult.FLASH_MODE_SINGLE) {
                    return false;
                }
                break;
            case AUTO:
                if (aeState == CaptureResult.CONTROL_AE_STATE_FLASH_REQUIRED
                        && flashState != CaptureResult.FLASH_STATE_FIRED) {
                    return false;
                }
                break;
            }

            if (afState == CaptureResult.CONTROL_AF_STATE_ACTIVE_SCAN
                    || afState == CaptureResult.CONTROL_AF_STATE_PASSIVE_SCAN) {
                return false;
            }

            if (awbState == CaptureResult.CONTROL_AWB_STATE_SEARCHING) {
                return false;
            }

            return true;
        }
    });
    // This constraint lets us capture images which have been explicitly
    // requested. See {@link RequestTag.EXPLICIT_CAPTURE}.
    ArrayList<ImageCaptureManager.CapturedImageConstraint> singleCaptureConstraint = new ArrayList<>();
    singleCaptureConstraint.add(new ImageCaptureManager.CapturedImageConstraint() {
        @Override
        public boolean satisfiesConstraint(TotalCaptureResult captureResult) {
            Object tag = captureResult.getRequest().getTag();
            return tag == RequestTag.EXPLICIT_CAPTURE;
        }
    });

    // If we can use ZSL, try to save a previously-captured frame, if an
    // acceptable one exists in the buffer.
    if (useZSL) {
        boolean capturedPreviousFrame = captureManager
                .tryCaptureExistingImage(new ImageCaptureTask(params, session), zslConstraints);
        if (capturedPreviousFrame) {
            Timber.v("Saving previous frame");
            onShutterInvokeUI(params);
        } else {
            Timber.v("No good image Available.  Capturing next available good image.");
            // If there was no good frame available in the ring buffer
            // already, capture the next good image.
            // TODO Disable the shutter button until this image is captured.

            if (params.flashMode == Flash.ON || params.flashMode == Flash.AUTO) {
                // We must issue a request for a single capture using the
                // flash, including an AE precapture trigger.

                // The following sets up a sequence of events which will
                // occur in reverse order to the associated method
                // calls:
                // 1. Send a request to trigger the Auto Exposure Precapture
                // 2. Wait for the AE_STATE to leave the PRECAPTURE state,
                // and then send a request for a single image, with the
                // appropriate flash settings.
                // 3. Capture the next appropriate image, which should be
                // the one we requested in (2).

                captureManager.captureNextImage(new ImageCaptureTask(params, session), singleCaptureConstraint);

                captureManager.addMetadataChangeListener(CaptureResult.CONTROL_AE_STATE,
                        new ImageCaptureManager.MetadataChangeListener() {
                            @Override
                            public void onImageMetadataChange(Key<?> key, Object oldValue, Object newValue,
                                    CaptureResult result) {
                                Timber.v("AE State Changed");
                                if (oldValue
                                        .equals(Integer.valueOf(CaptureResult.CONTROL_AE_STATE_PRECAPTURE))) {
                                    captureManager.removeMetadataChangeListener(key, this);
                                    sendSingleRequest(params);
                                    // TODO: Delay this until onCaptureStarted().
                                    onShutterInvokeUI(params);
                                }
                            }
                        });

                sendAutoExposureTriggerRequest(params.flashMode);
            } else {
                // We may get here if, for example, the auto focus is in the
                // middle of a scan.
                // If the flash is off, we should just wait for the next
                // image that arrives. This will have minimal delay since we
                // do not need to send a new capture request.
                captureManager.captureNextImage(new ImageCaptureTask(params, session), zslConstraints);
            }
        }
    } else {
        // TODO If we can't save a previous frame, create a new capture
        // request to do what we need (e.g. flash) and call
        // captureNextImage().
        throw new UnsupportedOperationException("Non-ZSL capture not yet supported");
    }
}

From source file:com.android.camera.one.v2.OneCameraZslImpl.java

/**
 * Take a picture./*from  ww  w .  jav  a 2 s.  c  o m*/
 */
@Override
public void takePicture(final PhotoCaptureParameters params, final CaptureSession session) {
    mReadyStateManager.setInput(ReadyStateRequirement.CAPTURE_NOT_IN_PROGRESS, false);

    boolean useZSL = ZSL_ENABLED;

    // We will only capture images from the zsl ring-buffer which satisfy
    // this constraint.
    ArrayList<ImageCaptureManager.CapturedImageConstraint> zslConstraints = new ArrayList<ImageCaptureManager.CapturedImageConstraint>();
    zslConstraints.add(new ImageCaptureManager.CapturedImageConstraint() {
        @Override
        public boolean satisfiesConstraint(TotalCaptureResult captureResult) {
            Long timestamp = captureResult.get(CaptureResult.SENSOR_TIMESTAMP);
            Integer lensState = captureResult.get(CaptureResult.LENS_STATE);
            Integer flashState = captureResult.get(CaptureResult.FLASH_STATE);
            Integer flashMode = captureResult.get(CaptureResult.FLASH_MODE);
            Integer aeState = captureResult.get(CaptureResult.CONTROL_AE_STATE);
            Integer afState = captureResult.get(CaptureResult.CONTROL_AF_STATE);
            Integer awbState = captureResult.get(CaptureResult.CONTROL_AWB_STATE);

            if (lensState == null) {
                lensState = CaptureResult.LENS_STATE_STATIONARY;
            }
            if (flashState == null) {
                flashState = CaptureResult.FLASH_STATE_UNAVAILABLE;
            }
            if (flashMode == null) {
                flashMode = CaptureResult.FLASH_MODE_OFF;
            }
            if (aeState == null) {
                aeState = CaptureResult.CONTROL_AE_STATE_INACTIVE;
            }
            if (afState == null) {
                afState = CaptureResult.CONTROL_AF_STATE_INACTIVE;
            }
            if (awbState == null) {
                awbState = CaptureResult.CONTROL_AWB_STATE_INACTIVE;
            }

            synchronized (mCapturedImageTimestamps) {
                if (mCapturedImageTimestamps.contains(timestamp)) {
                    // Don't save frames which we've already saved.
                    return false;
                }
            }

            if (lensState == CaptureResult.LENS_STATE_MOVING) {
                // If we know the lens was moving, don't use this image.
                return false;
            }

            if (aeState == CaptureResult.CONTROL_AE_STATE_SEARCHING
                    || aeState == CaptureResult.CONTROL_AE_STATE_PRECAPTURE) {
                return false;
            }

            if (afState == CaptureResult.CONTROL_AF_STATE_ACTIVE_SCAN
                    || afState == CaptureResult.CONTROL_AF_STATE_PASSIVE_SCAN) {
                return false;
            }

            if (awbState == CaptureResult.CONTROL_AWB_STATE_SEARCHING) {
                return false;
            }

            return true;
        }
    });
    // This constraint lets us capture images which have been explicitly
    // requested. See {@link RequestTag.EXPLICIT_CAPTURE}.
    ArrayList<ImageCaptureManager.CapturedImageConstraint> singleCaptureConstraint = new ArrayList<ImageCaptureManager.CapturedImageConstraint>();
    singleCaptureConstraint.add(new ImageCaptureManager.CapturedImageConstraint() {
        @Override
        public boolean satisfiesConstraint(TotalCaptureResult captureResult) {
            Object tag = captureResult.getRequest().getTag();
            return tag == RequestTag.EXPLICIT_CAPTURE;
        }
    });

    // If we can use ZSL, try to save a previously-captured frame, if an
    // acceptable one exists in the buffer.
    if (useZSL) {
        boolean capturedPreviousFrame = mCaptureManager
                .tryCaptureExistingImage(new ImageCaptureTask(params, session), zslConstraints);
        if (capturedPreviousFrame) {
            Log.v(TAG, "Saving previous frame");
            onShutterInvokeUI(params);
        } else {
            Log.v(TAG, "No good image Available.  Capturing next available good image.");
            // If there was no good frame available in the ring buffer
            // already, capture the next good image.
            // TODO Disable the shutter button until this image is captured.

            Flash flashMode = Flash.OFF;

            if (flashMode == Flash.ON || flashMode == Flash.AUTO) {
                // We must issue a request for a single capture using the
                // flash, including an AE precapture trigger.

                // The following sets up a sequence of events which will
                // occur in reverse order to the associated method
                // calls:
                // 1. Send a request to trigger the Auto Exposure Precapture
                // 2. Wait for the AE_STATE to leave the PRECAPTURE state,
                // and then send a request for a single image, with the
                // appropriate flash settings.
                // 3. Capture the next appropriate image, which should be
                // the one we requested in (2).

                mCaptureManager.captureNextImage(new ImageCaptureTask(params, session),
                        singleCaptureConstraint);

                mCaptureManager.addMetadataChangeListener(CaptureResult.CONTROL_AE_STATE,
                        new MetadataChangeListener() {
                            @Override
                            public void onImageMetadataChange(Key<?> key, Object oldValue, Object newValue,
                                    CaptureResult result) {
                                Log.v(TAG, "AE State Changed");
                                if (oldValue
                                        .equals(Integer.valueOf(CaptureResult.CONTROL_AE_STATE_PRECAPTURE))) {
                                    mCaptureManager.removeMetadataChangeListener(key, this);
                                    sendSingleRequest(params);
                                    // TODO: Delay this until
                                    // onCaptureStarted().
                                    onShutterInvokeUI(params);
                                }
                            }
                        });

                sendAutoExposureTriggerRequest(flashMode);
            } else {
                // We may get here if, for example, the auto focus is in the
                // middle of a scan.
                // If the flash is off, we should just wait for the next
                // image that arrives. This will have minimal delay since we
                // do not need to send a new capture request.
                mCaptureManager.captureNextImage(new ImageCaptureTask(params, session), zslConstraints);
            }
        }
    } else {
        // TODO If we can't save a previous frame, create a new capture
        // request to do what we need (e.g. flash) and call
        // captureNextImage().
        throw new UnsupportedOperationException("Non-ZSL capture not yet supported");
    }
}

From source file:com.almalence.opencam.PluginManagerBase.java

@TargetApi(21)
public boolean addToSharedMemExifTagsFromCaptureResult(final CaptureResult result, final long SessionID,
        final int num) {
    String exposure_time = String.valueOf(result.get(CaptureResult.SENSOR_EXPOSURE_TIME));
    String sensitivity = String.valueOf(result.get(CaptureResult.SENSOR_SENSITIVITY));
    String aperture = String.valueOf(result.get(CaptureResult.LENS_APERTURE));
    String focal_lenght = String.valueOf(result.get(CaptureResult.LENS_FOCAL_LENGTH));
    String flash_mode = String.valueOf(result.get(CaptureResult.FLASH_MODE));
    String awb_mode = String.valueOf(result.get(CaptureResult.CONTROL_AWB_MODE));

    if (num != -1 && exposure_time != null && !exposure_time.equals("null"))
        addToSharedMem("exiftag_exposure_time" + num + SessionID, exposure_time);
    else if (exposure_time != null && !exposure_time.equals("null"))
        addToSharedMem("exiftag_exposure_time" + SessionID, exposure_time);
    if (sensitivity != null && !sensitivity.equals("null"))
        addToSharedMem("exiftag_iso" + SessionID, sensitivity);
    if (aperture != null && !aperture.equals("null"))
        addToSharedMem("exiftag_aperture" + SessionID, aperture);
    if (focal_lenght != null && !focal_lenght.equals("null"))
        addToSharedMem("exiftag_focal_lenght" + SessionID, focal_lenght);
    if (flash_mode != null && !flash_mode.equals("null"))
        addToSharedMem("exiftag_flash" + SessionID, flash_mode);
    if (awb_mode != null && !awb_mode.equals("null"))
        addToSharedMem("exiftag_white_balance" + SessionID, awb_mode);

    return true;/*from  w  ww .  j a  v a2  s . c  om*/
}