Merge pull request #1155 from mvpstars/feature/fix-crop-to-preview-android

Fix cropToPreview on Android
This commit is contained in:
Nicolas Morin 2018-01-24 12:51:35 +01:00 committed by GitHub
commit c0202dc390
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 37 additions and 37 deletions

View File

@ -85,24 +85,23 @@ public class MutableImage {
}
}
public void cropToPreview(int orientation, float xPercentage, float yPercentage) throws IllegalArgumentException {
int width = getWidth();
int height = getHeight();
public void cropToPreview(double previewRatio) throws IllegalArgumentException {
int pictureWidth = getWidth(), pictureHeight = getHeight();
int targetPictureWidth, targetPictureHeight;
int x = (int) Math.round(height * xPercentage);
int y = (int) Math.round(width * yPercentage);
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);
if (previewRatio * pictureHeight > pictureWidth) {
targetPictureWidth = pictureWidth;
targetPictureHeight = (int) (pictureWidth / previewRatio);
} else {
int cropWidth = width - (x*2);
int cropHeight = height - (y*2);
this.currentRepresentation = Bitmap.createBitmap(this.currentRepresentation, x, y, cropWidth, cropHeight);
targetPictureHeight = pictureHeight;
targetPictureWidth = (int) (pictureHeight * previewRatio);
}
this.currentRepresentation = Bitmap.createBitmap(
this.currentRepresentation,
(pictureWidth - targetPictureWidth) / 2,
(pictureHeight - targetPictureHeight) / 2,
targetPictureWidth,
targetPictureHeight);
}
//see http://www.impulseadventure.com/photo/exif-orientation.html

View File

@ -74,20 +74,20 @@ public class RCTCamera {
return cameraInfo.previewHeight;
}
public float getPreviewPaddingHeight(int type) {
public int getPreviewVisibleHeight(int type) {
CameraInfoWrapper cameraInfo = _cameraInfos.get(type);
if (null == cameraInfo) {
return 0;
}
return cameraInfo.previewPaddingHeight;
return cameraInfo.previewVisibleHeight;
}
public float getPreviewPaddingWidth(int type) {
public int getPreviewVisibleWidth(int type) {
CameraInfoWrapper cameraInfo = _cameraInfos.get(type);
if (null == cameraInfo) {
return 0;
}
return cameraInfo.previewPaddingWidth;
return cameraInfo.previewVisibleWidth;
}
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);
if (cameraInfo == null) {
return;
}
cameraInfo.previewPaddingWidth = Math.abs(width);
cameraInfo.previewPaddingHeight = Math.abs(height);
cameraInfo.previewVisibleWidth = width;
cameraInfo.previewVisibleHeight = height;
}
private RCTCamera(int deviceOrientation) {
@ -491,8 +491,8 @@ public class RCTCamera {
public int rotation = 0;
public int previewWidth = -1;
public int previewHeight = -1;
public float previewPaddingHeight = -1;
public float previewPaddingWidth = -1;
public int previewVisibleWidth = -1;
public int previewVisibleHeight = -1;
public CameraInfoWrapper(Camera.CameraInfo info) {
this.info = info;

View File

@ -577,8 +577,6 @@ public class RCTCameraModule extends ReactContextBaseJavaModule
* concurrently which would blow the memory (esp on smaller devices), and slow things down.
*/
private synchronized void processImage(MutableImage mutableImage, ReadableMap options, Promise promise) {
int orientation = _reactContext.getResources().getConfiguration().orientation;
boolean shouldFixOrientation = options.hasKey("fixOrientation") && options.getBoolean("fixOrientation");
if(shouldFixOrientation) {
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");
if (shouldCropToPreview) {
try {
int type = options.getInt("type");
float paddingWidth = RCTCamera.getInstance().getPreviewPaddingWidth(type);
float paddingHeight = RCTCamera.getInstance().getPreviewPaddingHeight(type);
mutableImage.cropToPreview(orientation, paddingWidth, paddingHeight);
mutableImage.cropToPreview(needsReorient ? 1.0 / previewRatio : previewRatio);
} catch (IllegalArgumentException e) {
promise.reject("Error cropping image to preview", e);
}
@ -615,8 +619,8 @@ public class RCTCameraModule extends ReactContextBaseJavaModule
jpegQualityPercent = options.getInt("jpegQuality");
}
int imgWidth = (orientation == Configuration.ORIENTATION_PORTRAIT) ? mutableImage.getHeight() : mutableImage.getWidth();
int imgHeight = (orientation == Configuration.ORIENTATION_PORTRAIT) ? mutableImage.getWidth() : mutableImage.getHeight();
int imgWidth = (needsReorient) ? mutableImage.getHeight() : mutableImage.getWidth();
int imgHeight = (needsReorient) ? mutableImage.getWidth() : mutableImage.getHeight();
switch (options.getInt("target")) {
case RCT_CAMERA_CAPTURE_TARGET_MEMORY:

View File

@ -210,10 +210,7 @@ public class RCTCameraView extends ViewGroup {
int viewFinderPaddingX = (int) ((width - viewfinderWidth) / 2);
int viewFinderPaddingY = (int) ((height - viewfinderHeight) / 2);
float paddingXPercentage = (float)viewFinderPaddingX / (float)viewfinderWidth;
float paddingYPercentage = (float)viewFinderPaddingY / (float)viewfinderHeight;
RCTCamera.getInstance().setPreviewPadding(_viewFinder.getCameraType(), paddingXPercentage, paddingYPercentage);
RCTCamera.getInstance().setPreviewVisibleSize(_viewFinder.getCameraType(), (int) width, (int) height);
this._viewFinder.layout(viewFinderPaddingX, viewFinderPaddingY, viewFinderPaddingX + viewfinderWidth, viewFinderPaddingY + viewfinderHeight);
this.postInvalidate(this.getLeft(), this.getTop(), this.getRight(), this.getBottom());