mirror of
https://github.com/status-im/react-native-camera.git
synced 2026-08-31 13:41:08 +00:00
add captureQuality option
This commit is contained in:
@@ -59,6 +59,79 @@ public class RCTCamera {
|
||||
return cameraInfo.previewHeight;
|
||||
}
|
||||
|
||||
public Camera.Size getBestPreviewSize(int type, int width, int height)
|
||||
{
|
||||
Camera camera = _cameras.get(type);
|
||||
Camera.Size result = null;
|
||||
if(camera == null) {
|
||||
return null;
|
||||
}
|
||||
Camera.Parameters params = camera.getParameters();
|
||||
for (Camera.Size size : params.getSupportedPreviewSizes()) {
|
||||
if (size.width <= width && size.height <= height) {
|
||||
if (result == null) {
|
||||
result = size;
|
||||
} else {
|
||||
int resultArea = result.width * result.height;
|
||||
int newArea = size.width * size.height;
|
||||
|
||||
if (newArea > resultArea) {
|
||||
result = size;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public Camera.Size getBestPictureSize(int type, int width, int height)
|
||||
{
|
||||
Camera camera = _cameras.get(type);
|
||||
Camera.Size result = null;
|
||||
if(camera == null) {
|
||||
return null;
|
||||
}
|
||||
Camera.Parameters params = camera.getParameters();
|
||||
for (Camera.Size size : params.getSupportedPictureSizes()) {
|
||||
if (size.width <= width && size.height <= height) {
|
||||
if (result == null) {
|
||||
result = size;
|
||||
} else {
|
||||
int resultArea = result.width * result.height;
|
||||
int newArea = size.width * size.height;
|
||||
|
||||
if (newArea > resultArea) {
|
||||
result = size;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public Camera.Size getSmallestPictureSize(int type)
|
||||
{
|
||||
Camera camera = _cameras.get(type);
|
||||
Camera.Size result = null;
|
||||
if(camera == null) {
|
||||
return null;
|
||||
}
|
||||
Camera.Parameters params = camera.getParameters();
|
||||
for (Camera.Size size : params.getSupportedPictureSizes()) {
|
||||
if (result == null) {
|
||||
result = size;
|
||||
} else {
|
||||
int resultArea = result.width * result.height;
|
||||
int newArea = size.width * size.height;
|
||||
|
||||
if (newArea < resultArea) {
|
||||
result = size;
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public void setOrientation(int orientation) {
|
||||
if (_orientation == orientation) {
|
||||
return;
|
||||
@@ -74,6 +147,33 @@ public class RCTCamera {
|
||||
adjustPreviewLayout(RCTCameraModule.RCT_CAMERA_TYPE_BACK);
|
||||
}
|
||||
|
||||
public void setCaptureQuality(int cameraType, String captureQuality) {
|
||||
Camera camera = _cameras.get(cameraType);
|
||||
if (null == camera) {
|
||||
return;
|
||||
}
|
||||
|
||||
Camera.Parameters parameters = camera.getParameters();
|
||||
Camera.Size pictureSize = null;
|
||||
switch (captureQuality) {
|
||||
case "low":
|
||||
pictureSize = getSmallestPictureSize(cameraType); // select the lowest res
|
||||
break;
|
||||
case "medium":
|
||||
List<Camera.Size> sizes = parameters.getSupportedPictureSizes();
|
||||
pictureSize = sizes.get(sizes.size() / 2);
|
||||
break;
|
||||
case "high":
|
||||
pictureSize = getBestPictureSize(cameraType, Integer.MAX_VALUE, Integer.MAX_VALUE); // select the highest res
|
||||
break;
|
||||
}
|
||||
|
||||
if (pictureSize != null) {
|
||||
parameters.setPictureSize(pictureSize.width, pictureSize.height);
|
||||
camera.setParameters(parameters);
|
||||
}
|
||||
}
|
||||
|
||||
public void setTorchMode(int cameraType, int torchMode) {
|
||||
Camera camera = _cameras.get(cameraType);
|
||||
if (null == camera) {
|
||||
@@ -150,8 +250,10 @@ public class RCTCamera {
|
||||
parameters.setRotation(cameraInfo.rotation);
|
||||
|
||||
// set preview size
|
||||
int width = parameters.getSupportedPreviewSizes().get(0).width;
|
||||
int height = parameters.getSupportedPreviewSizes().get(0).height;
|
||||
// defaults to highest resolution available
|
||||
Camera.Size optimalPreviewSize = getBestPreviewSize(type, Integer.MAX_VALUE, Integer.MAX_VALUE);
|
||||
int width = optimalPreviewSize.width;
|
||||
int height = optimalPreviewSize.height;
|
||||
|
||||
parameters.setPreviewSize(width, height);
|
||||
try {
|
||||
|
||||
@@ -17,6 +17,7 @@ public class RCTCameraView extends ViewGroup {
|
||||
private RCTCameraViewFinder _viewFinder = null;
|
||||
private int _actualDeviceOrientation = -1;
|
||||
private int _aspect = RCTCameraModule.RCT_CAMERA_ASPECT_FIT;
|
||||
private String _captureQuality = "high";
|
||||
private int _torchMode = -1;
|
||||
private int _flashMode = -1;
|
||||
|
||||
@@ -66,6 +67,13 @@ public class RCTCameraView extends ViewGroup {
|
||||
}
|
||||
}
|
||||
|
||||
public void setCaptureQuality(String captureQuality) {
|
||||
this._captureQuality = captureQuality;
|
||||
if (this._viewFinder != null) {
|
||||
this._viewFinder.setCaptureQuality(captureQuality);
|
||||
}
|
||||
}
|
||||
|
||||
public void setTorchMode(int torchMode) {
|
||||
this._torchMode = torchMode;
|
||||
if (this._viewFinder != null) {
|
||||
|
||||
@@ -65,6 +65,10 @@ class RCTCameraViewFinder extends TextureView implements TextureView.SurfaceText
|
||||
}).start();
|
||||
}
|
||||
|
||||
public void setCaptureQuality(String captureQuality) {
|
||||
RCTCamera.getInstance().setCaptureQuality(_cameraType, captureQuality);
|
||||
}
|
||||
|
||||
public void setTorchMode(int torchMode) {
|
||||
RCTCamera.getInstance().setTorchMode(_cameraType, torchMode);
|
||||
}
|
||||
@@ -91,10 +95,16 @@ class RCTCameraViewFinder extends TextureView implements TextureView.SurfaceText
|
||||
try {
|
||||
_camera = RCTCamera.getInstance().acquireCameraInstance(_cameraType);
|
||||
Camera.Parameters parameters = _camera.getParameters();
|
||||
// set autofocus
|
||||
List<String> focusModes = parameters.getSupportedFocusModes();
|
||||
if (focusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) {
|
||||
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
|
||||
}
|
||||
// set picture size
|
||||
// defaults to max available size
|
||||
Camera.Size optimalPictureSize = RCTCamera.getInstance().getBestPictureSize(_cameraType, Integer.MAX_VALUE, Integer.MAX_VALUE);
|
||||
parameters.setPictureSize(optimalPictureSize.width, optimalPictureSize.height);
|
||||
|
||||
_camera.setParameters(parameters);
|
||||
_camera.setPreviewTexture(_surfaceTexture);
|
||||
_camera.startPreview();
|
||||
|
||||
@@ -37,6 +37,11 @@ public class RCTCameraViewManager extends ViewGroupManager<RCTCameraView> {
|
||||
view.setCameraType(type);
|
||||
}
|
||||
|
||||
@ReactProp(name = "captureQuality")
|
||||
public void setCaptureQuality(RCTCameraView view, String captureQuality) {
|
||||
view.setCaptureQuality(captureQuality);
|
||||
}
|
||||
|
||||
@ReactProp(name = "torchMode")
|
||||
public void setTorchMode(RCTCameraView view, int torchMode) {
|
||||
view.setTorchMode(torchMode);
|
||||
|
||||
Reference in New Issue
Block a user