java.lang.ClassNotFoundException:在路径上找不到类"com.huawei.hiai.common.AIRuntimeException"



我尝试使用HiAI创建一个应用程序来尝试图像超分辨率。

活动代码如下所示:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_isr);
    Thread thread = new Thread(
            new Runnable() {
                @Override
                public void run() {
                    Intent intent = new Intent();
                    intent.setType("image/*");
                    intent.setAction(Intent.ACTION_GET_CONTENT);
                    startActivityForResult(Intent.createChooser(intent, "Select Picture"),
                            1001);
                }
            }
    );
    thread.start();
    try {
        thread.join();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 1001 && resultCode == RESULT_OK) {
        // Let's read picked image data - its URI
        Uri pickedImage = data.getData();
        // Let's read picked image path using content resolver
        String[] filePath = {MediaStore.Images.Media.DATA};
        Cursor cursor = getContentResolver().query(pickedImage, filePath, null, null, null);
        cursor.moveToFirst();
        String imagePath = cursor.getString(cursor.getColumnIndex(filePath[0]));

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        bitmap = BitmapFactory.decodeFile(imagePath, options);
        // Do something with the bitmap
        bitmap = ShrinkBitmap(imagePath, 700, 500);
        ImageView img = findViewById(R.id.imageView);
        img.setImageBitmap(bitmap);
        // At the end remember to close the cursor or you will end with the RuntimeException!
        cursor.close();
    }
}
public void press(View v) {
    Log.i("AI", "Feladat előtt");
    VisionBase.init(this, new ConnectionCallback() {
        @Override
        public void onServiceConnect() {
        }
        @Override
        public void onServiceDisconnect() {
        }
    });
    /** Define class detector,the context of this project is the input parameter */
    ImageSuperResolution superResolution = new ImageSuperResolution(this);
    /** Define the frame, put the bitmap that needs to detect the image into the frame*/
    Frame frame = new Frame();
    /** BitmapFactory.decodeFile input resource file path*/
    //Bitmap bitmap = BitmapFactory.decodeFile(null); //TODO
    frame.setBitmap(bitmap);
    /** Define and set super-resolution parameters*/
    SuperResolutionConfiguration paras = new SuperResolutionConfiguration(
            SuperResolutionConfiguration.SISR_SCALE_3X,
            SuperResolutionConfiguration.SISR_QUALITY_HIGH);
    superResolution.setSuperResolutionConfiguration(paras);
    /** Run super-resolution and get result of processing */
    ImageResult result = superResolution.doSuperResolution(frame, null);
    /** After the results are processed to get bitmap*/
    Bitmap bmp = result.getBitmap();
    /** Note: The result and the Bitmap in the result must be NULL, but also to determine whether the returned error code is 0 (0 means no error)*/

    /** Source release */
    VisionBase.destroy();
    Log.i("AI", "Destroy után");
    v.setVisibility(View.GONE);
    ImageView img = findViewById(R.id.imageView);
    img.setImageBitmap(bmp);
    Log.i("AI", "Kirajzolva");
}
Bitmap ShrinkBitmap(String file, int width, int height) {
    BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
    bmpFactoryOptions.inJustDecodeBounds = true;
    Bitmap bitmap;
    BitmapFactory.decodeFile(file, bmpFactoryOptions);
    int heightRatio = (int) Math.ceil(bmpFactoryOptions.outHeight / (float) height);
    int widthRatio = (int) Math.ceil(bmpFactoryOptions.outWidth / (float) width);
    if (heightRatio > 1 || widthRatio > 1) {
        if (heightRatio > widthRatio) {
            bmpFactoryOptions.inSampleSize = heightRatio;
        } else {
            bmpFactoryOptions.inSampleSize = widthRatio;
        }
    }
    bmpFactoryOptions.inJustDecodeBounds = false;
    bmpFactoryOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;
    bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);
    return bitmap;
}

它启动图库应用程序,选择图片,将图片裁剪为良好的分辨率,并启动一个按钮来启动AI功能以工作。它应该在图像视图中显示图片。 但是,当我在手机上运行该应用程序时,在我按下按钮让AI做它的事情后,出现了以下4个错误:

java.lang.IllegalStateException: Could not execute method for android:onClick
...
Caused by: java.lang.reflect.InvocationTargetException
                      at java.lang.reflect.Method.invoke(Native Method)
                      at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:384)
...
Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Lcom/huawei/hiai/common/AIRuntimeException;
...
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.huawei.hiai.common.AIRuntimeException" on path: DexPathList[[zip file "/data/app/hu.hiai.aitestapp -8V0mgETpAnjXZ5HAotNGPA==/base.apk"],nativeLibraryDirectories=[/data/app/hu.hiai.aitestapp-8V0mgETpAnjXZ5HAotNGPA==/lib/arm64, /system/lib64, /vendor/lib64, /product/lib64]]

app/libs 文件夹中有 3 个文件:

  • vision-oversea-release.aar - 这是由DevEco自动添加
  • huawei-hiai-vision.aar - 这是华为网站的zip文件
  • Huawei_IDE.jar - 这是在网站的另一个zip中,并作为最后一次测试添加,但它没有解决任何问题。

build.gradle 看起来像这样:

apply plugin: 'com.android.application'
android {
    compileSdkVersion 27
    defaultConfig {
        multiDexEnabled true
        applicationId "hu.hiai.aitestapp"
        minSdkVersion 26
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}
dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation(name: 'vision-oversea-release', ext: 'aar')
    implementation 'com.google.code.gson:gson:2.8.2'
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.2'
    implementation 'com.android.support:multidex:1.0.3'
    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 project(':vision-oversea-release')
    implementation project(':huawei-hiai-vision')
    implementation files('libs/Huawei_IDE.jar')
}
repositories {
     flatDir {
          dirs 'libs'
     }
}

如果有人能帮忙,我将不胜感激!

我的同事也有类似的问题,当他的代码调用 FaceCompare 方法时就会发生这种情况。碰巧的是,HiAI库中没有这样的类。那我们该怎么办呢?

如果在 UI 线程中调用 FaceCompare 方法,它看起来像是抛出的,因此我们将其移动到另一个线程,我们不再遇到该异常。

最新更新