Skip to content
This repository was archived by the owner on Jun 16, 2023. It is now read-only.

Commit f6f9f29

Browse files
eqyieln1ru4l
authored andcommitted
feat: add property pauseAfterCapture to takePictureAsync (#1641)
1 parent 5b26315 commit f6f9f29

File tree

8 files changed

+45
-13
lines changed

8 files changed

+45
-13
lines changed

android/src/main/java/com/google/android/cameraview/Camera1.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import android.support.v4.util.SparseArrayCompat;
2626
import android.view.SurfaceHolder;
2727

28+
import com.facebook.react.bridge.ReadableMap;
29+
2830
import java.io.File;
2931
import java.io.IOException;
3032
import java.util.List;
@@ -377,7 +379,7 @@ boolean getScanning() {
377379
}
378380

379381
@Override
380-
void takePicture() {
382+
void takePicture(final ReadableMap options) {
381383
if (!isCameraOpened()) {
382384
throw new IllegalStateException(
383385
"Camera is not ready. Call start() before takePicture().");
@@ -390,23 +392,25 @@ void takePicture() {
390392
mCamera.autoFocus(new Camera.AutoFocusCallback() {
391393
@Override
392394
public void onAutoFocus(boolean success, Camera camera) {
393-
takePictureInternal();
395+
takePictureInternal(options);
394396
}
395397
});
396398
} else {
397-
takePictureInternal();
399+
takePictureInternal(options);
398400
}
399401
}
400402

401-
void takePictureInternal() {
403+
void takePictureInternal(final ReadableMap options) {
402404
if (!isPictureCaptureInProgress.getAndSet(true)) {
403405
mCamera.takePicture(null, null, null, new Camera.PictureCallback() {
404406
@Override
405407
public void onPictureTaken(byte[] data, Camera camera) {
406408
isPictureCaptureInProgress.set(false);
407409
camera.cancelAutoFocus();
408-
camera.startPreview();
409-
mIsPreviewActive = true;
410+
if (options.hasKey("pauseAfterCapture") && !options.getBoolean("pauseAfterCapture")) {
411+
camera.startPreview();
412+
mIsPreviewActive = true;
413+
}
410414
if (mIsScanning) {
411415
camera.setPreviewCallback(Camera1.this);
412416
}

android/src/main/java/com/google/android/cameraview/Camera2.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
import android.os.Handler;
4242
import android.os.Looper;
4343

44+
import com.facebook.react.bridge.ReadableMap;
45+
4446
import java.io.File;
4547
import java.io.IOException;
4648
import java.nio.ByteBuffer;
@@ -460,7 +462,9 @@ int getFlash() {
460462
}
461463

462464
@Override
463-
void takePicture() {
465+
void takePicture(ReadableMap options) {
466+
mCaptureCallback.setOptions(options);
467+
464468
if (mAutoFocus) {
465469
lockFocus();
466470
} else {
@@ -770,7 +774,7 @@ void startCaptureSession() {
770774

771775
@Override
772776
public void resumePreview() {
773-
startCaptureSession();
777+
unlockFocus();
774778
}
775779

776780
@Override
@@ -1049,7 +1053,10 @@ void captureStillPicture() {
10491053
public void onCaptureCompleted(@NonNull CameraCaptureSession session,
10501054
@NonNull CaptureRequest request,
10511055
@NonNull TotalCaptureResult result) {
1052-
unlockFocus();
1056+
if (mCaptureCallback.getOptions().hasKey("stopPreviewAfterCapture")
1057+
&& !mCaptureCallback.getOptions().getBoolean("stopPreviewAfterCapture")) {
1058+
unlockFocus();
1059+
}
10531060
}
10541061
}, null);
10551062
} catch (CameraAccessException e) {
@@ -1187,6 +1194,7 @@ private static abstract class PictureCaptureCallback
11871194
static final int STATE_CAPTURING = 5;
11881195

11891196
private int mState;
1197+
private ReadableMap mOptions = null;
11901198

11911199
PictureCaptureCallback() {
11921200
}
@@ -1195,6 +1203,10 @@ void setState(int state) {
11951203
mState = state;
11961204
}
11971205

1206+
void setOptions(ReadableMap options) { mOptions = options; }
1207+
1208+
ReadableMap getOptions() { return mOptions; }
1209+
11981210
@Override
11991211
public void onCaptureProgressed(@NonNull CameraCaptureSession session,
12001212
@NonNull CaptureRequest request, @NonNull CaptureResult partialResult) {

android/src/main/java/com/google/android/cameraview/CameraView.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
import android.widget.FrameLayout;
3434
import android.graphics.SurfaceTexture;
3535

36+
import com.facebook.react.bridge.ReadableMap;
37+
3638
import java.lang.annotation.Retention;
3739
import java.lang.annotation.RetentionPolicy;
3840
import java.util.ArrayList;
@@ -503,8 +505,8 @@ public int getWhiteBalance() {
503505
* Take a picture. The result will be returned to
504506
* {@link Callback#onPictureTaken(CameraView, byte[])}.
505507
*/
506-
public void takePicture() {
507-
mImpl.takePicture();
508+
public void takePicture(ReadableMap options) {
509+
mImpl.takePicture(options);
508510
}
509511

510512
/**

android/src/main/java/com/google/android/cameraview/CameraViewImpl.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import android.view.View;
2121
import android.graphics.SurfaceTexture;
2222

23+
import com.facebook.react.bridge.ReadableMap;
24+
2325
import java.util.Set;
2426
import java.util.SortedSet;
2527

@@ -74,7 +76,7 @@ View getView() {
7476

7577
abstract int getFlash();
7678

77-
abstract void takePicture();
79+
abstract void takePicture(ReadableMap options);
7880

7981
abstract boolean record(String path, int maxDuration, int maxFileSize,
8082
boolean recordAudio, CamcorderProfile profile);

android/src/main/java/org/reactnative/camera/RNCameraView.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ public void takePicture(ReadableMap options, final Promise promise, File cacheDi
239239
sound.play(MediaActionSound.SHUTTER_CLICK);
240240
}
241241
try {
242-
super.takePicture();
242+
super.takePicture(options);
243243
} catch (Exception e) {
244244
mPictureTakenPromises.remove(promise);
245245
mPictureTakenOptions.remove(promise);

docs/RNCamera.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,8 @@ Supported options:
426426
- `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.
427427

428428

429+
- `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.
430+
429431
The promise will be fulfilled with an object with some of the following properties:
430432

431433
- `width`: returns the image's width (taking image orientation into account)

ios/RN/RNCamera.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,10 @@ - (void)takePicture:(NSDictionary *)options resolve:(RCTPromiseResolveBlock)reso
391391
[connection setVideoOrientation:orientation];
392392
[self.stillImageOutput captureStillImageAsynchronouslyFromConnection:connection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error) {
393393
if (imageSampleBuffer && !error) {
394+
if ([options[@"pauseAfterCapture"] boolValue]) {
395+
[[self.previewLayer connection] setEnabled:NO];
396+
}
397+
394398
BOOL useFastMode = options[@"fastMode"] && [options[@"fastMode"] boolValue];
395399
if (useFastMode) {
396400
resolve(nil);

src/RNCamera.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ type PictureOptions = {
4141
width?: number,
4242
fixOrientation?: boolean,
4343
forceUpOrientation?: boolean,
44+
pauseAfterCapture?: boolean,
4445
};
4546

4647
type TrackedFaceFeature = FaceFeature & {
@@ -276,6 +277,11 @@ export default class Camera extends React.Component<PropsType, StateType> {
276277
if (options.orientation) {
277278
options.orientation = CameraManager.Orientation[options.orientation];
278279
}
280+
281+
if (options.pauseAfterCapture === undefined) {
282+
options.pauseAfterCapture = false;
283+
}
284+
279285
return await CameraManager.takePicture(options, this._cameraHandle);
280286
}
281287

0 commit comments

Comments
 (0)