我尝试使用HiAI创建一个应用程序来尝试Image Super-Resolution。
该活动的代码如下:
@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功能。它应该在imageview中显示图片。 但是,当我在手机上运行该应用程序时,在按下按钮让AI完成任务后,出现以下四个错误:
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个文件:
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'
}
}
如果有人可以帮助我,我将不胜感激!
答案 0 :(得分:0)
我的同事有类似的问题,当他的代码调用FaceCompare方法时就会发生。碰巧的是,HiAI库中没有此类。那我们该怎么办?
如果在UI线程中调用FaceCompare方法,则好像抛出了该异常,因此我们将其移至另一个线程,并且不再遇到该异常。