Merge pull request #1155 from mvpstars/feature/fix-crop-to-preview-android
Fix cropToPreview on Android
This commit is contained in:
commit
c0202dc390
|
@ -85,24 +85,23 @@ public class MutableImage {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void cropToPreview(int orientation, float xPercentage, float yPercentage) throws IllegalArgumentException {
|
public void cropToPreview(double previewRatio) throws IllegalArgumentException {
|
||||||
int width = getWidth();
|
int pictureWidth = getWidth(), pictureHeight = getHeight();
|
||||||
int height = getHeight();
|
int targetPictureWidth, targetPictureHeight;
|
||||||
|
|
||||||
int x = (int) Math.round(height * xPercentage);
|
if (previewRatio * pictureHeight > pictureWidth) {
|
||||||
int y = (int) Math.round(width * yPercentage);
|
targetPictureWidth = pictureWidth;
|
||||||
|
targetPictureHeight = (int) (pictureWidth / previewRatio);
|
||||||
if (orientation == Configuration.ORIENTATION_PORTRAIT) {
|
|
||||||
int cropWidth = width - (y*2);
|
|
||||||
int cropHeight = height - (x*2);
|
|
||||||
|
|
||||||
this.currentRepresentation = Bitmap.createBitmap(this.currentRepresentation, y, x, cropWidth, cropHeight);
|
|
||||||
} else {
|
} else {
|
||||||
int cropWidth = width - (x*2);
|
targetPictureHeight = pictureHeight;
|
||||||
int cropHeight = height - (y*2);
|
targetPictureWidth = (int) (pictureHeight * previewRatio);
|
||||||
|
|
||||||
this.currentRepresentation = Bitmap.createBitmap(this.currentRepresentation, x, y, cropWidth, cropHeight);
|
|
||||||
}
|
}
|
||||||
|
this.currentRepresentation = Bitmap.createBitmap(
|
||||||
|
this.currentRepresentation,
|
||||||
|
(pictureWidth - targetPictureWidth) / 2,
|
||||||
|
(pictureHeight - targetPictureHeight) / 2,
|
||||||
|
targetPictureWidth,
|
||||||
|
targetPictureHeight);
|
||||||
}
|
}
|
||||||
|
|
||||||
//see http://www.impulseadventure.com/photo/exif-orientation.html
|
//see http://www.impulseadventure.com/photo/exif-orientation.html
|
||||||
|
|
|
@ -74,20 +74,20 @@ public class RCTCamera {
|
||||||
return cameraInfo.previewHeight;
|
return cameraInfo.previewHeight;
|
||||||
}
|
}
|
||||||
|
|
||||||
public float getPreviewPaddingHeight(int type) {
|
public int getPreviewVisibleHeight(int type) {
|
||||||
CameraInfoWrapper cameraInfo = _cameraInfos.get(type);
|
CameraInfoWrapper cameraInfo = _cameraInfos.get(type);
|
||||||
if (null == cameraInfo) {
|
if (null == cameraInfo) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
return cameraInfo.previewPaddingHeight;
|
return cameraInfo.previewVisibleHeight;
|
||||||
}
|
}
|
||||||
|
|
||||||
public float getPreviewPaddingWidth(int type) {
|
public int getPreviewVisibleWidth(int type) {
|
||||||
CameraInfoWrapper cameraInfo = _cameraInfos.get(type);
|
CameraInfoWrapper cameraInfo = _cameraInfos.get(type);
|
||||||
if (null == cameraInfo) {
|
if (null == cameraInfo) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
return cameraInfo.previewPaddingWidth;
|
return cameraInfo.previewVisibleWidth;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Camera.Size getBestSize(List<Camera.Size> supportedSizes, int maxWidth, int maxHeight) {
|
public Camera.Size getBestSize(List<Camera.Size> supportedSizes, int maxWidth, int maxHeight) {
|
||||||
|
@ -451,14 +451,14 @@ public class RCTCamera {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setPreviewPadding(int type, float width, float height) {
|
public void setPreviewVisibleSize(int type, int width, int height) {
|
||||||
CameraInfoWrapper cameraInfo = _cameraInfos.get(type);
|
CameraInfoWrapper cameraInfo = _cameraInfos.get(type);
|
||||||
if (cameraInfo == null) {
|
if (cameraInfo == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
cameraInfo.previewPaddingWidth = Math.abs(width);
|
cameraInfo.previewVisibleWidth = width;
|
||||||
cameraInfo.previewPaddingHeight = Math.abs(height);
|
cameraInfo.previewVisibleHeight = height;
|
||||||
}
|
}
|
||||||
|
|
||||||
private RCTCamera(int deviceOrientation) {
|
private RCTCamera(int deviceOrientation) {
|
||||||
|
@ -491,8 +491,8 @@ public class RCTCamera {
|
||||||
public int rotation = 0;
|
public int rotation = 0;
|
||||||
public int previewWidth = -1;
|
public int previewWidth = -1;
|
||||||
public int previewHeight = -1;
|
public int previewHeight = -1;
|
||||||
public float previewPaddingHeight = -1;
|
public int previewVisibleWidth = -1;
|
||||||
public float previewPaddingWidth = -1;
|
public int previewVisibleHeight = -1;
|
||||||
|
|
||||||
public CameraInfoWrapper(Camera.CameraInfo info) {
|
public CameraInfoWrapper(Camera.CameraInfo info) {
|
||||||
this.info = info;
|
this.info = info;
|
||||||
|
|
|
@ -577,8 +577,6 @@ public class RCTCameraModule extends ReactContextBaseJavaModule
|
||||||
* concurrently which would blow the memory (esp on smaller devices), and slow things down.
|
* concurrently which would blow the memory (esp on smaller devices), and slow things down.
|
||||||
*/
|
*/
|
||||||
private synchronized void processImage(MutableImage mutableImage, ReadableMap options, Promise promise) {
|
private synchronized void processImage(MutableImage mutableImage, ReadableMap options, Promise promise) {
|
||||||
int orientation = _reactContext.getResources().getConfiguration().orientation;
|
|
||||||
|
|
||||||
boolean shouldFixOrientation = options.hasKey("fixOrientation") && options.getBoolean("fixOrientation");
|
boolean shouldFixOrientation = options.hasKey("fixOrientation") && options.getBoolean("fixOrientation");
|
||||||
if(shouldFixOrientation) {
|
if(shouldFixOrientation) {
|
||||||
try {
|
try {
|
||||||
|
@ -588,14 +586,20 @@ public class RCTCameraModule extends ReactContextBaseJavaModule
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
boolean needsReorient = false;
|
||||||
|
double previewRatio, pictureRatio = (double) mutableImage.getWidth() / (double) mutableImage.getHeight();
|
||||||
|
try {
|
||||||
|
int type = options.getInt("type");
|
||||||
|
previewRatio = (double) RCTCamera.getInstance().getPreviewVisibleWidth(type) / (double) RCTCamera.getInstance().getPreviewVisibleHeight(type);
|
||||||
|
needsReorient = (previewRatio > 1) != (pictureRatio > 1);
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
previewRatio = pictureRatio;
|
||||||
|
}
|
||||||
|
|
||||||
boolean shouldCropToPreview = options.hasKey("cropToPreview") && options.getBoolean("cropToPreview");
|
boolean shouldCropToPreview = options.hasKey("cropToPreview") && options.getBoolean("cropToPreview");
|
||||||
if (shouldCropToPreview) {
|
if (shouldCropToPreview) {
|
||||||
try {
|
try {
|
||||||
int type = options.getInt("type");
|
mutableImage.cropToPreview(needsReorient ? 1.0 / previewRatio : previewRatio);
|
||||||
float paddingWidth = RCTCamera.getInstance().getPreviewPaddingWidth(type);
|
|
||||||
float paddingHeight = RCTCamera.getInstance().getPreviewPaddingHeight(type);
|
|
||||||
|
|
||||||
mutableImage.cropToPreview(orientation, paddingWidth, paddingHeight);
|
|
||||||
} catch (IllegalArgumentException e) {
|
} catch (IllegalArgumentException e) {
|
||||||
promise.reject("Error cropping image to preview", e);
|
promise.reject("Error cropping image to preview", e);
|
||||||
}
|
}
|
||||||
|
@ -615,8 +619,8 @@ public class RCTCameraModule extends ReactContextBaseJavaModule
|
||||||
jpegQualityPercent = options.getInt("jpegQuality");
|
jpegQualityPercent = options.getInt("jpegQuality");
|
||||||
}
|
}
|
||||||
|
|
||||||
int imgWidth = (orientation == Configuration.ORIENTATION_PORTRAIT) ? mutableImage.getHeight() : mutableImage.getWidth();
|
int imgWidth = (needsReorient) ? mutableImage.getHeight() : mutableImage.getWidth();
|
||||||
int imgHeight = (orientation == Configuration.ORIENTATION_PORTRAIT) ? mutableImage.getWidth() : mutableImage.getHeight();
|
int imgHeight = (needsReorient) ? mutableImage.getWidth() : mutableImage.getHeight();
|
||||||
|
|
||||||
switch (options.getInt("target")) {
|
switch (options.getInt("target")) {
|
||||||
case RCT_CAMERA_CAPTURE_TARGET_MEMORY:
|
case RCT_CAMERA_CAPTURE_TARGET_MEMORY:
|
||||||
|
|
|
@ -210,10 +210,7 @@ public class RCTCameraView extends ViewGroup {
|
||||||
int viewFinderPaddingX = (int) ((width - viewfinderWidth) / 2);
|
int viewFinderPaddingX = (int) ((width - viewfinderWidth) / 2);
|
||||||
int viewFinderPaddingY = (int) ((height - viewfinderHeight) / 2);
|
int viewFinderPaddingY = (int) ((height - viewfinderHeight) / 2);
|
||||||
|
|
||||||
float paddingXPercentage = (float)viewFinderPaddingX / (float)viewfinderWidth;
|
RCTCamera.getInstance().setPreviewVisibleSize(_viewFinder.getCameraType(), (int) width, (int) height);
|
||||||
float paddingYPercentage = (float)viewFinderPaddingY / (float)viewfinderHeight;
|
|
||||||
|
|
||||||
RCTCamera.getInstance().setPreviewPadding(_viewFinder.getCameraType(), paddingXPercentage, paddingYPercentage);
|
|
||||||
|
|
||||||
this._viewFinder.layout(viewFinderPaddingX, viewFinderPaddingY, viewFinderPaddingX + viewfinderWidth, viewFinderPaddingY + viewfinderHeight);
|
this._viewFinder.layout(viewFinderPaddingX, viewFinderPaddingY, viewFinderPaddingX + viewfinderWidth, viewFinderPaddingY + viewfinderHeight);
|
||||||
this.postInvalidate(this.getLeft(), this.getTop(), this.getRight(), this.getBottom());
|
this.postInvalidate(this.getLeft(), this.getTop(), this.getRight(), this.getBottom());
|
||||||
|
|
Loading…
Reference in New Issue