Skip to content

Commit

Permalink
feat(camera): option to select front or back
Browse files Browse the repository at this point in the history
  • Loading branch information
ravirajn22 committed Mar 15, 2021
1 parent 251950c commit c57569a
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ The `callback` will be called with a response object, refer to [The Response Obj
| videoQuality | OK | OK | 'low', 'medium', or 'high' on iOS, 'low' or 'high' on Android |
| durationLimit | OK | OK | Video max duration in seconds |
| quality | OK | OK | 0 to 1, photos |
| cameraType | OK | OK | 'back' or 'front'. May not be supported in few android devices |
| includeBase64 | OK | OK | If true, creates base64 string of the image (Avoid using on large image files due to performance) |
| saveToPhotos | OK | OK | (Boolean) Only for launchCamera, saves the image/video file captured to public photo |
Expand Down
5 changes: 5 additions & 0 deletions android/src/main/java/com/imagepicker/ImagePickerModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ public void launchCamera(final ReadableMap options, final Callback callback) {
cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraCaptureURI = createUri(createFile(reactContext, "jpg"), reactContext);
}

if (this.options.useFrontCamera) {
setFrontCamera(cameraIntent);
}

cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, cameraCaptureURI);

if (cameraIntent.resolveActivity(reactContext.getPackageManager()) == null) {
Expand Down
5 changes: 5 additions & 0 deletions android/src/main/java/com/imagepicker/Options.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class Options {
int maxHeight;
Boolean saveToPhotos;
int durationLimit;
Boolean useFrontCamera = false;


Options(ReadableMap options) {
Expand All @@ -25,6 +26,10 @@ public class Options {
videoQuality = 0;
}

if (options.getString("cameraType").equals("front")) {
useFrontCamera = true;
}

quality = (int) (options.getDouble("quality") * 100);
maxHeight = options.getInt("maxHeight");
maxWidth = options.getInt("maxWidth");
Expand Down
15 changes: 15 additions & 0 deletions android/src/main/java/com/imagepicker/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.camera2.CameraCharacteristics;
import android.net.Uri;
import android.os.Build;
import android.os.ParcelFileDescriptor;
import android.provider.MediaStore;
import android.provider.OpenableColumns;
Expand Down Expand Up @@ -116,6 +119,18 @@ public static boolean isCameraAvailable(Context reactContext) {
|| reactContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY);
}

// Opening front camera is not officially supported in android, the below hack is obtained from various online sources
public static void setFrontCamera(Intent intent) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
intent.putExtra("android.intent.extras.CAMERA_FACING", CameraCharacteristics.LENS_FACING_FRONT);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
intent.putExtra("android.intent.extra.USE_FRONT_CAMERA", true);
}
} else {
intent.putExtra("android.intent.extras.CAMERA_FACING", 1);
}
}

public static int[] getImageDimensions(Uri uri, Context reactContext) {
InputStream inputStream;
try {
Expand Down
7 changes: 6 additions & 1 deletion ios/ImagePickerUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ @implementation ImagePickerUtils
+ (void) setupPickerFromOptions:(UIImagePickerController *)picker options:(NSDictionary *)options target:(RNImagePickerTarget)target {
if (target == camera) {
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.cameraDevice = UIImagePickerControllerCameraDeviceRear;

if ([options[@"cameraType"] isEqualToString:@"front"]) {
picker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
} else {
picker.cameraDevice = UIImagePickerControllerCameraDeviceRear;
}
}
else {
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ const DEFAULT_OPTIONS: CameraOptions = {
maxHeight: 0,
includeBase64: false,
saveToPhotos: false,
durationLimit: 0
durationLimit: 0,
cameraType: 'back'
};

export function launchCamera(options: CameraOptions, callback: Callback) {
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface ImageLibraryOptions {
export interface CameraOptions extends ImageLibraryOptions {
durationLimit?: number;
saveToPhotos?: boolean;
cameraType?: string;
}

export interface ImagePickerResponse {
Expand Down

0 comments on commit c57569a

Please sign in to comment.