Skip to content

Commit

Permalink
feat: add property pauseAfterCapture to takePictureAsync (react-nativ…
Browse files Browse the repository at this point in the history
  • Loading branch information
eqyiel authored and n1ru4l committed Nov 6, 2018
1 parent 5b26315 commit f6f9f29
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 13 deletions.
16 changes: 10 additions & 6 deletions android/src/main/java/com/google/android/cameraview/Camera1.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import android.support.v4.util.SparseArrayCompat;
import android.view.SurfaceHolder;

import com.facebook.react.bridge.ReadableMap;

import java.io.File;
import java.io.IOException;
import java.util.List;
Expand Down Expand Up @@ -377,7 +379,7 @@ boolean getScanning() {
}

@Override
void takePicture() {
void takePicture(final ReadableMap options) {
if (!isCameraOpened()) {
throw new IllegalStateException(
"Camera is not ready. Call start() before takePicture().");
Expand All @@ -390,23 +392,25 @@ void takePicture() {
mCamera.autoFocus(new Camera.AutoFocusCallback() {
@Override
public void onAutoFocus(boolean success, Camera camera) {
takePictureInternal();
takePictureInternal(options);
}
});
} else {
takePictureInternal();
takePictureInternal(options);
}
}

void takePictureInternal() {
void takePictureInternal(final ReadableMap options) {
if (!isPictureCaptureInProgress.getAndSet(true)) {
mCamera.takePicture(null, null, null, new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
isPictureCaptureInProgress.set(false);
camera.cancelAutoFocus();
camera.startPreview();
mIsPreviewActive = true;
if (options.hasKey("pauseAfterCapture") && !options.getBoolean("pauseAfterCapture")) {
camera.startPreview();
mIsPreviewActive = true;
}
if (mIsScanning) {
camera.setPreviewCallback(Camera1.this);
}
Expand Down
18 changes: 15 additions & 3 deletions android/src/main/java/com/google/android/cameraview/Camera2.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
import android.os.Handler;
import android.os.Looper;

import com.facebook.react.bridge.ReadableMap;

import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
Expand Down Expand Up @@ -460,7 +462,9 @@ int getFlash() {
}

@Override
void takePicture() {
void takePicture(ReadableMap options) {
mCaptureCallback.setOptions(options);

if (mAutoFocus) {
lockFocus();
} else {
Expand Down Expand Up @@ -770,7 +774,7 @@ void startCaptureSession() {

@Override
public void resumePreview() {
startCaptureSession();
unlockFocus();
}

@Override
Expand Down Expand Up @@ -1049,7 +1053,10 @@ void captureStillPicture() {
public void onCaptureCompleted(@NonNull CameraCaptureSession session,
@NonNull CaptureRequest request,
@NonNull TotalCaptureResult result) {
unlockFocus();
if (mCaptureCallback.getOptions().hasKey("stopPreviewAfterCapture")
&& !mCaptureCallback.getOptions().getBoolean("stopPreviewAfterCapture")) {
unlockFocus();
}
}
}, null);
} catch (CameraAccessException e) {
Expand Down Expand Up @@ -1187,6 +1194,7 @@ private static abstract class PictureCaptureCallback
static final int STATE_CAPTURING = 5;

private int mState;
private ReadableMap mOptions = null;

PictureCaptureCallback() {
}
Expand All @@ -1195,6 +1203,10 @@ void setState(int state) {
mState = state;
}

void setOptions(ReadableMap options) { mOptions = options; }

ReadableMap getOptions() { return mOptions; }

@Override
public void onCaptureProgressed(@NonNull CameraCaptureSession session,
@NonNull CaptureRequest request, @NonNull CaptureResult partialResult) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import android.widget.FrameLayout;
import android.graphics.SurfaceTexture;

import com.facebook.react.bridge.ReadableMap;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
Expand Down Expand Up @@ -503,8 +505,8 @@ public int getWhiteBalance() {
* Take a picture. The result will be returned to
* {@link Callback#onPictureTaken(CameraView, byte[])}.
*/
public void takePicture() {
mImpl.takePicture();
public void takePicture(ReadableMap options) {
mImpl.takePicture(options);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import android.view.View;
import android.graphics.SurfaceTexture;

import com.facebook.react.bridge.ReadableMap;

import java.util.Set;
import java.util.SortedSet;

Expand Down Expand Up @@ -74,7 +76,7 @@ View getView() {

abstract int getFlash();

abstract void takePicture();
abstract void takePicture(ReadableMap options);

abstract boolean record(String path, int maxDuration, int maxFileSize,
boolean recordAudio, CamcorderProfile profile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ public void takePicture(ReadableMap options, final Promise promise, File cacheDi
sound.play(MediaActionSound.SHUTTER_CLICK);
}
try {
super.takePicture();
super.takePicture(options);
} catch (Exception e) {
mPictureTakenPromises.remove(promise);
mPictureTakenOptions.remove(promise);
Expand Down
2 changes: 2 additions & 0 deletions docs/RNCamera.md
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,8 @@ Supported options:
- `doNotSave` (boolean true or false). Use this with `true` if you do not want the picture to be saved as a file to cache. If no value is specified `doNotSave:false` is used. If you only need the base64 for the image, you can use this with `base64:true` and avoid having to save the file.


- `pauseAfterCapture` (boolean true or false). If true, pause the preview layer immediately after capturing the image. You will need to call `cameraRef.resumePreview()` before using the camera again. If no value is specified `pauseAfterCapture:false` is used.

The promise will be fulfilled with an object with some of the following properties:

- `width`: returns the image's width (taking image orientation into account)
Expand Down
4 changes: 4 additions & 0 deletions ios/RN/RNCamera.m
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,10 @@ - (void)takePicture:(NSDictionary *)options resolve:(RCTPromiseResolveBlock)reso
[connection setVideoOrientation:orientation];
[self.stillImageOutput captureStillImageAsynchronouslyFromConnection:connection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error) {
if (imageSampleBuffer && !error) {
if ([options[@"pauseAfterCapture"] boolValue]) {
[[self.previewLayer connection] setEnabled:NO];
}

BOOL useFastMode = options[@"fastMode"] && [options[@"fastMode"] boolValue];
if (useFastMode) {
resolve(nil);
Expand Down
6 changes: 6 additions & 0 deletions src/RNCamera.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type PictureOptions = {
width?: number,
fixOrientation?: boolean,
forceUpOrientation?: boolean,
pauseAfterCapture?: boolean,
};

type TrackedFaceFeature = FaceFeature & {
Expand Down Expand Up @@ -276,6 +277,11 @@ export default class Camera extends React.Component<PropsType, StateType> {
if (options.orientation) {
options.orientation = CameraManager.Orientation[options.orientation];
}

if (options.pauseAfterCapture === undefined) {
options.pauseAfterCapture = false;
}

return await CameraManager.takePicture(options, this._cameraHandle);
}

Expand Down

0 comments on commit f6f9f29

Please sign in to comment.