[英]React Native Android] Resize image and upload to backend server
The photos captured with the device are big. 用设备拍摄的照片很大。 I want to upload them to my backend server after resizing them (scaling them) to more reasonable sizes (less than 800x800).
我想将它们调整大小(缩放)到更合理的大小(小于800x800)后,将它们上传到后端服务器。 I hoped to use
ImageEditor
module's coprImage()
function, but running it with a large image results in a OutOfMemory
exception. 我希望使用
ImageEditor
模块的coprImage()
函数,但是以较大的图像运行它会导致OutOfMemory
异常。 I assume that since the module tries to decode a large image and store it in memory, the app crashes. 我假设由于模块尝试解码大图像并将其存储在内存中,因此应用程序崩溃了。
What I need is the following: 我需要的是以下内容:
Input 输入项
{
width: 3100,
height: 2500,
uri: content://android/1 (some location in Android device)
}
Output 产量
{
width: 800,
height: 650,
uri: content://android/1/resized (some location in Android device)
}
Then I can grab this uri to send the picture to my backend server, and delete the resized photo from the device. 然后,我可以抓住这个uri将图片发送到我的后端服务器,然后从设备中删除已调整大小的照片。
I assume that I will have to write a NativeModule
so I can resize an image without loading a large decoded image into memory. 我假设必须编写
NativeModule
以便可以调整图像的大小,而无需将大的已解码图像加载到内存中。 React Native's Image
component uses Fresco to handle resizing before rendering them, but I don't think it provides a way to resize an image and temporarily save it in fs. React Native的
Image
组件在渲染它们之前使用Fresco来处理大小调整,但是我认为它没有提供调整图像大小并将其临时保存在fs中的方法。
Any help would be appreciated. 任何帮助,将不胜感激。
References: 参考文献:
In my app I use react-native-image-picker which works really well. 在我的应用中,我使用react-native-image-picker效果很好。
If you don't want to use it, have a look into this function source to see how resizing is done. 如果您不想使用它,请查看此函数源,以了解如何调整大小。 Cheers.
干杯。
Did you try with react-native-image-resizer ? 您是否尝试过react-native-image-resizer ? For me it works pretty well, I'm using it with react-native-camera , it looks like this:
对我来说,它工作得很好,我将它与react-native-camera一起使用 ,它看起来像这样:
fromCamera() {
let newWidth = 800;
let newHeight = 650;
this.refs.camera.capture()
.then((data) => {
ImageResizer.createResizedImage(data.path, newWidth, newHeight, 'JPEG', 100, rotation)
.then(uri => {
// send to backend
}
})
}
Original image is saving on device with uri: data.path
, so there are no problems with memory. 原始图像通过
uri: data.path
保存在设备上,因此内存没有问题。
Expo library has an image manipulator that can resize and more: 世博库有一个图像操纵器,可以调整大小以及更多功能:
import { ImageManipulator } from 'expo';
... ...
const manipResult = await ImageManipulator.manipulate(
imageUri,
[{ resize: { width: 640, height: 480 } }],
{ format: 'jpg' }
);
manipResult
will return an object with the new uri, width and height. manipResult
将返回具有新uri,宽度和高度的对象。
Find it here: https://docs.expo.io/versions/latest/sdk/imagemanipulator.html 在这里找到: https : //docs.expo.io/versions/latest/sdk/imagemanipulator.html
Incase If you want to send Base64 String you can check the code below 如果要发送Base64字符串,可以检查以下代码
how to install and link with android please check this link 如何安装并与android链接,请检查此链接
https://github.com/TBouder/react-native-asset-resize-to-base64 https://github.com/TBouder/react-native-asset-resize-to-base64
// this code is for resizing and pushing to array only //此代码仅用于调整大小并推入数组
let images = [];
NativeModules.RNAssetResizeToBase64.assetToResizedBase64(
response.uri,
newWidth,
newHeight,
(err, base64) => {
if (base64 != null) {
const imgSources = { uri: base64 };
this.setState({
image: this.state.image.concat(imgSources)
});
this.state.image.map((item, index) => {
images.push(item);
});
if (err) {
console.log(err, "errors");
}
}
}
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.