1

我只是想在我的 java android 应用程序中添加 google Vision API。作为一部分,当我尝试将位图转换为图像时,我得到一个异常如下......

    java.lang.NoSuchMethodError: No static method encodeBase64URLSafeString([B)Ljava/lang/String; in class Lorg/apache/commons/codec/binary/Base64; or its super classes (declaration of 'org.apache.commons.codec.binary.Base64' appears in /system/framework/org.apache.http.legacy.boot.jar)
    at com.google.api.client.util.Base64.encodeBase64URLSafeString(Base64.java:79)
    at com.google.api.services.vision.v1.model.Image.encodeContent(Image.java:98)

我的gradle文件如下……

apply plugin: 'com.android.application'

android {
compileSdkVersion 28
buildToolsVersion "29.0.0"
defaultConfig {
    applicationId "com.camreader1"
    minSdkVersion 24
    targetSdkVersion 28
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
}

packagingOptions {
    exclude 'META-INF/DEPENDENCIES'

}
}

dependencies {



implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation ('com.android.support:appcompat-v7:28.0.0')
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:design:28.0.0'
implementation 'commons-io:commons-io:2.5'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.rmtheis:tess-two:7.0.0'





implementation ('com.google.android.gms:play-services-base:17.0.0')

implementation ('com.google.android.gms:play-services-auth:17.0.0')

implementation 'com.google.apis:google-api-services-vision:v1-rev400-1.25.0'
implementation ('com.google.api-client:google-api-client-android:1.26.0') {
    exclude module: 'httpclient'
}
implementation ('com.google.http-client:google-http-client-gson:1.26.0') {
    exclude module: 'httpclient'
}

  implementation group: 'commons-codec', name: 'commons-codec', version: '1.12'
  }

根据我在谷歌上的发现这个问题是由于 android-support 指向一个旧的通用版本,其中确实没有“encodeContent”方法。

我的代码将位图转换为图像……

 public Image getBase64EncodedJpeg(Bitmap bitmap) {
    com.google.api.services.vision.v1.model.Image image = new com.google.api.services.vision.v1.model.Image();
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 90, byteArrayOutputStream);
    byte[] imageBytes = byteArrayOutputStream.toByteArray();
    image.encodeContent(imageBytes);
    return image;
}

任何人都可以帮助我解决这个问题......任何建议都对我有用..

1)更新/添加/添加运行时依赖项我项目中的通用版本。--- 如果可能的话,在我的选择中最好....

2)以其他方式从位图转换为Base64EncodedJpeg ...

任何帮助都可以得到...

4

1 回答 1

0

将图像字节转换为图像

String imageBase64 = Base64.encodeToString(imageBytes, Base64.DEFAULT);
base64EncodedImage.setContent(imageBase64);
annotateImageRequest1.setImage(base64EncodedImage);
于 2019-11-13T04:14:04.833 回答