Comments (13)
Before call ImageEditor.cropImage you need to prepare the target image. Use react-native-image-resizer for just resize the image by a few pixels. As a result this will create a new image with the correct metadata.
from react-native-image-editor.
@AndriiHnedko Thank you so much. I tried what you suggest and it worked! I think this issue is not related about device pixel ratio, size or something, it is related about image metadata. You guys if you try to create a new image with new metadata with react-native-image-resizer library, you'll see this library is working like a charm.
from react-native-image-editor.
@YanisKondakov. That issue is caused by the device scale factor, for example you run your app in an iPhone X that has the scale factor equals to 3, if you have and image with the dimensions w = 200, h = 200, the device represents those dimensions as w = 600, h = 600;
Check this link for a better answer.
from react-native-image-editor.
I have the same problem. How I solve that?
from react-native-image-editor.
The same
from react-native-image-editor.
I found a solution
export default function cropImage(imageURI, imageWidth, imageHeight, x, y) {
let screenWidth = PixelRatio.getPixelSizeForLayoutSize(
Dimensions.get('window').width,
);
let screenHeight = PixelRatio.getPixelSizeForLayoutSize(
Dimensions.get('window').height,
);
let scaleFactor = imageHeight / screenHeight;
x *= scaleFactor;
y *= scaleFactor;
screenWidth *= scaleFactor;
screenHeight *= scaleFactor;
let newSize = 8 * scaleFactor;
let offsetX = (imageWidth - screenWidth) / 2 + (x - newSize / 2);
let offsetY = y - newSize / 2;
const cropData = {
offset: {
x: offsetX,
y: offsetY,
},
size: {
width: newSize,
height: newSize,
},
};
return ImageEditor.cropImage(imageURI, cropData);
}
imageHeight: PixelRatio.getPixelSizeForLayoutSize(imageHeight)
imageWidth: PixelRatio.getPixelSizeForLayoutSize(imageWidth)
from react-native-image-editor.
Hi I want only selected rectangular area to be cropped but with below code getting warning only
Error: rectangle is outside the image
Or If I set dummy x and y then error gone but proper cropped image not getting
I have implemented RNCamera then cropping the captured image
Emulator camera image
Code
takePicture = async () => {
if (this.camera) {
const options = {
quality: 0.5,
base64: true,
};
this.camera
.takePictureAsync(options)
.then((capturedImg) => {
// 2a) Extract a reference to the captured image,
// along with its natural dimensions
const {uri, width, height} = capturedImg;
console.log(capturedImg);
console.log('Capture uri:', uri);
console.log('Capture width heigth:', width, height);
console.log('X: ', this.state.captureBoxXAxis);
console.log('Y: ', this.state.captureBoxYAxis);
let imageHeight = PixelRatio.getPixelSizeForLayoutSize(height);
let imageWidth = PixelRatio.getPixelSizeForLayoutSize(width);
cropImage(uri, imageWidth, imageHeight, this.state.captureBoxXAxis,this.state.captureBoxYAxis).then((url) => {
console.log('Cropped image uri', url);
this.setState({ image: url });
});
})
.catch((error) => {
console.log('Could not capture image.', error);
});
}
};
}
function cropImage(imageURI, imageWidth, imageHeight, x, y) {
let screenWidth = PixelRatio.getPixelSizeForLayoutSize(
Dimensions.get('window').width,
);
let screenHeight = PixelRatio.getPixelSizeForLayoutSize(
Dimensions.get('window').height,
);
let scaleFactor = imageHeight / screenHeight;
x *= scaleFactor;
y *= scaleFactor;
screenWidth *= scaleFactor;
screenHeight *= scaleFactor;
let newSize = 8 * scaleFactor;
let offsetX = (imageWidth - screenWidth) / 2 + (x - newSize / 2);
let offsetY = y - newSize / 2;
const cropData = {
offset: {
x: offsetX,
y: offsetY,
},
size: {
width: newSize,
height: newSize,
},
};
return ImageEditor.cropImage(imageURI, cropData);
}
handleLayoutChange() {
this.myComponent.measure( (fx, fy, width, height, px, py) => {
this.setState({
captureBoxXAxis: px,
captureBoxYAxis: py,
captureBoxWidth: width,
captureBoxHeight: height,
});
})
}
return (
<SafeAreaView style={styles.safeAreaContainer}>
<RNCamera
ref={(ref) => {
this.camera = ref;
}}
style={styles.cameraStyle}
type={this.state.type}
flashMode={this.state.flash}
androidCameraPermissionOptions={{
title: 'Permission to use camera',
message: 'We need your permission to use your camera',
buttonPositive: 'Ok',
buttonNegative: 'Cancel',
}}
androidRecordAudioPermissionOptions={{
title: 'Permission to use audio recording',
message: 'We need your permission to use your audio',
buttonPositive: 'Ok',
buttonNegative: 'Cancel',
}}>
<View style={styles.mainContainer}>
<View
style={[
{ height: maskTopRowHeight },
styles.maskRow,
styles.maskFrame,
]}>
<Text style={styles.titleLabel}>Back Image</Text>
</View>
<View style={[styles.maskCenter]}>
<View style={[{ width: maskColWidth }, styles.maskFrame]} />
<View
ref={view => { this.myComponent = view; }}
style={styles.boxContainer}
onLayout={(event) => this.handleLayoutChange(event)} >
<Text style={styles.topLabel}>Top of Card</Text>
<Text style={styles.bottomLabel}>Bottom of Card</Text>
</View>
<View style={[{ width: maskColWidth }, styles.maskFrame]} />
</View>
<View
style={[
{ height: maskBottomRowHeight },
styles.maskRow,
styles.maskFrame,
]}>
<View style={styles.actionContainer}>
<TouchableOpacity
onPress={this.takePicture.bind(this)}
style={styles.captureContainer}>
<Image
source={require('../images/circle-camera.png')}
size={26}
style={[styles.clickButton]}
/>
</TouchableOpacity>
<TouchableOpacity
style={styles.flashContainer}
onPress={this.toggleFlash.bind(this)}>
<Image
source={
this.state.flash === 'on'
? require('../images/flash-on.png')
: require('../images/flash-off.png')
}
size={26}
style={[styles.flashButton]}
/>
</TouchableOpacity>
</View>
</View>
</View>
</RNCamera>
</SafeAreaView>
);
Please help @MariaMindera.
from react-native-image-editor.
Is there any update on this? I have the same problem on android manily. If I multiply the coordinates and image size by the device scale I get to work on some devices, but still get wrong cropping in other ones.
if (Platform.OS === 'android') {
const xFactor = Dimensions.get('screen').scale;
const yFactor = Dimensions.get('screen').scale;
imageWidth = imageWidth * xFactor;
imageHeight = imageHeight * yFactor;
cropData = {
offset: { x: cropOffset.x * xFactor, y: cropOffset.y * yFactor },
size: { width: imageWidth, height: imageHeight },
};
}
from react-native-image-editor.
handleLayoutChange(event) {
this.myComponent.measure( (fx, fy, width, height, px, py) => {
this.setState({
captureBoxXAxis: px,
captureBoxYAxis: py,
captureBoxWidth: width,
captureBoxHeight: height,
});
})
}
const width = Dimensions.get('window').width
const height = Dimensions.get('window').height
const cropData = {
offset: {x: ((this.state.captureBoxXAxis)/width)*imageWidth, y:((this.state.captureBoxYAxis)/height)*imageHeight},
size: {width: ((this.state.captureBoxWidth)/width)*imageWidth, height: this.state.captureBoxHeight/height*imageHeight},
};
ImageEditor.cropImage(imageURI, cropData)
from react-native-image-editor.
For me, on certain platform (in my case android) and for certain images, the image width and height reported by Image.onLoad
callback is different from the actual size of the image file. A work around is to read the actual image size using Image.getSize
and use it to calculate the actual coordinates to crop.
from react-native-image-editor.
Before call ImageEditor.cropImage you need to prepare the target image. Use react-native-image-resizer for just resize the image by a few pixels. As a result this will create a new image with the correct metadata.
Work like a charm~
from react-native-image-editor.
Before call ImageEditor.cropImage you need to prepare the target image. Use react-native-image-resizer for just resize the image by a few pixels. As a result this will create a new image with the correct metadata.
Your workaround did work. Thanks.
Unfortunately we have to perform an extra step here, but it is what it is..
from react-native-image-editor.
It should be fixed in the 4.0.0
release.
from react-native-image-editor.
Related Issues (20)
- Please publish the latest build from Github, onto npm! HOT 2
- iOS compilation failures after Expo SDK 44/RN 0.64.3 upgrade HOT 3
- Why i can't install this library? HOT 6
- I am trying to use Image editor library of react native but whenever I install this library I am unable to make my build to start my app locally can you help me ?? HOT 2
- Inconsistent handling of EXIF between Android and iOS HOT 5
- run-ios fail HOT 9
- Not compatible with react 18 HOT 4
- Is this discontinued? HOT 1
- remove jcenter for android build.gradle HOT 1
- call to undeclared function 'RCTTransformFromTargetRect'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration] CGAffineTransform transform = RCTTransformFromTargetRect(image.size, targetRect); HOT 1
- [4.x] Release plan HOT 1
- Prompt error that ' Error: Requiring module "src/component/index.tsx", which threw an exception: Invariant Violation: TurboModuleRegistry.getEnforcing(...): 'RNCImageEditor' could not be found. Verify that a module by this name is registered in the native binary' in IOS
- getting error in some android devices when cropping the images HOT 4
- Prompt error that ' Error: Requiring module "src/component/index.tsx", which threw an exception: Invariant Violation: TurboModuleRegistry.getEnforcing(...): 'RNCImageEditor' could not be found. Verify that a module by this name is registered in the native binary'only happen in IOS HOT 4
- Incorrect scaling on Android HOT 6
- Offset not correct in some Android devices HOT 10
- Library support for New Architecture + Bridgeless HOT 2
- Prompt error that ' Error: Requiring module "src/component/index.tsx", which threw an exception: Invariant Violation: TurboModuleRegistry.getEnforcing(...): 'RNCImageEditor' could not be found. Verify that a module by this name is registered in the native binary'only happen in IOS HOT 3
- not working with react-native-image-crop-picker, pickedImage.path or pickedImage.sourceURL
- install error usning npm@7 HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-native-image-editor.