【问题标题】:React Native: Crop image on AndroidReact Native:在 Android 上裁剪图像
【发布时间】:2017-06-23 12:54:24
【问题描述】:

以下功能适用于 iOS,但不适用于 Android。我想在两个平台上实现相同的结果。

基于this official documentation,原因是Android的react-native包中缺少ImageStore

import {
  ImageEditor,
  ImageStore
} from 'react-native';

react-native for Android 中裁剪图像(正方形)的替代方法是什么?

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 日),但它只有 implements

  • 布尔型 removeImageForTag(String file_uri)

剩下的都不见了:

  • 静态addImageFromBase64(base64ImageData,成功,失败)

我的初步解决方案:

  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());
    }
  }
  • 静态 getBase64ForTag(uri, success, failure)

this fileImageStorageManager.java中实现

  • 静态 hasImageForTag(uri, callback)

【问题讨论】:

  • 您找到解决方案了吗?我遇到了同样的问题。 :)
  • 目前没有解决办法
  • 世博会为此努力了几个月。希望好消息快点到来
  • @hellofanengineer 你找到更好的解决方案了吗?
  • 不。我把它推到了生产中。从事其他项目:D

标签: android react-native base64 crop android-bitmap


【解决方案1】:

问题一定是偏移量+尺寸之和大于接受的高度,试试吧:

{offset:{x:0,y:(1920-1080)/2},size:{width:1080, height:1080 - (1920-1080)/2)}},

看看它是否有效,如果有效,只需尝试调整您的 size.height 的大小,看看什么对您有效! :)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-08
    • 1970-01-01
    • 1970-01-01
    • 2013-05-19
    • 2020-04-05
    • 2015-02-02
    • 2021-08-12
    • 1970-01-01
    相关资源
    最近更新 更多