diff --git a/README.md b/README.md index 55f8b4b..2469be9 100644 --- a/README.md +++ b/README.md @@ -161,6 +161,8 @@ This property allows you to specify the quality output of the captured image or When choosing more-specific quality settings (1080p, 720p, 480p), note that each platform and device supports different valid picture/video sizes, and actual resolution within each of these quality settings might differ. There should not be too much variance (if any) for iOS; 1080p should give 1920x1080, 720p should give 1280x720, and 480p should give 640x480 (note that iOS 480p therefore is NOT the typical 16:9 HD aspect ratio, and the typically-HD camera preview screen may differ greatly in aspect from what you actually record!!). For Android, expect more variance: on most Androids, 1080p *should* give 1920x1080 and 720p *should* give 1280x720; however, 480p will at "best" be 853x480 (16:9 HD aspect ratio), but falls back/down to 800x480, 720x480, or "worse", depending on what is closest-but-less-than 853x480 and available on the actual device. If your application requires knowledge of the precise resolution of the output image/video, you might consider manually determine the actual resolution itself after capture has completed (particularly for 480p on Android). +Android also supports `Camera.constants.CaptureQuality.preview` or `"preview"` which matches the output image to the same one used in the preview + #### `type` Values: `Camera.constants.Type.front` or `"front"`, `Camera.constants.Type.back` or `"back"` (default) diff --git a/android/src/main/java/com/lwansbrough/RCTCamera/RCTCamera.java b/android/src/main/java/com/lwansbrough/RCTCamera/RCTCamera.java index 37b6324..063a40a 100644 --- a/android/src/main/java/com/lwansbrough/RCTCamera/RCTCamera.java +++ b/android/src/main/java/com/lwansbrough/RCTCamera/RCTCamera.java @@ -11,6 +11,7 @@ import android.util.Log; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.lang.Math; public class RCTCamera { private static RCTCamera ourInstance; @@ -112,6 +113,24 @@ public class RCTCamera { return smallestSize; } + private Camera.Size getClosestSize(List supportedSizes, int matchWidth, int matchHeight) { + Camera.Size closestSize = null; + for (Camera.Size size : supportedSizes) { + if (closestSize == null) { + closestSize = size; + continue; + } + + int currentDelta = Math.abs(closestSize.width - matchWidth) * Math.abs(closestSize.height - matchHeight); + int newDelta = Math.abs(size.width - matchWidth) * Math.abs(size.height - matchHeight); + + if (newDelta < currentDelta) { + closestSize = size; + } + } + return closestSize; + } + protected List getSupportedVideoSizes(Camera camera) { Camera.Parameters params = camera.getParameters(); // defer to preview instead of params.getSupportedVideoSizes() http://bit.ly/1rxOsq0 @@ -203,7 +222,11 @@ public class RCTCamera { pictureSize = supportedSizes.get(supportedSizes.size() / 2); break; case RCTCameraModule.RCT_CAMERA_CAPTURE_QUALITY_HIGH: - pictureSize = getBestSize(supportedSizes, Integer.MAX_VALUE, Integer.MAX_VALUE); + pictureSize = getBestSize(parameters.getSupportedPictureSizes(), Integer.MAX_VALUE, Integer.MAX_VALUE); + break; + case RCTCameraModule.RCT_CAMERA_CAPTURE_QUALITY_PREVIEW: + Camera.Size optimalPreviewSize = getBestSize(parameters.getSupportedPreviewSizes(), Integer.MAX_VALUE, Integer.MAX_VALUE); + pictureSize = getClosestSize(parameters.getSupportedPictureSizes(), optimalPreviewSize.width, optimalPreviewSize.height); break; case RCTCameraModule.RCT_CAMERA_CAPTURE_QUALITY_480P: pictureSize = getBestSize(supportedSizes, RESOLUTION_480P.width, RESOLUTION_480P.height); diff --git a/android/src/main/java/com/lwansbrough/RCTCamera/RCTCameraModule.java b/android/src/main/java/com/lwansbrough/RCTCamera/RCTCameraModule.java index d3ee2cc..d30313f 100644 --- a/android/src/main/java/com/lwansbrough/RCTCamera/RCTCameraModule.java +++ b/android/src/main/java/com/lwansbrough/RCTCamera/RCTCameraModule.java @@ -73,6 +73,7 @@ public class RCTCameraModule extends ReactContextBaseJavaModule public static final int RCT_CAMERA_TORCH_MODE_OFF = 0; public static final int RCT_CAMERA_TORCH_MODE_ON = 1; public static final int RCT_CAMERA_TORCH_MODE_AUTO = 2; + public static final String RCT_CAMERA_CAPTURE_QUALITY_PREVIEW = "preview"; public static final String RCT_CAMERA_CAPTURE_QUALITY_HIGH = "high"; public static final String RCT_CAMERA_CAPTURE_QUALITY_MEDIUM = "medium"; public static final String RCT_CAMERA_CAPTURE_QUALITY_LOW = "low"; @@ -196,6 +197,7 @@ public class RCTCameraModule extends ReactContextBaseJavaModule put("medium", RCT_CAMERA_CAPTURE_QUALITY_MEDIUM); put("high", RCT_CAMERA_CAPTURE_QUALITY_HIGH); put("photo", RCT_CAMERA_CAPTURE_QUALITY_HIGH); + put("preview", RCT_CAMERA_CAPTURE_QUALITY_PREVIEW); put("480p", RCT_CAMERA_CAPTURE_QUALITY_480P); put("720p", RCT_CAMERA_CAPTURE_QUALITY_720P); put("1080p", RCT_CAMERA_CAPTURE_QUALITY_1080P);