fixed so android cropping works in all orientations.

This commit is contained in:
Marcus Andersson 2017-12-20 12:14:41 +01:00
parent ed2768828b
commit c51f0b5f93
3 changed files with 14 additions and 7 deletions

View File

@ -14,7 +14,6 @@ const styles = StyleSheet.create({
},
preview: {
flex: 1,
alignSelf: "stretch",
justifyContent: 'flex-end',
alignItems: 'center'
},
@ -187,7 +186,7 @@ export default class Example extends React.Component {
onZoomChanged={() => {}}
defaultTouchToFocus
mirrorImage={false}
cropToPreview={false}
cropToPreview={true}
/>
<View style={[styles.overlay, styles.topOverlay]}>
<TouchableOpacity

View File

@ -76,17 +76,24 @@ public class MutableImage {
}
}
public void cropToPreview(float xPercentage, float yPercentage) throws ImageMutationFailedException {
public void cropToPreview(int orientation, float xPercentage, float yPercentage) throws ImageMutationFailedException {
int width = this.currentRepresentation.getWidth();
int height = this.currentRepresentation.getHeight();
int x = (int) Math.round(height * xPercentage);
int y = (int) Math.round(width * yPercentage);
int cropWidth = width - (y*2);
int cropHeight = height - (x*2);
if (orientation == 1) {
int cropWidth = width - (y*2);
int cropHeight = height - (x*2);
this.currentRepresentation = Bitmap.createBitmap(this.currentRepresentation, y, x, cropWidth, cropHeight);
this.currentRepresentation = Bitmap.createBitmap(this.currentRepresentation, y, x, cropWidth, cropHeight);
} else {
int cropWidth = width - (x*2);
int cropHeight = height - (y*2);
this.currentRepresentation = Bitmap.createBitmap(this.currentRepresentation, x, y, cropWidth, cropHeight);
}
}
//see http://www.impulseadventure.com/photo/exif-orientation.html

View File

@ -586,8 +586,9 @@ public class RCTCameraModule extends ReactContextBaseJavaModule
int type = options.getInt("type");
float paddingWidth = RCTCamera.getInstance().getPreviewPaddingWidth(type);
float paddingHeight = RCTCamera.getInstance().getPreviewPaddingHeight(type);
int orientation = _reactContext.getResources().getConfiguration().orientation;
mutableImage.cropToPreview(paddingWidth, paddingHeight);
mutableImage.cropToPreview(orientation, paddingWidth, paddingHeight);
} catch (MutableImage.ImageMutationFailedException e) {
promise.reject("Error cropping image to preview", e);
}