[英]React Native: Crop image on Android
以下功能適用於 iOS,但不適用於 Android。 我想在兩個平台上獲得相同的結果。
基於此官方文檔,原因是在 Android 的react-native
包中缺少ImageStore
。
import {
ImageEditor,
ImageStore
} from 'react-native';
在 Android 的react-native
裁剪圖像(方形)的另一種方法是什么?
cropImage(base64ImageData) {
return new Promise((resolve, reject)=>{
ImageStore.addImageFromBase64(
base64ImageData,
(photoURI)=>{
ImageEditor.cropImage(
photoURI,
{offset:{x:0,y:(1920-1080)/2},size:{width:1080, height:1080}},
(croppedURI)=>{
ImageStore.getBase64ForTag(
croppedURI,
(base64CroppedData)=>{
ImageStore.removeImageForTag(croppedURI);
resolve(base64CroppedData);
},
(err)=>{
reject(err);
}
);
ImageStore.removeImageForTag(photoURI);
},
(err)=>{
reject(err);
}
);
},
(err)=>{
reject(err);
}
);
});
}
更新:
有一個項目https://github.com/seancunningham/react-native-image-store-ext
(最后一次提交時間為 2016 年 10 月 28 日),但它只實現了:
剩下的沒了:
我的初步解決方案:
private static final String IMAGE_STORAGE_URL_SCHEME = "rct-image-store";
@ReactMethod
public void addImageFromBase64(String base64_image_data, Callback successCallback, Callback failureCallback){
String imageStorageDir = this.reactContext.getApplicationContext().getFilesDir()+"/"+IMAGE_STORAGE_URL_SCHEME+"/";
byte[] buffer = new byte[BUFFER_SIZE];
String file_uri = imageStorageDir+"1";
try {
File f = new File(imageStorageDir);
if(!f.exists()) {
f.mkdir();
}
FileOutputStream fos = new FileOutputStream(file_uri, false);
byte[] decodedImage = Base64.decode(base64_image_data, Base64.DEFAULT);
fos.write(decodedImage);
fos.close();
successCallback.invoke("file://"+file_uri);
} catch (IOException ioe) {
failureCallback.invoke("Failed to add image from base64String"+ioe.getMessage());
} catch (Exception e) {
failureCallback.invoke("Failed to add image from base64String"+e.getMessage());
}
}
在這個文件ImageStorageManager.java
問題必須是偏移量 + 大小的總和大於接受的高度,試試看:
{offset:{x:0,y:(1920-1080)/2},size:{width:1080, height:1080 - (1920-1080)/2)}},
看看它是否有效,如果有效,只需嘗試調整 size.height 的大小,看看什么對你有效! :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.