| @@ -8,8 +8,8 @@ android { | |||||
| applicationId "com.mindspore.himindspore" | applicationId "com.mindspore.himindspore" | ||||
| minSdkVersion 21 | minSdkVersion 21 | ||||
| targetSdkVersion 30 | targetSdkVersion 30 | ||||
| versionCode 2 | |||||
| versionName "1.1.0" | |||||
| versionCode 3 | |||||
| versionName "1.1.1" | |||||
| testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" | ||||
| javaCompileOptions { | javaCompileOptions { | ||||
| @@ -25,13 +25,23 @@ android { | |||||
| minifyEnabled false | minifyEnabled false | ||||
| proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' | ||||
| } | } | ||||
| } | } | ||||
| aaptOptions { | aaptOptions { | ||||
| noCompress "ms" | noCompress "ms" | ||||
| } | } | ||||
| repositories { | |||||
| google() | |||||
| jcenter() | |||||
| mavenCentral() | |||||
| maven { url "https://jitpack.io" } | |||||
| flatDir { | |||||
| dirs 'libs', '../mindsporelibrary/libs' | |||||
| } | |||||
| } | |||||
| compileOptions { | compileOptions { | ||||
| sourceCompatibility JavaVersion.VERSION_1_8 | sourceCompatibility JavaVersion.VERSION_1_8 | ||||
| @@ -45,7 +55,6 @@ dependencies { | |||||
| implementation 'androidx.appcompat:appcompat:1.2.0' | implementation 'androidx.appcompat:appcompat:1.2.0' | ||||
| implementation 'androidx.constraintlayout:constraintlayout:2.0.4' | implementation 'androidx.constraintlayout:constraintlayout:2.0.4' | ||||
| implementation 'androidx.cardview:cardview:1.0.0' | implementation 'androidx.cardview:cardview:1.0.0' | ||||
| testImplementation 'junit:junit:4.13.1' | |||||
| androidTestImplementation 'androidx.test.ext:junit:1.1.2' | androidTestImplementation 'androidx.test.ext:junit:1.1.2' | ||||
| androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' | androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' | ||||
| @@ -1,5 +1,6 @@ | |||||
| <?xml version="1.0" encoding="utf-8"?> | <?xml version="1.0" encoding="utf-8"?> | ||||
| <manifest xmlns:android="http://schemas.android.com/apk/res/android" | <manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||||
| xmlns:tools="http://schemas.android.com/tools" | |||||
| package="com.mindspore.himindspore"> | package="com.mindspore.himindspore"> | ||||
| <uses-permission android:name="android.permission.CAMERA" /> | <uses-permission android:name="android.permission.CAMERA" /> | ||||
| @@ -21,10 +22,11 @@ | |||||
| android:requestLegacyExternalStorage="true" | android:requestLegacyExternalStorage="true" | ||||
| android:roundIcon="@mipmap/ic_launcher_round" | android:roundIcon="@mipmap/ic_launcher_round" | ||||
| android:supportsRtl="true" | android:supportsRtl="true" | ||||
| tools:replace="android:label" | |||||
| android:theme="@style/AppTheme"> | android:theme="@style/AppTheme"> | ||||
| <meta-data | <meta-data | ||||
| android:name="com.google.android.actions" | android:name="com.google.android.actions" | ||||
| android:resource="@xml/file_paths" /> | |||||
| android:resource="@xml/style_file_paths" /> | |||||
| <activity | <activity | ||||
| android:name=".SplashActivity" | android:name=".SplashActivity" | ||||
| @@ -41,10 +43,12 @@ | |||||
| android:name="androidx.core.content.FileProvider" | android:name="androidx.core.content.FileProvider" | ||||
| android:authorities="com.mindspore.himindspore.fileprovider" | android:authorities="com.mindspore.himindspore.fileprovider" | ||||
| android:exported="false" | android:exported="false" | ||||
| tools:replace="android:authorities" | |||||
| android:grantUriPermissions="true"> | android:grantUriPermissions="true"> | ||||
| <meta-data | <meta-data | ||||
| tools:replace="android:resource" | |||||
| android:name="android.support.FILE_PROVIDER_PATHS" | android:name="android.support.FILE_PROVIDER_PATHS" | ||||
| android:resource="@xml/file_paths" /> | |||||
| android:resource="@xml/style_file_paths" /> | |||||
| </provider> | </provider> | ||||
| </application> | </application> | ||||
| @@ -24,6 +24,7 @@ import android.content.pm.PackageManager; | |||||
| import android.net.Uri; | import android.net.Uri; | ||||
| import android.os.Build; | import android.os.Build; | ||||
| import android.os.Environment; | import android.os.Environment; | ||||
| import android.provider.Settings; | |||||
| import android.util.Log; | import android.util.Log; | ||||
| import android.view.View; | import android.view.View; | ||||
| import android.widget.TextView; | import android.widget.TextView; | ||||
| @@ -32,6 +33,7 @@ import android.widget.Toast; | |||||
| import androidx.annotation.NonNull; | import androidx.annotation.NonNull; | ||||
| import androidx.appcompat.app.AlertDialog; | import androidx.appcompat.app.AlertDialog; | ||||
| import androidx.core.app.ActivityCompat; | import androidx.core.app.ActivityCompat; | ||||
| import androidx.core.content.ContextCompat; | |||||
| import androidx.core.content.FileProvider; | import androidx.core.content.FileProvider; | ||||
| import com.alibaba.android.arouter.facade.annotation.Route; | import com.alibaba.android.arouter.facade.annotation.Route; | ||||
| @@ -48,9 +50,12 @@ import java.io.File; | |||||
| public class SplashActivity extends BaseActivity<MainPresenter> implements MainContract.View { | public class SplashActivity extends BaseActivity<MainPresenter> implements MainContract.View { | ||||
| private static final String TAG = "SplashActivity"; | private static final String TAG = "SplashActivity"; | ||||
| private static final String[] PERMISSIONS = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, | |||||
| Manifest.permission.READ_PHONE_STATE, Manifest.permission.CAMERA}; | |||||
| private static final int REQUEST_PERMISSION = 1; | private static final int REQUEST_PERMISSION = 1; | ||||
| private boolean isHasPermssion; | |||||
| private boolean isAllGranted; | |||||
| private int now_version; | private int now_version; | ||||
| private ProgressDialog progressDialog; | private ProgressDialog progressDialog; | ||||
| @@ -83,13 +88,27 @@ public class SplashActivity extends BaseActivity<MainPresenter> implements MainC | |||||
| } catch (PackageManager.NameNotFoundException e) { | } catch (PackageManager.NameNotFoundException e) { | ||||
| e.printStackTrace(); | e.printStackTrace(); | ||||
| } | } | ||||
| } | } | ||||
| private void requestPermissions() { | private void requestPermissions() { | ||||
| ActivityCompat.requestPermissions(this, | |||||
| new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, | |||||
| Manifest.permission.READ_PHONE_STATE, Manifest.permission.CAMERA}, REQUEST_PERMISSION); | |||||
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | |||||
| isAllGranted = checkPermissionAllGranted(PERMISSIONS); | |||||
| if (!isAllGranted) { | |||||
| ActivityCompat.requestPermissions(this, PERMISSIONS, REQUEST_PERMISSION); | |||||
| } | |||||
| } else { | |||||
| isAllGranted = true; | |||||
| } | |||||
| } | |||||
| private boolean checkPermissionAllGranted(String[] permissions) { | |||||
| for (String permission : permissions) { | |||||
| if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) { | |||||
| return false; | |||||
| } | |||||
| } | |||||
| return true; | |||||
| } | } | ||||
| /** | /** | ||||
| @@ -98,17 +117,47 @@ public class SplashActivity extends BaseActivity<MainPresenter> implements MainC | |||||
| @Override | @Override | ||||
| public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { | public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { | ||||
| if (REQUEST_PERMISSION == requestCode) { | if (REQUEST_PERMISSION == requestCode) { | ||||
| isHasPermssion = true; | |||||
| isAllGranted = true; | |||||
| for (int grant : grantResults) { | |||||
| if (grant != PackageManager.PERMISSION_GRANTED) { | |||||
| isAllGranted = false; | |||||
| break; | |||||
| } | |||||
| } | |||||
| if (!isAllGranted) { | |||||
| openAppDetails(); | |||||
| } | |||||
| } | } | ||||
| } | } | ||||
| private void openAppDetails() { | |||||
| AlertDialog.Builder builder = new AlertDialog.Builder(this); | |||||
| builder.setMessage("HiMindSpore需要访问 “相机” 和 “外部存储器”,请到 “应用信息 -> 权限” 中授予!"); | |||||
| builder.setPositiveButton("去手动授权", new DialogInterface.OnClickListener() { | |||||
| @Override | |||||
| public void onClick(DialogInterface dialog, int which) { | |||||
| Intent intent = new Intent(); | |||||
| intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); | |||||
| intent.addCategory(Intent.CATEGORY_DEFAULT); | |||||
| intent.setData(Uri.parse("package:" + getPackageName())); | |||||
| intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | |||||
| intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); | |||||
| intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); | |||||
| startActivity(intent); | |||||
| } | |||||
| }); | |||||
| builder.setNegativeButton("取消", null); | |||||
| builder.show(); | |||||
| } | |||||
| private void getUpdateInfo() { | private void getUpdateInfo() { | ||||
| presenter.getUpdateInfo(); | presenter.getUpdateInfo(); | ||||
| } | } | ||||
| public void onClickImage(View view) { | public void onClickImage(View view) { | ||||
| if (isHasPermssion) { | |||||
| if (isAllGranted) { | |||||
| ARouter.getInstance().build("/imageobject/ImageCameraActivity") | ARouter.getInstance().build("/imageobject/ImageCameraActivity") | ||||
| .withInt("OPEN_TYPE", 1).navigation(); | .withInt("OPEN_TYPE", 1).navigation(); | ||||
| } else { | } else { | ||||
| @@ -117,7 +166,7 @@ public class SplashActivity extends BaseActivity<MainPresenter> implements MainC | |||||
| } | } | ||||
| public void onClickGarbage(View view) { | public void onClickGarbage(View view) { | ||||
| if (isHasPermssion) { | |||||
| if (isAllGranted) { | |||||
| ARouter.getInstance().build("/imageobject/ImageCameraActivity") | ARouter.getInstance().build("/imageobject/ImageCameraActivity") | ||||
| .withInt("OPEN_TYPE", 2).navigation(); | .withInt("OPEN_TYPE", 2).navigation(); | ||||
| } else { | } else { | ||||
| @@ -126,7 +175,7 @@ public class SplashActivity extends BaseActivity<MainPresenter> implements MainC | |||||
| } | } | ||||
| public void onClickPhotoDetection(View view) { | public void onClickPhotoDetection(View view) { | ||||
| if (isHasPermssion) { | |||||
| if (isAllGranted) { | |||||
| ARouter.getInstance().build("/imageobject/ObjectPhotoActivity").navigation(); | ARouter.getInstance().build("/imageobject/ObjectPhotoActivity").navigation(); | ||||
| } else { | } else { | ||||
| requestPermissions(); | requestPermissions(); | ||||
| @@ -134,7 +183,7 @@ public class SplashActivity extends BaseActivity<MainPresenter> implements MainC | |||||
| } | } | ||||
| public void onClickCameraDetection(View view) { | public void onClickCameraDetection(View view) { | ||||
| if (isHasPermssion) { | |||||
| if (isAllGranted) { | |||||
| ARouter.getInstance().build("/imageobject/ObjectCameraActivity").navigation(); | ARouter.getInstance().build("/imageobject/ObjectCameraActivity").navigation(); | ||||
| } else { | } else { | ||||
| requestPermissions(); | requestPermissions(); | ||||
| @@ -142,7 +191,7 @@ public class SplashActivity extends BaseActivity<MainPresenter> implements MainC | |||||
| } | } | ||||
| public void onClickPoseNet(View view) { | public void onClickPoseNet(View view) { | ||||
| if (isHasPermssion) { | |||||
| if (isAllGranted) { | |||||
| ARouter.getInstance().build("/posenet/PosenetMainActivity").navigation(this); | ARouter.getInstance().build("/posenet/PosenetMainActivity").navigation(this); | ||||
| } else { | } else { | ||||
| requestPermissions(); | requestPermissions(); | ||||
| @@ -150,7 +199,7 @@ public class SplashActivity extends BaseActivity<MainPresenter> implements MainC | |||||
| } | } | ||||
| public void onClickStyleTransfer(View view) { | public void onClickStyleTransfer(View view) { | ||||
| if (isHasPermssion) { | |||||
| if (isAllGranted) { | |||||
| ARouter.getInstance().build("/styletransfer/StyleMainActivity").navigation(this); | ARouter.getInstance().build("/styletransfer/StyleMainActivity").navigation(this); | ||||
| } else { | } else { | ||||
| requestPermissions(); | requestPermissions(); | ||||
| @@ -219,7 +268,6 @@ public class SplashActivity extends BaseActivity<MainPresenter> implements MainC | |||||
| public void showUpdate(final UpdateInfoBean updateInfo) { | public void showUpdate(final UpdateInfoBean updateInfo) { | ||||
| if (now_version == updateInfo.getVersionCode()) { | if (now_version == updateInfo.getVersionCode()) { | ||||
| Toast.makeText(this, "已经是最新版本", Toast.LENGTH_SHORT).show(); | Toast.makeText(this, "已经是最新版本", Toast.LENGTH_SHORT).show(); | ||||
| Log.d(TAG + "版本号是", "onResponse: " + now_version); | Log.d(TAG + "版本号是", "onResponse: " + now_version); | ||||
| @@ -1,6 +1,6 @@ | |||||
| <resources> | <resources> | ||||
| <!-- Base application theme. --> | <!-- Base application theme. --> | ||||
| <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> | |||||
| <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> | |||||
| <!-- Customize your theme here. --> | <!-- Customize your theme here. --> | ||||
| <item name="colorPrimary">@color/colorPrimary</item> | <item name="colorPrimary">@color/colorPrimary</item> | ||||
| <item name="colorPrimaryDark">@color/colorPrimaryDark</item> | <item name="colorPrimaryDark">@color/colorPrimaryDark</item> | ||||
| @@ -3,6 +3,9 @@ buildscript { | |||||
| repositories { | repositories { | ||||
| google() | google() | ||||
| jcenter() | jcenter() | ||||
| mavenCentral() | |||||
| maven { url "https://jitpack.io" } | |||||
| } | } | ||||
| dependencies { | dependencies { | ||||
| classpath "com.android.tools.build:gradle:4.0.1" | classpath "com.android.tools.build:gradle:4.0.1" | ||||
| @@ -16,6 +19,7 @@ allprojects { | |||||
| repositories { | repositories { | ||||
| google() | google() | ||||
| jcenter() | jcenter() | ||||
| mavenCentral() | |||||
| maven { url "https://jitpack.io" } | maven { url "https://jitpack.io" } | ||||
| } | } | ||||
| } | } | ||||
| @@ -74,7 +74,6 @@ dependencies { | |||||
| implementation 'androidx.appcompat:appcompat:1.2.0' | implementation 'androidx.appcompat:appcompat:1.2.0' | ||||
| implementation 'com.google.android.material:material:1.2.1' | implementation 'com.google.android.material:material:1.2.1' | ||||
| implementation 'androidx.constraintlayout:constraintlayout:2.0.4' | implementation 'androidx.constraintlayout:constraintlayout:2.0.4' | ||||
| testImplementation 'junit:junit:4.+' | |||||
| androidTestImplementation 'androidx.test.ext:junit:1.1.2' | androidTestImplementation 'androidx.test.ext:junit:1.1.2' | ||||
| androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' | androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' | ||||
| @@ -0,0 +1 @@ | |||||
| /build | |||||
| @@ -0,0 +1,66 @@ | |||||
| plugins { | |||||
| id 'com.android.library' | |||||
| } | |||||
| android { | |||||
| compileSdkVersion 30 | |||||
| buildToolsVersion "30.0.1" | |||||
| defaultConfig { | |||||
| minSdkVersion 21 | |||||
| targetSdkVersion 30 | |||||
| versionCode 1 | |||||
| versionName "1.0" | |||||
| testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" | |||||
| javaCompileOptions { | |||||
| annotationProcessorOptions { | |||||
| arguments = [moduleName: project.getName()] | |||||
| } | |||||
| } | |||||
| } | |||||
| buildTypes { | |||||
| release { | |||||
| minifyEnabled false | |||||
| proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' | |||||
| } | |||||
| } | |||||
| lintOptions { | |||||
| checkReleaseBuilds false | |||||
| // Or, if you prefer, you can continue to check for errors in release builds, | |||||
| // but continue the build even when errors are found: | |||||
| abortOnError false | |||||
| } | |||||
| aaptOptions { | |||||
| noCompress "ms" | |||||
| } | |||||
| repositories { | |||||
| google() | |||||
| jcenter() | |||||
| flatDir { | |||||
| dirs 'libs' | |||||
| } | |||||
| } | |||||
| compileOptions { | |||||
| sourceCompatibility JavaVersion.VERSION_1_8 | |||||
| targetCompatibility JavaVersion.VERSION_1_8 | |||||
| } | |||||
| } | |||||
| // Download default models; if you wish to use your own models then | |||||
| // place them in the "assets" directory and comment out this line. | |||||
| apply from: 'download.gradle' | |||||
| dependencies { | |||||
| implementation fileTree(dir: 'libs', include: ['*.jar', '*.aar']) | |||||
| implementation 'androidx.appcompat:appcompat:1.2.0' | |||||
| implementation 'com.google.android.material:material:1.2.1' | |||||
| androidTestImplementation 'androidx.test.ext:junit:1.1.2' | |||||
| androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' | |||||
| implementation 'com.alibaba:arouter-api:1.2.1' | |||||
| annotationProcessor 'com.alibaba:arouter-compiler:1.1.2' | |||||
| } | |||||
| @@ -0,0 +1,57 @@ | |||||
| /** | |||||
| * To download necessary library from HuaWei server. | |||||
| * Including mindspore-lite .so file, minddata-lite .so file and model file. | |||||
| * The libraries can be downloaded manually. | |||||
| */ | |||||
| def mindsporeLite_Version = "mindspore-lite-maven-1.0.1" | |||||
| def mindsporeLiteDownloadUrl = "https://ms-release.obs.cn-north-4.myhuaweicloud.com/1.0.1/lite/java/${mindsporeLite_Version}.zip" | |||||
| def mindSporeLibrary = "libs/${mindsporeLite_Version}.zip" | |||||
| def cleantargetMindSporeInclude = "libs" | |||||
| def targetMindSporeInclude = "libs/" | |||||
| task downloadMindSporeLibrary(type: DownloadUrlTask) { | |||||
| doFirst { | |||||
| println "Downloading ${mindsporeLiteDownloadUrl}" | |||||
| } | |||||
| sourceUrl = "${mindsporeLiteDownloadUrl}" | |||||
| target = file("${mindSporeLibrary}") | |||||
| } | |||||
| task unzipMindSporeInclude(type: Copy, dependsOn: ['downloadMindSporeLibrary']) { | |||||
| doFirst { | |||||
| println "Unzipping ${mindSporeLibrary}" | |||||
| } | |||||
| from zipTree("${mindSporeLibrary}") | |||||
| into "${targetMindSporeInclude}" | |||||
| } | |||||
| task cleanUnusedmindsporeFiles(type: Delete, dependsOn: ['unzipMindSporeInclude']) { | |||||
| delete fileTree("${cleantargetMindSporeInclude}").matching { | |||||
| include "*.zip" | |||||
| } | |||||
| } | |||||
| if (file("libs/mindspore-lite-1.0.1.aar").exists()) { | |||||
| downloadMindSporeLibrary.enabled = false | |||||
| unzipMindSporeInclude.enabled = false | |||||
| cleanUnusedmindsporeFiles.enabled = false | |||||
| } | |||||
| preBuild.dependsOn downloadMindSporeLibrary | |||||
| preBuild.dependsOn unzipMindSporeInclude | |||||
| preBuild.dependsOn cleanUnusedmindsporeFiles | |||||
| class DownloadUrlTask extends DefaultTask { | |||||
| @Input | |||||
| String sourceUrl | |||||
| @OutputFile | |||||
| File target | |||||
| @TaskAction | |||||
| void download() { | |||||
| ant.get(src: sourceUrl, dest: target) | |||||
| } | |||||
| } | |||||
| @@ -0,0 +1,21 @@ | |||||
| # Add project specific ProGuard rules here. | |||||
| # You can control the set of applied configuration files using the | |||||
| # proguardFiles setting in build.gradle. | |||||
| # | |||||
| # For more details, see | |||||
| # http://developer.android.com/guide/developing/tools/proguard.html | |||||
| # If your project uses WebView with JS, uncomment the following | |||||
| # and specify the fully qualified class name to the JavaScript interface | |||||
| # class: | |||||
| #-keepclassmembers class fqcn.of.javascript.interface.for.webview { | |||||
| # public *; | |||||
| #} | |||||
| # Uncomment this to preserve the line number information for | |||||
| # debugging stack traces. | |||||
| #-keepattributes SourceFile,LineNumberTable | |||||
| # If you keep the line number information, uncomment this to | |||||
| # hide the original source file name. | |||||
| #-renamesourcefileattribute SourceFile | |||||
| @@ -0,0 +1,26 @@ | |||||
| package com.mindspore.mindsporelibrary; | |||||
| import android.content.Context; | |||||
| import androidx.test.ext.junit.runners.AndroidJUnit4; | |||||
| import androidx.test.platform.app.InstrumentationRegistry; | |||||
| import org.junit.Test; | |||||
| import org.junit.runner.RunWith; | |||||
| import static org.junit.Assert.assertEquals; | |||||
| /** | |||||
| * Instrumented test, which will execute on an Android device. | |||||
| * | |||||
| * @see <a href="http://d.android.com/tools/testing">Testing documentation</a> | |||||
| */ | |||||
| @RunWith(AndroidJUnit4.class) | |||||
| public class ExampleInstrumentedTest { | |||||
| @Test | |||||
| public void useAppContext() { | |||||
| // Context of the app under test. | |||||
| Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext(); | |||||
| assertEquals("com.mindspore.mindsporelibrary.test", appContext.getPackageName()); | |||||
| } | |||||
| } | |||||
| @@ -0,0 +1,4 @@ | |||||
| <?xml version="1.0" encoding="utf-8"?> | |||||
| <manifest package="com.mindspore.mindsporelibrary"> | |||||
| </manifest> | |||||
| @@ -0,0 +1,17 @@ | |||||
| package com.mindspore.mindsporelibrary; | |||||
| import org.junit.Test; | |||||
| import static org.junit.Assert.*; | |||||
| /** | |||||
| * Example local unit test, which will execute on the development machine (host). | |||||
| * | |||||
| * @see <a href="http://d.android.com/tools/testing">Testing documentation</a> | |||||
| */ | |||||
| public class ExampleUnitTest { | |||||
| @Test | |||||
| public void addition_isCorrect() { | |||||
| assertEquals(4, 2 + 2); | |||||
| } | |||||
| } | |||||
| @@ -5,13 +5,11 @@ plugins { | |||||
| android { | android { | ||||
| compileSdkVersion 30 | compileSdkVersion 30 | ||||
| buildToolsVersion "30.0.1" | buildToolsVersion "30.0.1" | ||||
| defaultConfig { | defaultConfig { | ||||
| minSdkVersion 21 | minSdkVersion 21 | ||||
| targetSdkVersion 30 | targetSdkVersion 30 | ||||
| versionCode 1 | versionCode 1 | ||||
| versionName "1.0" | versionName "1.0" | ||||
| testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" | ||||
| javaCompileOptions { | javaCompileOptions { | ||||
| annotationProcessorOptions { | annotationProcessorOptions { | ||||
| @@ -41,8 +39,11 @@ android { | |||||
| repositories { | repositories { | ||||
| google() | google() | ||||
| jcenter() | jcenter() | ||||
| mavenCentral() | |||||
| maven { url "https://jitpack.io" } | |||||
| flatDir { | flatDir { | ||||
| dirs 'libs' | |||||
| dirs 'libs', '../mindsporelibrary/libs' | |||||
| } | } | ||||
| } | } | ||||
| @@ -61,11 +62,10 @@ dependencies { | |||||
| implementation 'androidx.appcompat:appcompat:1.2.0' | implementation 'androidx.appcompat:appcompat:1.2.0' | ||||
| implementation 'com.google.android.material:material:1.2.1' | implementation 'com.google.android.material:material:1.2.1' | ||||
| implementation 'androidx.constraintlayout:constraintlayout:2.0.4' | implementation 'androidx.constraintlayout:constraintlayout:2.0.4' | ||||
| testImplementation 'junit:junit:4.+' | |||||
| androidTestImplementation 'androidx.test.ext:junit:1.1.2' | androidTestImplementation 'androidx.test.ext:junit:1.1.2' | ||||
| androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' | androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' | ||||
| implementation 'com.alibaba:arouter-api:1.2.1' | implementation 'com.alibaba:arouter-api:1.2.1' | ||||
| annotationProcessor 'com.alibaba:arouter-compiler:1.1.2' | annotationProcessor 'com.alibaba:arouter-compiler:1.1.2' | ||||
| implementation(name: 'mindspore-lite-1.0.1', ext: 'aar') | |||||
| } | } | ||||
| @@ -3,14 +3,8 @@ | |||||
| * Including mindspore-lite .so file, minddata-lite .so file and model file. | * Including mindspore-lite .so file, minddata-lite .so file and model file. | ||||
| * The libraries can be downloaded manually. | * The libraries can be downloaded manually. | ||||
| */ | */ | ||||
| def mindsporeLite_Version = "mindspore-lite-maven-1.0.1" | |||||
| def targetModelFile = "src/main/assets/posenet_model.ms" | def targetModelFile = "src/main/assets/posenet_model.ms" | ||||
| def modelDownloadUrl = "https://download.mindspore.cn/model_zoo/official/lite/posenet_lite/posenet_model.ms" | def modelDownloadUrl = "https://download.mindspore.cn/model_zoo/official/lite/posenet_lite/posenet_model.ms" | ||||
| def mindsporeLiteDownloadUrl = "https://ms-release.obs.cn-north-4.myhuaweicloud.com/1.0.1/lite/java/${mindsporeLite_Version}.zip" | |||||
| def mindSporeLibrary = "libs/${mindsporeLite_Version}.zip" | |||||
| def cleantargetMindSporeInclude = "libs" | |||||
| def targetMindSporeInclude = "libs/" | |||||
| task downloadModelFile(type: DownloadUrlTask) { | task downloadModelFile(type: DownloadUrlTask) { | ||||
| doFirst { | doFirst { | ||||
| @@ -20,44 +14,11 @@ task downloadModelFile(type: DownloadUrlTask) { | |||||
| target = file("${targetModelFile}") | target = file("${targetModelFile}") | ||||
| } | } | ||||
| task downloadMindSporeLibrary(type: DownloadUrlTask) { | |||||
| doFirst { | |||||
| println "Downloading ${mindsporeLiteDownloadUrl}" | |||||
| } | |||||
| sourceUrl = "${mindsporeLiteDownloadUrl}" | |||||
| target = file("${mindSporeLibrary}") | |||||
| } | |||||
| task unzipMindSporeInclude(type: Copy, dependsOn: ['downloadMindSporeLibrary']) { | |||||
| doFirst { | |||||
| println "Unzipping ${mindSporeLibrary}" | |||||
| } | |||||
| from zipTree("${mindSporeLibrary}") | |||||
| into "${targetMindSporeInclude}" | |||||
| } | |||||
| task cleanUnusedmindsporeFiles(type: Delete, dependsOn: ['unzipMindSporeInclude']) { | |||||
| delete fileTree("${cleantargetMindSporeInclude}").matching { | |||||
| include "*.zip" | |||||
| } | |||||
| } | |||||
| if (file("libs/mindspore-lite-1.0.1.aar").exists()) { | |||||
| downloadMindSporeLibrary.enabled = false | |||||
| unzipMindSporeInclude.enabled = false | |||||
| cleanUnusedmindsporeFiles.enabled = false | |||||
| } | |||||
| if (file("src/main/assets/posenet_model.ms").exists()) { | if (file("src/main/assets/posenet_model.ms").exists()) { | ||||
| downloadModelFile.enabled = false | downloadModelFile.enabled = false | ||||
| } | } | ||||
| preBuild.dependsOn downloadModelFile | preBuild.dependsOn downloadModelFile | ||||
| preBuild.dependsOn downloadMindSporeLibrary | |||||
| preBuild.dependsOn unzipMindSporeInclude | |||||
| preBuild.dependsOn cleanUnusedmindsporeFiles | |||||
| class DownloadUrlTask extends DefaultTask { | class DownloadUrlTask extends DefaultTask { | ||||
| @Input | @Input | ||||
| @@ -1,18 +1,18 @@ | |||||
| <?xml version="1.0" encoding="utf-8"?> | <?xml version="1.0" encoding="utf-8"?> | ||||
| <manifest xmlns:android="http://schemas.android.com/apk/res/android" | <manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||||
| xmlns:tools="http://schemas.android.com/tools" | |||||
| package="com.mindspore.posenet"> | package="com.mindspore.posenet"> | ||||
| <application | <application | ||||
| tools:replace="android:label" | |||||
| android:allowBackup="true" | android:allowBackup="true" | ||||
| android:label="@string/app_name" | |||||
| android:label="@string/posenet_app_name" | |||||
| android:supportsRtl="true"> | android:supportsRtl="true"> | ||||
| <activity | <activity | ||||
| android:name=".PosenetMainActivity" | android:name=".PosenetMainActivity" | ||||
| android:screenOrientation="portrait" | android:screenOrientation="portrait" | ||||
| android:theme="@style/Theme.AppCompat.NoActionBar"> | |||||
| </activity> | |||||
| android:theme="@style/Theme.AppCompat.NoActionBar" /> | |||||
| </application> | </application> | ||||
| </manifest> | </manifest> | ||||
| @@ -135,7 +135,7 @@ public class PoseNetFragment extends Fragment { | |||||
| public View onCreateView(LayoutInflater inflater, ViewGroup container, | public View onCreateView(LayoutInflater inflater, ViewGroup container, | ||||
| Bundle savedInstanceState) { | Bundle savedInstanceState) { | ||||
| // Inflate the layout for this fragment | // Inflate the layout for this fragment | ||||
| return inflater.inflate(R.layout.fragment_pose_net, container, false); | |||||
| return inflater.inflate(R.layout.posenet_fragment_pose_net, container, false); | |||||
| } | } | ||||
| @Override | @Override | ||||
| @@ -87,7 +87,7 @@ public class PosenetMainActivity extends AppCompatActivity implements CameraData | |||||
| @Override | @Override | ||||
| protected void onCreate(Bundle savedInstanceState) { | protected void onCreate(Bundle savedInstanceState) { | ||||
| super.onCreate(savedInstanceState); | super.onCreate(savedInstanceState); | ||||
| setContentView(R.layout.activity_main); | |||||
| setContentView(R.layout.posenet_activity_main); | |||||
| addCameraFragment(); | addCameraFragment(); | ||||
| } | } | ||||
| @@ -178,7 +178,7 @@ public class PosenetMainActivity extends AppCompatActivity implements CameraData | |||||
| * Set the paint color and size. | * Set the paint color and size. | ||||
| */ | */ | ||||
| private void setPaint() { | private void setPaint() { | ||||
| paint.setColor(getResources().getColor(R.color.text_blue)); | |||||
| paint.setColor(getResources().getColor(R.color.posenet_text_blue)); | |||||
| paint.setTextSize(80.0f); | paint.setTextSize(80.0f); | ||||
| paint.setStrokeWidth(8.0f); | paint.setStrokeWidth(8.0f); | ||||
| } | } | ||||
| @@ -1,30 +0,0 @@ | |||||
| <vector xmlns:android="http://schemas.android.com/apk/res/android" | |||||
| xmlns:aapt="http://schemas.android.com/aapt" | |||||
| android:width="108dp" | |||||
| android:height="108dp" | |||||
| android:viewportWidth="108" | |||||
| android:viewportHeight="108"> | |||||
| <path android:pathData="M31,63.928c0,0 6.4,-11 12.1,-13.1c7.2,-2.6 26,-1.4 26,-1.4l38.1,38.1L107,108.928l-32,-1L31,63.928z"> | |||||
| <aapt:attr name="android:fillColor"> | |||||
| <gradient | |||||
| android:endX="85.84757" | |||||
| android:endY="92.4963" | |||||
| android:startX="42.9492" | |||||
| android:startY="49.59793" | |||||
| android:type="linear"> | |||||
| <item | |||||
| android:color="#44000000" | |||||
| android:offset="0.0" /> | |||||
| <item | |||||
| android:color="#00000000" | |||||
| android:offset="1.0" /> | |||||
| </gradient> | |||||
| </aapt:attr> | |||||
| </path> | |||||
| <path | |||||
| android:fillColor="#FFFFFF" | |||||
| android:fillType="nonZero" | |||||
| android:pathData="M65.3,45.828l3.8,-6.6c0.2,-0.4 0.1,-0.9 -0.3,-1.1c-0.4,-0.2 -0.9,-0.1 -1.1,0.3l-3.9,6.7c-6.3,-2.8 -13.4,-2.8 -19.7,0l-3.9,-6.7c-0.2,-0.4 -0.7,-0.5 -1.1,-0.3C38.8,38.328 38.7,38.828 38.9,39.228l3.8,6.6C36.2,49.428 31.7,56.028 31,63.928h46C76.3,56.028 71.8,49.428 65.3,45.828zM43.4,57.328c-0.8,0 -1.5,-0.5 -1.8,-1.2c-0.3,-0.7 -0.1,-1.5 0.4,-2.1c0.5,-0.5 1.4,-0.7 2.1,-0.4c0.7,0.3 1.2,1 1.2,1.8C45.3,56.528 44.5,57.328 43.4,57.328L43.4,57.328zM64.6,57.328c-0.8,0 -1.5,-0.5 -1.8,-1.2s-0.1,-1.5 0.4,-2.1c0.5,-0.5 1.4,-0.7 2.1,-0.4c0.7,0.3 1.2,1 1.2,1.8C66.5,56.528 65.6,57.328 64.6,57.328L64.6,57.328z" | |||||
| android:strokeWidth="1" | |||||
| android:strokeColor="#00000000" /> | |||||
| </vector> | |||||
| @@ -4,7 +4,7 @@ | |||||
| android:layout_width="match_parent" | android:layout_width="match_parent" | ||||
| android:layout_height="match_parent" | android:layout_height="match_parent" | ||||
| android:background="@android:color/black" | android:background="@android:color/black" | ||||
| tools:context="com.mindspore.posenetdemo.MainActivity"> | |||||
| tools:context="com.mindspore.posenet.PosenetMainActivity"> | |||||
| <FrameLayout | <FrameLayout | ||||
| android:id="@+id/container" | android:id="@+id/container" | ||||
| @@ -1,19 +1,19 @@ | |||||
| <?xml version="1.0" encoding="utf-8"?> | <?xml version="1.0" encoding="utf-8"?> | ||||
| <resources> | <resources> | ||||
| <color name="colorPrimary">#6200EE</color> | |||||
| <color name="colorPrimaryDark">#3700B3</color> | |||||
| <color name="colorAccent">#03DAC5</color> | |||||
| <color name="posenet_colorPrimary">#6200EE</color> | |||||
| <color name="posenet_colorPrimaryDark">#3700B3</color> | |||||
| <color name="posenet_colorAccent">#03DAC5</color> | |||||
| <color name="mindspore_semi_transparent">#66000000</color> | |||||
| <color name="posenet_mindspore_semi_transparent">#66000000</color> | |||||
| <color name="white">#ffffff</color> | |||||
| <color name="black">#000000</color> | |||||
| <color name="gray">#A69D9D</color> | |||||
| <color name="gray_btn">#424242</color> | |||||
| <color name="posenet_white">#ffffff</color> | |||||
| <color name="posenet_black">#000000</color> | |||||
| <color name="posenet_gray">#A69D9D</color> | |||||
| <color name="posenet_gray_btn">#424242</color> | |||||
| <color name="text_blue">#6DA7FF</color> | |||||
| <color name="text_yellow">#F8E71C</color> | |||||
| <color name="text_orange">#FF844D</color> | |||||
| <color name="text_green">#66B50A</color> | |||||
| <color name="posenet_text_blue">#6DA7FF</color> | |||||
| <color name="posenet_text_yellow">#F8E71C</color> | |||||
| <color name="posenet_text_orange">#FF844D</color> | |||||
| <color name="posenet_text_green">#66B50A</color> | |||||
| </resources> | </resources> | ||||
| @@ -1,5 +1,5 @@ | |||||
| <resources> | <resources> | ||||
| <string name="app_name">PoseNetDemo</string> | |||||
| <string name="posenet_app_name">PoseNetDemo</string> | |||||
| <!-- TODO: Remove or change this placeholder text --> | <!-- TODO: Remove or change this placeholder text --> | ||||
| <string name="hello_blank_fragment">Hello blank fragment</string> | |||||
| <string name="posenet_hello_blank_fragment">Hello blank fragment</string> | |||||
| </resources> | </resources> | ||||
| @@ -1,10 +1,10 @@ | |||||
| <resources> | <resources> | ||||
| <!-- Base application theme. --> | <!-- Base application theme. --> | ||||
| <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> | |||||
| <style name="posenet_AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> | |||||
| <!-- Customize your theme here. --> | <!-- Customize your theme here. --> | ||||
| <item name="colorPrimary">@color/colorPrimary</item> | |||||
| <item name="colorPrimaryDark">@color/colorPrimaryDark</item> | |||||
| <item name="colorAccent">@color/colorAccent</item> | |||||
| <item name="colorPrimary">@color/posenet_colorPrimary</item> | |||||
| <item name="colorPrimaryDark">@color/posenet_colorPrimaryDark</item> | |||||
| <item name="colorAccent">@color/posenet_colorAccent</item> | |||||
| </style> | </style> | ||||
| </resources> | </resources> | ||||
| @@ -1,3 +1,4 @@ | |||||
| include ':mindsporelibrary' | |||||
| include ':imageObject' | include ':imageObject' | ||||
| include ':styletransfer' | include ':styletransfer' | ||||
| include ':posenet' | include ':posenet' | ||||
| @@ -46,7 +46,7 @@ android { | |||||
| google() | google() | ||||
| jcenter() | jcenter() | ||||
| flatDir { | flatDir { | ||||
| dirs 'libs' | |||||
| dirs 'libs', '../mindsporelibrary/libs' | |||||
| } | } | ||||
| } | } | ||||
| @@ -63,7 +63,6 @@ apply from: 'download.gradle' | |||||
| dependencies { | dependencies { | ||||
| implementation 'androidx.legacy:legacy-support-v4:1.0.0' | implementation 'androidx.legacy:legacy-support-v4:1.0.0' | ||||
| implementation 'androidx.recyclerview:recyclerview:1.1.0' | implementation 'androidx.recyclerview:recyclerview:1.1.0' | ||||
| implementation fileTree(dir: 'libs', include: ['*.jar', '*.aar']) | |||||
| implementation 'androidx.appcompat:appcompat:1.2.0' | implementation 'androidx.appcompat:appcompat:1.2.0' | ||||
| implementation 'androidx.constraintlayout:constraintlayout:2.0.4' | implementation 'androidx.constraintlayout:constraintlayout:2.0.4' | ||||
| testImplementation 'junit:junit:4.12' | testImplementation 'junit:junit:4.12' | ||||
| @@ -75,5 +74,7 @@ dependencies { | |||||
| implementation 'com.alibaba:arouter-api:1.2.1' | implementation 'com.alibaba:arouter-api:1.2.1' | ||||
| annotationProcessor 'com.alibaba:arouter-compiler:1.1.2' | annotationProcessor 'com.alibaba:arouter-compiler:1.1.2' | ||||
| implementation(name: 'mindspore-lite-1.0.1', ext: 'aar') | |||||
| } | } | ||||
| @@ -3,16 +3,10 @@ | |||||
| * Including mindspore-lite .so file, minddata-lite .so file and model file. | * Including mindspore-lite .so file, minddata-lite .so file and model file. | ||||
| * The libraries can be downloaded manually. | * The libraries can be downloaded manually. | ||||
| */ | */ | ||||
| def mindsporeLite_Version = "mindspore-lite-maven-1.0.1" | |||||
| def targetPredictModelFile = "src/main/assets/style_predict_quant.ms" | def targetPredictModelFile = "src/main/assets/style_predict_quant.ms" | ||||
| def targetTransferModelFile = "src/main/assets/style_transfer_quant.ms" | def targetTransferModelFile = "src/main/assets/style_transfer_quant.ms" | ||||
| def modelPredictDownloadUrl = "https://download.mindspore.cn/model_zoo/official/lite/style_lite/style_predict_quant.ms" | def modelPredictDownloadUrl = "https://download.mindspore.cn/model_zoo/official/lite/style_lite/style_predict_quant.ms" | ||||
| def modelTransferDownloadUrl = "https://download.mindspore.cn/model_zoo/official/lite/style_lite/style_transfer_quant.ms" | def modelTransferDownloadUrl = "https://download.mindspore.cn/model_zoo/official/lite/style_lite/style_transfer_quant.ms" | ||||
| def mindsporeLiteDownloadUrl = "https://ms-release.obs.cn-north-4.myhuaweicloud.com/1.0.1/lite/java/${mindsporeLite_Version}.zip" | |||||
| def mindSporeLibrary = "libs/${mindsporeLite_Version}.zip" | |||||
| def cleantargetMindSporeInclude = "libs" | |||||
| def targetMindSporeInclude = "libs/" | |||||
| task downloadPredictModelFile(type: DownloadUrlTask) { | task downloadPredictModelFile(type: DownloadUrlTask) { | ||||
| doFirst { | doFirst { | ||||
| @@ -30,36 +24,6 @@ task downloadTransferModelFile(type: DownloadUrlTask) { | |||||
| target = file("${targetTransferModelFile}") | target = file("${targetTransferModelFile}") | ||||
| } | } | ||||
| task downloadMindSporeLibrary(type: DownloadUrlTask) { | |||||
| doFirst { | |||||
| println "Downloading ${mindsporeLiteDownloadUrl}" | |||||
| } | |||||
| sourceUrl = "${mindsporeLiteDownloadUrl}" | |||||
| target = file("${mindSporeLibrary}") | |||||
| } | |||||
| task unzipMindSporeInclude(type: Copy, dependsOn: ['downloadMindSporeLibrary']) { | |||||
| doFirst { | |||||
| println "Unzipping ${mindSporeLibrary}" | |||||
| } | |||||
| from zipTree("${mindSporeLibrary}") | |||||
| into "${targetMindSporeInclude}" | |||||
| } | |||||
| task cleanUnusedmindsporeFiles(type: Delete, dependsOn: ['unzipMindSporeInclude']) { | |||||
| delete fileTree("${cleantargetMindSporeInclude}").matching { | |||||
| include "*.zip" | |||||
| } | |||||
| } | |||||
| if (file("libs/mindspore-lite-1.0.1.aar").exists()) { | |||||
| downloadMindSporeLibrary.enabled = false | |||||
| unzipMindSporeInclude.enabled = false | |||||
| cleanUnusedmindsporeFiles.enabled = false | |||||
| } | |||||
| if (file("src/main/assets/style_transfer_quant.ms").exists()) { | if (file("src/main/assets/style_transfer_quant.ms").exists()) { | ||||
| downloadTransferModelFile.enabled = false | downloadTransferModelFile.enabled = false | ||||
| } | } | ||||
| @@ -70,9 +34,6 @@ if (file("src/main/assets/style_predict_quant.ms").exists()) { | |||||
| preBuild.dependsOn downloadPredictModelFile | preBuild.dependsOn downloadPredictModelFile | ||||
| preBuild.dependsOn downloadTransferModelFile | preBuild.dependsOn downloadTransferModelFile | ||||
| preBuild.dependsOn downloadMindSporeLibrary | |||||
| preBuild.dependsOn unzipMindSporeInclude | |||||
| preBuild.dependsOn cleanUnusedmindsporeFiles | |||||
| class DownloadUrlTask extends DefaultTask { | class DownloadUrlTask extends DefaultTask { | ||||
| @Input | @Input | ||||
| @@ -1,5 +1,6 @@ | |||||
| <?xml version="1.0" encoding="utf-8"?> | <?xml version="1.0" encoding="utf-8"?> | ||||
| <manifest xmlns:android="http://schemas.android.com/apk/res/android" | <manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||||
| xmlns:tools="http://schemas.android.com/tools" | |||||
| package="com.mindspore.styletransfer"> | package="com.mindspore.styletransfer"> | ||||
| <application | <application | ||||
| @@ -10,7 +11,18 @@ | |||||
| android:name=".StyleMainActivity" | android:name=".StyleMainActivity" | ||||
| android:screenOrientation="portrait" | android:screenOrientation="portrait" | ||||
| android:theme="@style/Theme.AppCompat.NoActionBar" /> | android:theme="@style/Theme.AppCompat.NoActionBar" /> | ||||
| </application> | |||||
| <provider | |||||
| android:name="androidx.core.content.FileProvider" | |||||
| android:authorities="com.mindspore.styletransfer.fileprovider" | |||||
| android:exported="false" | |||||
| tools:replace="android:authorities" | |||||
| android:grantUriPermissions="true"> | |||||
| <meta-data | |||||
| tools:replace="android:resource" | |||||
| android:name="android.support.FILE_PROVIDER_PATHS" | |||||
| android:resource="@xml/style_file_paths" /> | |||||
| </provider> | |||||
| </application> | |||||
| </manifest> | </manifest> | ||||
| @@ -89,7 +89,7 @@ public class BitmapUtils { | |||||
| } | } | ||||
| // Scale pictures to screen width. | // Scale pictures to screen width. | ||||
| private static Bitmap zoomImage(Bitmap imageBitmap, int targetWidth, int maxHeight) { | |||||
| public static Bitmap zoomImage(Bitmap imageBitmap, int targetWidth, int maxHeight) { | |||||
| float scaleFactor = | float scaleFactor = | ||||
| Math.max( | Math.max( | ||||
| (float) imageBitmap.getWidth() / (float) targetWidth, | (float) imageBitmap.getWidth() / (float) targetWidth, | ||||
| @@ -104,6 +104,26 @@ public class BitmapUtils { | |||||
| return resizedBitmap; | return resizedBitmap; | ||||
| } | } | ||||
| public static Bitmap changeBitmapSize(Bitmap bitmap, int targetWidth, int targetHeight) { | |||||
| int width = bitmap.getWidth(); | |||||
| int height = bitmap.getHeight(); | |||||
| Log.e("width", "width:" + width); | |||||
| Log.e("height", "height:" + height); | |||||
| float scaleWidth = ((float) targetWidth) / width; | |||||
| float scaleHeight = ((float) targetHeight) / height; | |||||
| Matrix matrix = new Matrix(); | |||||
| matrix.postScale(scaleWidth, scaleHeight); | |||||
| bitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true); | |||||
| bitmap.getWidth(); | |||||
| bitmap.getHeight(); | |||||
| Log.e("newWidth", "newWidth" + bitmap.getWidth()); | |||||
| Log.e("newHeight", "newHeight" + bitmap.getHeight()); | |||||
| return bitmap; | |||||
| } | |||||
| /** | /** | ||||
| * Get the rotation angle of the photo. | * Get the rotation angle of the photo. | ||||
| * | * | ||||
| @@ -16,16 +16,24 @@ | |||||
| package com.mindspore.styletransfer; | package com.mindspore.styletransfer; | ||||
| import android.content.Context; | import android.content.Context; | ||||
| import android.content.Intent; | |||||
| import android.graphics.Bitmap; | import android.graphics.Bitmap; | ||||
| import android.graphics.BitmapFactory; | import android.graphics.BitmapFactory; | ||||
| import android.graphics.Color; | import android.graphics.Color; | ||||
| import android.graphics.Matrix; | import android.graphics.Matrix; | ||||
| import android.graphics.RectF; | import android.graphics.RectF; | ||||
| import android.media.MediaScannerConnection; | |||||
| import android.net.Uri; | |||||
| import android.os.Build; | |||||
| import android.os.Environment; | |||||
| import android.util.Log; | |||||
| import androidx.annotation.NonNull; | import androidx.annotation.NonNull; | ||||
| import androidx.exifinterface.media.ExifInterface; | import androidx.exifinterface.media.ExifInterface; | ||||
| import java.io.File; | import java.io.File; | ||||
| import java.io.FileNotFoundException; | |||||
| import java.io.FileOutputStream; | |||||
| import java.io.IOException; | import java.io.IOException; | ||||
| import java.io.InputStream; | import java.io.InputStream; | ||||
| import java.nio.ByteBuffer; | import java.nio.ByteBuffer; | ||||
| @@ -33,6 +41,8 @@ import java.nio.ByteOrder; | |||||
| public class ImageUtils { | public class ImageUtils { | ||||
| private static final String TAG = "ImageUtils"; | |||||
| private static Matrix decodeExifOrientation(int orientation) { | private static Matrix decodeExifOrientation(int orientation) { | ||||
| Matrix matrix = new Matrix(); | Matrix matrix = new Matrix(); | ||||
| @@ -198,4 +208,59 @@ public class ImageUtils { | |||||
| } | } | ||||
| return ret; | return ret; | ||||
| } | } | ||||
| // Save the picture to the system album and refresh it. | |||||
| public static void saveToAlbum(final Context context, Bitmap bitmap) { | |||||
| File file = null; | |||||
| String fileName = System.currentTimeMillis() + ".jpg"; | |||||
| File root = new File(Environment.getExternalStorageDirectory().getAbsoluteFile(), context.getPackageName()); | |||||
| File dir = new File(root, "image"); | |||||
| if (dir.mkdirs() || dir.isDirectory()) { | |||||
| file = new File(dir, fileName); | |||||
| } | |||||
| FileOutputStream os = null; | |||||
| try { | |||||
| os = new FileOutputStream(file); | |||||
| bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os); | |||||
| os.flush(); | |||||
| } catch (FileNotFoundException e) { | |||||
| Log.e(TAG, e.getMessage()); | |||||
| } catch (IOException e) { | |||||
| Log.e(TAG, e.getMessage()); | |||||
| } finally { | |||||
| try { | |||||
| if (os != null) { | |||||
| os.close(); | |||||
| } | |||||
| } catch (IOException e) { | |||||
| Log.e(TAG, e.getMessage()); | |||||
| } | |||||
| } | |||||
| if (file == null) { | |||||
| return; | |||||
| } | |||||
| // Gallery refresh. | |||||
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { | |||||
| String path = null; | |||||
| try { | |||||
| path = file.getCanonicalPath(); | |||||
| } catch (IOException e) { | |||||
| Log.e(TAG, e.getMessage()); | |||||
| } | |||||
| MediaScannerConnection.scanFile(context, new String[]{path}, null, | |||||
| new MediaScannerConnection.OnScanCompletedListener() { | |||||
| @Override | |||||
| public void onScanCompleted(String path, Uri uri) { | |||||
| Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); | |||||
| mediaScanIntent.setData(uri); | |||||
| context.sendBroadcast(mediaScanIntent); | |||||
| } | |||||
| }); | |||||
| } else { | |||||
| String relationDir = file.getParent(); | |||||
| File file1 = new File(relationDir); | |||||
| context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.fromFile(file1.getAbsoluteFile()))); | |||||
| } | |||||
| } | |||||
| } | } | ||||
| @@ -0,0 +1,9 @@ | |||||
| package com.mindspore.styletransfer; | |||||
| import android.view.View; | |||||
| public interface OnBackgroundImageListener { | |||||
| void onBackImageSelected(int position); | |||||
| void onImageAdd(View view); | |||||
| } | |||||
| @@ -1,5 +0,0 @@ | |||||
| package com.mindspore.styletransfer; | |||||
| public interface OnListFragmentInteractionListener { | |||||
| void onListFragmentInteraction(String item); | |||||
| } | |||||
| @@ -18,44 +18,54 @@ package com.mindspore.styletransfer; | |||||
| import android.content.Intent; | import android.content.Intent; | ||||
| import android.content.res.Configuration; | import android.content.res.Configuration; | ||||
| import android.graphics.Bitmap; | import android.graphics.Bitmap; | ||||
| import android.graphics.BitmapFactory; | |||||
| import android.net.Uri; | import android.net.Uri; | ||||
| import android.os.Bundle; | import android.os.Bundle; | ||||
| import android.os.Environment; | |||||
| import android.provider.MediaStore; | import android.provider.MediaStore; | ||||
| import android.text.TextUtils; | |||||
| import android.util.Log; | import android.util.Log; | ||||
| import android.util.Pair; | import android.util.Pair; | ||||
| import android.view.View; | import android.view.View; | ||||
| import android.widget.Button; | |||||
| import android.widget.ImageView; | import android.widget.ImageView; | ||||
| import android.widget.ProgressBar; | |||||
| import android.widget.TextView; | |||||
| import android.widget.Toast; | import android.widget.Toast; | ||||
| import androidx.annotation.Nullable; | import androidx.annotation.Nullable; | ||||
| import androidx.appcompat.app.AppCompatActivity; | import androidx.appcompat.app.AppCompatActivity; | ||||
| import androidx.core.content.FileProvider; | |||||
| import androidx.recyclerview.widget.GridLayoutManager; | import androidx.recyclerview.widget.GridLayoutManager; | ||||
| import androidx.recyclerview.widget.RecyclerView; | import androidx.recyclerview.widget.RecyclerView; | ||||
| import com.alibaba.android.arouter.facade.annotation.Route; | import com.alibaba.android.arouter.facade.annotation.Route; | ||||
| import com.bumptech.glide.Glide; | import com.bumptech.glide.Glide; | ||||
| import java.io.IOException; | |||||
| import java.util.ArrayList; | |||||
| import java.util.Arrays; | |||||
| import java.util.List; | |||||
| import java.io.File; | |||||
| import java.io.FileNotFoundException; | |||||
| @Route(path = "/styletransfer/StyleMainActivity") | @Route(path = "/styletransfer/StyleMainActivity") | ||||
| public class StyleMainActivity extends AppCompatActivity implements View.OnClickListener, OnListFragmentInteractionListener { | |||||
| public class StyleMainActivity extends AppCompatActivity implements OnBackgroundImageListener { | |||||
| private static final String TAG = "StyleMainActivity"; | private static final String TAG = "StyleMainActivity"; | ||||
| private static final int[] IMAGES = {R.drawable.style0, R.drawable.style1, R.drawable.style2, R.drawable.style3, R.drawable.style4, | |||||
| R.drawable.style5, R.drawable.style6, R.drawable.style7, R.drawable.style8, R.drawable.style9, | |||||
| R.drawable.style10, R.drawable.style11, R.drawable.style12, R.drawable.style13, R.drawable.style14, | |||||
| R.drawable.style15, R.drawable.style16, R.drawable.style17, R.drawable.style18, R.drawable.style19, R.drawable.add}; | |||||
| private static final int RC_CHOOSE_PHOTO = 1; | private static final int RC_CHOOSE_PHOTO = 1; | ||||
| private static final int RC_CHOOSE_PHOTO_FOR_BACKGROUND = 11; | |||||
| private static final int RC_CHOOSE_CAMERA = 2; | |||||
| private StyleTransferModelExecutor transferModelExecutor; | private StyleTransferModelExecutor transferModelExecutor; | ||||
| private boolean isRunningModel; | private boolean isRunningModel; | ||||
| private ImageView imgOrigin; | |||||
| private Button btnImage; | |||||
| private ImageView imgPreview; | |||||
| private Uri imageUri; | private Uri imageUri; | ||||
| private TextView textOriginImage; | |||||
| private ProgressBar progressBar; | |||||
| private RecyclerView recyclerView; | private RecyclerView recyclerView; | ||||
| @@ -63,8 +73,7 @@ public class StyleMainActivity extends AppCompatActivity implements View.OnClick | |||||
| private Integer maxHeightOfImage; | private Integer maxHeightOfImage; | ||||
| private boolean isLandScape; | private boolean isLandScape; | ||||
| private Bitmap originBitmap, styleBitmap; | |||||
| private String selectedStyle; | |||||
| private Bitmap originBitmap, styleBitmap, resultBitmap; | |||||
| @Override | @Override | ||||
| protected void onCreate(Bundle savedInstanceState) { | protected void onCreate(Bundle savedInstanceState) { | ||||
| @@ -75,52 +84,78 @@ public class StyleMainActivity extends AppCompatActivity implements View.OnClick | |||||
| } | } | ||||
| private void init() { | private void init() { | ||||
| imgOrigin = findViewById(R.id.img_origin); | |||||
| btnImage = findViewById(R.id.btn_image); | |||||
| imgOrigin.setOnClickListener(this); | |||||
| btnImage.setOnClickListener(this); | |||||
| imgPreview = findViewById(R.id.img_origin); | |||||
| textOriginImage = findViewById(R.id.tv_image); | |||||
| progressBar = findViewById(R.id.progress); | |||||
| recyclerView = findViewById(R.id.recyclerview); | recyclerView = findViewById(R.id.recyclerview); | ||||
| List<String> styles = new ArrayList<>(); | |||||
| try { | |||||
| styles.addAll(Arrays.asList(getAssets().list("thumbnails"))); | |||||
| } catch (IOException e) { | |||||
| e.printStackTrace(); | |||||
| } | |||||
| GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3); | |||||
| recyclerView.setLayoutManager(gridLayoutManager); | |||||
| recyclerView.setAdapter(new StyleRecyclerViewAdapter(this, styles, this)); | |||||
| recyclerView.setLayoutManager(new GridLayoutManager(this, 3)); | |||||
| recyclerView.setAdapter(new StyleRecyclerViewAdapter(this, IMAGES, this)); | |||||
| transferModelExecutor = new StyleTransferModelExecutor(this, false); | transferModelExecutor = new StyleTransferModelExecutor(this, false); | ||||
| } | } | ||||
| public void onClickPhoto(View view) { | |||||
| openGallay(RC_CHOOSE_PHOTO); | |||||
| textOriginImage.setVisibility(View.GONE); | |||||
| } | |||||
| @Override | |||||
| public void onClick(View view) { | |||||
| if (view.getId() == R.id.img_origin || view.getId() == R.id.btn_image) { | |||||
| btnImage.setVisibility(View.GONE); | |||||
| openGallay(); | |||||
| public void onClickCamera(View view) { | |||||
| openCamera(); | |||||
| textOriginImage.setVisibility(View.GONE); | |||||
| } | |||||
| public void onClickRecovery(View view) { | |||||
| if (originBitmap != null) { | |||||
| Glide.with(this).load(originBitmap).into(imgPreview); | |||||
| } else { | |||||
| Toast.makeText(this, "Please select an original picture first", Toast.LENGTH_SHORT).show(); | |||||
| } | |||||
| } | |||||
| public void onClickSave(View view) { | |||||
| if (this.resultBitmap == null) { | |||||
| Log.e(TAG, "null processed image"); | |||||
| Toast.makeText(this.getApplicationContext(), R.string.no_pic_neededSave, Toast.LENGTH_SHORT).show(); | |||||
| } else { | |||||
| ImageUtils.saveToAlbum(getApplicationContext(), this.resultBitmap); | |||||
| Toast.makeText(this.getApplicationContext(), R.string.save_success, Toast.LENGTH_SHORT).show(); | |||||
| } | } | ||||
| } | } | ||||
| private void openGallay() { | |||||
| private void openGallay(int request) { | |||||
| Intent intentToPickPic = new Intent(Intent.ACTION_PICK, null); | Intent intentToPickPic = new Intent(Intent.ACTION_PICK, null); | ||||
| intentToPickPic.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*"); | intentToPickPic.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*"); | ||||
| startActivityForResult(intentToPickPic, RC_CHOOSE_PHOTO); | |||||
| startActivityForResult(intentToPickPic, request); | |||||
| } | |||||
| private void openCamera() { | |||||
| Intent intentToTakePhoto = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); | |||||
| String mTempPhotoPath = Environment.getExternalStorageDirectory() + File.separator + "photo.jpeg"; | |||||
| imageUri = FileProvider.getUriForFile(this, getApplicationContext().getPackageName() + ".fileprovider", new File(mTempPhotoPath)); | |||||
| intentToTakePhoto.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); | |||||
| startActivityForResult(intentToTakePhoto, RC_CHOOSE_CAMERA); | |||||
| } | } | ||||
| @Override | @Override | ||||
| protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { | protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { | ||||
| super.onActivityResult(requestCode, resultCode, data); | super.onActivityResult(requestCode, resultCode, data); | ||||
| if (RC_CHOOSE_PHOTO == requestCode && null != data && null != data.getData()) { | |||||
| if (data != null) { | |||||
| this.imageUri = data.getData(); | |||||
| showOriginImage(); | |||||
| if (resultCode == RESULT_OK) { | |||||
| if (RC_CHOOSE_PHOTO == requestCode) { | |||||
| if (null != data && null != data.getData()) { | |||||
| this.imageUri = data.getData(); | |||||
| showOriginImage(); | |||||
| } else { | |||||
| finish(); | |||||
| } | |||||
| } else if (RC_CHOOSE_PHOTO_FOR_BACKGROUND == requestCode) { | |||||
| if (null != data && null != data.getData()) { | |||||
| showCustomBack(data.getData()); | |||||
| } else { | |||||
| finish(); | |||||
| } | |||||
| } else if (RC_CHOOSE_CAMERA == requestCode) { | |||||
| showOriginCamera(); | |||||
| } | } | ||||
| } else { | |||||
| finish(); | |||||
| } | } | ||||
| } | } | ||||
| @@ -128,49 +163,81 @@ public class StyleMainActivity extends AppCompatActivity implements View.OnClick | |||||
| Pair<Integer, Integer> targetedSize = this.getTargetSize(); | Pair<Integer, Integer> targetedSize = this.getTargetSize(); | ||||
| int targetWidth = targetedSize.first; | int targetWidth = targetedSize.first; | ||||
| int maxHeight = targetedSize.second; | int maxHeight = targetedSize.second; | ||||
| originBitmap = BitmapUtils.loadFromPath(StyleMainActivity.this, imageUri, targetWidth, maxHeight); | |||||
| originBitmap = BitmapUtils.loadFromPath(this, imageUri, targetWidth, maxHeight); | |||||
| // Determine how much to scale down the image. | // Determine how much to scale down the image. | ||||
| Log.i(TAG, "resized image size width:" + originBitmap.getWidth() + ",height: " + originBitmap.getHeight()); | |||||
| Log.e(TAG, "resized image size width:" + originBitmap.getWidth() + ",height: " + originBitmap.getHeight()); | |||||
| if (originBitmap != null) { | |||||
| Glide.with(this).load(originBitmap).into(imgPreview); | |||||
| } | |||||
| } | |||||
| private void showOriginCamera() { | |||||
| try { | |||||
| Pair<Integer, Integer> targetedSize = this.getTargetSize(); | |||||
| int targetWidth = targetedSize.first; | |||||
| int maxHeight = targetedSize.second; | |||||
| Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri)); | |||||
| originBitmap = BitmapUtils.zoomImage(bitmap, targetWidth, maxHeight); | |||||
| } catch (FileNotFoundException e) { | |||||
| e.printStackTrace(); | |||||
| } | |||||
| // Determine how much to scale down the image. | |||||
| Log.e(TAG, "resized image size width:" + originBitmap.getWidth() + ",height: " + originBitmap.getHeight()); | |||||
| if (originBitmap != null) { | if (originBitmap != null) { | ||||
| Glide.with(this).load(originBitmap).into(imgOrigin); | |||||
| Glide.with(this).load(originBitmap).into(imgPreview); | |||||
| } | } | ||||
| } | } | ||||
| private void showCustomBack(Uri imageUri) { | |||||
| Pair<Integer, Integer> targetedSize = this.getTargetSize(); | |||||
| int targetWidth = targetedSize.first; | |||||
| int maxHeight = targetedSize.second; | |||||
| styleBitmap = BitmapUtils.loadFromPath(this, imageUri, targetWidth, maxHeight); | |||||
| startRunningModel(styleBitmap); | |||||
| } | |||||
| @Override | @Override | ||||
| public void onListFragmentInteraction(String item) { | |||||
| this.selectedStyle = item; | |||||
| startRunningModel(); | |||||
| public void onBackImageSelected(int position) { | |||||
| styleBitmap = BitmapFactory.decodeResource(getResources(), IMAGES[position]); | |||||
| startRunningModel(styleBitmap); | |||||
| } | } | ||||
| private void startRunningModel() { | |||||
| if (!isRunningModel && !TextUtils.isEmpty(selectedStyle)) { | |||||
| styleBitmap = ImageUtils.loadBitmapFromResources(this, getUriFromAssetThumb(selectedStyle)); | |||||
| if (originBitmap == null) { | |||||
| Toast.makeText(this, "Please select an original picture first", Toast.LENGTH_SHORT).show(); | |||||
| return; | |||||
| } | |||||
| @Override | |||||
| public void onImageAdd(View view) { | |||||
| openGallay(RC_CHOOSE_PHOTO_FOR_BACKGROUND); | |||||
| } | |||||
| private void startRunningModel(Bitmap styleBitmap) { | |||||
| if (originBitmap == null) { | |||||
| Toast.makeText(this, "Please select an original picture first", Toast.LENGTH_SHORT).show(); | |||||
| return; | |||||
| } | |||||
| if (!isRunningModel) { | |||||
| isRunningModel = true; | isRunningModel = true; | ||||
| progressBar.setVisibility(View.VISIBLE); | |||||
| ModelExecutionResult result = transferModelExecutor.execute(originBitmap, styleBitmap); | ModelExecutionResult result = transferModelExecutor.execute(originBitmap, styleBitmap); | ||||
| Glide.with(this).load(result.getStyledImage()).into(imgOrigin); | |||||
| if (null != result && null != result.getStyledImage()) { | |||||
| resultBitmap = BitmapUtils.changeBitmapSize(result.getStyledImage(), originBitmap.getWidth(), originBitmap.getHeight()); | |||||
| Log.e("AAA", "w>>" + resultBitmap.getWidth() + ">>>h>>" + resultBitmap.getHeight()); | |||||
| Glide.with(this).load(resultBitmap).override(resultBitmap.getWidth(), resultBitmap.getHeight()).into(imgPreview); | |||||
| } else { | |||||
| Toast.makeText(this, "ModelExecute failed", Toast.LENGTH_SHORT).show(); | |||||
| } | |||||
| isRunningModel = false; | isRunningModel = false; | ||||
| progressBar.setVisibility(View.INVISIBLE); | |||||
| } else { | } else { | ||||
| Toast.makeText(this, "Previous Model still running", Toast.LENGTH_SHORT).show(); | Toast.makeText(this, "Previous Model still running", Toast.LENGTH_SHORT).show(); | ||||
| } | } | ||||
| } | } | ||||
| private String getUriFromAssetThumb(String thumb) { | |||||
| return "thumbnails/" + thumb; | |||||
| } | |||||
| // Returns max width of image. | // Returns max width of image. | ||||
| private Integer getMaxWidthOfImage() { | private Integer getMaxWidthOfImage() { | ||||
| if (this.maxWidthOfImage == null) { | if (this.maxWidthOfImage == null) { | ||||
| if (this.isLandScape) { | if (this.isLandScape) { | ||||
| this.maxWidthOfImage = ((View) this.imgOrigin.getParent()).getHeight(); | |||||
| this.maxWidthOfImage = ((View) this.imgPreview.getParent()).getHeight(); | |||||
| } else { | } else { | ||||
| this.maxWidthOfImage = ((View) this.imgOrigin.getParent()).getWidth(); | |||||
| this.maxWidthOfImage = ((View) this.imgPreview.getParent()).getWidth(); | |||||
| } | } | ||||
| } | } | ||||
| return this.maxWidthOfImage; | return this.maxWidthOfImage; | ||||
| @@ -180,9 +247,9 @@ public class StyleMainActivity extends AppCompatActivity implements View.OnClick | |||||
| private Integer getMaxHeightOfImage() { | private Integer getMaxHeightOfImage() { | ||||
| if (this.maxHeightOfImage == null) { | if (this.maxHeightOfImage == null) { | ||||
| if (this.isLandScape) { | if (this.isLandScape) { | ||||
| this.maxHeightOfImage = ((View) this.imgOrigin.getParent()).getWidth(); | |||||
| this.maxHeightOfImage = ((View) this.imgPreview.getParent()).getWidth(); | |||||
| } else { | } else { | ||||
| this.maxHeightOfImage = ((View) this.imgOrigin.getParent()).getHeight(); | |||||
| this.maxHeightOfImage = ((View) this.imgPreview.getParent()).getHeight(); | |||||
| } | } | ||||
| } | } | ||||
| return this.maxHeightOfImage; | return this.maxHeightOfImage; | ||||
| @@ -16,7 +16,6 @@ | |||||
| package com.mindspore.styletransfer; | package com.mindspore.styletransfer; | ||||
| import android.content.Context; | import android.content.Context; | ||||
| import android.net.Uri; | |||||
| import android.view.LayoutInflater; | import android.view.LayoutInflater; | ||||
| import android.view.View; | import android.view.View; | ||||
| import android.view.ViewGroup; | import android.view.ViewGroup; | ||||
| @@ -27,35 +26,16 @@ import androidx.recyclerview.widget.RecyclerView; | |||||
| import com.bumptech.glide.Glide; | import com.bumptech.glide.Glide; | ||||
| import java.util.List; | |||||
| public class StyleRecyclerViewAdapter extends RecyclerView.Adapter<StyleRecyclerViewAdapter.StyleItemViewHolder> { | public class StyleRecyclerViewAdapter extends RecyclerView.Adapter<StyleRecyclerViewAdapter.StyleItemViewHolder> { | ||||
| private View.OnClickListener mOnClickListener; | |||||
| private List<String> stylesList; | |||||
| private Context context; | |||||
| private OnListFragmentInteractionListener mListener; | |||||
| private final int[] IMAGES; | |||||
| private final Context context; | |||||
| private final OnBackgroundImageListener mListener; | |||||
| public StyleRecyclerViewAdapter(Context context, List<String> stylesList, OnListFragmentInteractionListener mListener) { | |||||
| this.stylesList = stylesList; | |||||
| public StyleRecyclerViewAdapter(Context context, int[] IMAGES, OnBackgroundImageListener mListener) { | |||||
| this.IMAGES = IMAGES; | |||||
| this.context = context; | this.context = context; | ||||
| this.mListener = mListener; | this.mListener = mListener; | ||||
| this.mOnClickListener = new View.OnClickListener() { | |||||
| @Override | |||||
| public void onClick(View view) { | |||||
| } | |||||
| }; | |||||
| this.mOnClickListener = (View.OnClickListener) (new View.OnClickListener() { | |||||
| public final void onClick(View v) { | |||||
| if (v.getTag() != null && v.getTag() instanceof String) { | |||||
| mListener.onListFragmentInteraction(String.valueOf(v.getTag())); | |||||
| } | |||||
| } | |||||
| }); | |||||
| } | } | ||||
| @NonNull | @NonNull | ||||
| @@ -68,21 +48,27 @@ public class StyleRecyclerViewAdapter extends RecyclerView.Adapter<StyleRecycler | |||||
| @Override | @Override | ||||
| public void onBindViewHolder(@NonNull StyleItemViewHolder holder, int position) { | public void onBindViewHolder(@NonNull StyleItemViewHolder holder, int position) { | ||||
| String imagePath = stylesList.get(position); | |||||
| Glide.with(context). | Glide.with(context). | ||||
| load(Uri.parse("file:///android_asset/thumbnails/" + imagePath)). | |||||
| centerInside(). | |||||
| load(IMAGES[position]). | |||||
| into(holder.getImageView()); | into(holder.getImageView()); | ||||
| View view = holder.getMView(); | View view = holder.getMView(); | ||||
| view.setTag(imagePath); | |||||
| view.setOnClickListener(this.mOnClickListener); | |||||
| view.setTag(IMAGES[position]); | |||||
| view.setOnClickListener(view1 -> { | |||||
| if (mListener != null) { | |||||
| if (IMAGES.length - 1 == position) { | |||||
| mListener.onImageAdd(holder.getImageView()); | |||||
| } else { | |||||
| mListener.onBackImageSelected(position); | |||||
| } | |||||
| } | |||||
| }); | |||||
| } | } | ||||
| @Override | @Override | ||||
| public int getItemCount() { | public int getItemCount() { | ||||
| return stylesList == null ? 0 : stylesList.size(); | |||||
| return IMAGES == null ? 0 : IMAGES.length; | |||||
| } | } | ||||
| @@ -113,8 +113,6 @@ public class StyleTransferModelExecutor { | |||||
| /** | /** | ||||
| * float 数组转 byte数组. | |||||
| * | |||||
| * @param floats the floats | * @param floats the floats | ||||
| * @return the byte [ ] | * @return the byte [ ] | ||||
| */ | */ | ||||
| @@ -127,23 +125,15 @@ public class StyleTransferModelExecutor { | |||||
| } | } | ||||
| @SuppressLint("LongLogTag") | @SuppressLint("LongLogTag") | ||||
| // public ModelExecutionResult execute(String contentImagePath, String styleImageName) { | |||||
| public ModelExecutionResult execute(Bitmap contentImage, Bitmap styleBitmap) { | public ModelExecutionResult execute(Bitmap contentImage, Bitmap styleBitmap) { | ||||
| Log.i(TAG, "running models"); | Log.i(TAG, "running models"); | ||||
| fullExecutionTime = SystemClock.uptimeMillis(); | fullExecutionTime = SystemClock.uptimeMillis(); | ||||
| preProcessTime = SystemClock.uptimeMillis(); | preProcessTime = SystemClock.uptimeMillis(); | ||||
| // Bitmap contentImage = ImageUtils.decodeBitmap(new File(contentImagePath)); | |||||
| ByteBuffer contentArray = | ByteBuffer contentArray = | ||||
| ImageUtils.bitmapToByteBuffer(contentImage, CONTENT_IMAGE_SIZE, CONTENT_IMAGE_SIZE, 0, 255); | ImageUtils.bitmapToByteBuffer(contentImage, CONTENT_IMAGE_SIZE, CONTENT_IMAGE_SIZE, 0, 255); | ||||
| // Bitmap styleBitmap = | |||||
| // ImageUtils.loadBitmapFromResources(context, "thumbnails/" + styleImageName); | |||||
| ByteBuffer input = ImageUtils.bitmapToByteBuffer(styleBitmap, STYLE_IMAGE_SIZE, STYLE_IMAGE_SIZE, 0, 255); | ByteBuffer input = ImageUtils.bitmapToByteBuffer(styleBitmap, STYLE_IMAGE_SIZE, STYLE_IMAGE_SIZE, 0, 255); | ||||
| List<MSTensor> Predict_inputs = Predict_session.getInputs(); | List<MSTensor> Predict_inputs = Predict_session.getInputs(); | ||||
| if (Predict_inputs.size() != 1) { | if (Predict_inputs.size() != 1) { | ||||
| return null; | return null; | ||||
| @@ -154,7 +144,6 @@ public class StyleTransferModelExecutor { | |||||
| preProcessTime = SystemClock.uptimeMillis() - preProcessTime; | preProcessTime = SystemClock.uptimeMillis() - preProcessTime; | ||||
| stylePredictTime = SystemClock.uptimeMillis(); | stylePredictTime = SystemClock.uptimeMillis(); | ||||
| if (!Predict_session.runGraph()) { | if (!Predict_session.runGraph()) { | ||||
| Log.e("MS_LITE", "Run Predict_graph failed"); | Log.e("MS_LITE", "Run Predict_graph failed"); | ||||
| return null; | return null; | ||||
| @@ -186,8 +175,6 @@ public class StyleTransferModelExecutor { | |||||
| MSTensor Transform_inputs_inTensor1 = Transform_inputs.get(1); | MSTensor Transform_inputs_inTensor1 = Transform_inputs.get(1); | ||||
| Transform_inputs_inTensor1.setData(contentArray); | Transform_inputs_inTensor1.setData(contentArray); | ||||
| styleTransferTime = SystemClock.uptimeMillis(); | styleTransferTime = SystemClock.uptimeMillis(); | ||||
| if (!Transform_session.runGraph()) { | if (!Transform_session.runGraph()) { | ||||
| @@ -197,7 +184,6 @@ public class StyleTransferModelExecutor { | |||||
| styleTransferTime = SystemClock.uptimeMillis() - styleTransferTime; | styleTransferTime = SystemClock.uptimeMillis() - styleTransferTime; | ||||
| Log.d(TAG, "Style apply Time to run: " + styleTransferTime); | Log.d(TAG, "Style apply Time to run: " + styleTransferTime); | ||||
| postProcessTime = SystemClock.uptimeMillis(); | postProcessTime = SystemClock.uptimeMillis(); | ||||
| // Get output tensor values. | // Get output tensor values. | ||||
| @@ -232,7 +218,6 @@ public class StyleTransferModelExecutor { | |||||
| outputImage[x] = arrayThree; | outputImage[x] = arrayThree; | ||||
| } | } | ||||
| Bitmap styledImage = | Bitmap styledImage = | ||||
| ImageUtils.convertArrayToBitmap(outputImage, CONTENT_IMAGE_SIZE, CONTENT_IMAGE_SIZE); | ImageUtils.convertArrayToBitmap(outputImage, CONTENT_IMAGE_SIZE, CONTENT_IMAGE_SIZE); | ||||
| postProcessTime = SystemClock.uptimeMillis() - postProcessTime; | postProcessTime = SystemClock.uptimeMillis() - postProcessTime; | ||||
| @@ -0,0 +1,20 @@ | |||||
| <?xml version="1.0" encoding="utf-8"?> | |||||
| <animated-rotate xmlns:android="http://schemas.android.com/apk/res/android" | |||||
| android:fromDegrees="0" | |||||
| android:pivotX="50%" | |||||
| android:pivotY="50%" | |||||
| android:toDegrees="360"> | |||||
| <shape | |||||
| android:innerRadiusRatio="3" | |||||
| android:shape="ring" | |||||
| android:thicknessRatio="8" | |||||
| android:useLevel="false"> | |||||
| <gradient | |||||
| android:centerColor="#62AEEC" | |||||
| android:centerY="0.50" | |||||
| android:endColor="#1063A5" | |||||
| android:startColor="#61C2EC" | |||||
| android:type="sweep" | |||||
| android:useLevel="false" /> | |||||
| </shape> | |||||
| </animated-rotate> | |||||
| @@ -18,6 +18,7 @@ | |||||
| android:drawableStart="@drawable/logo2" | android:drawableStart="@drawable/logo2" | ||||
| android:drawablePadding="5dp" | android:drawablePadding="5dp" | ||||
| android:gravity="center_vertical" | android:gravity="center_vertical" | ||||
| android:maxLines="1" | |||||
| android:text="MindSpore StyleTransfer" | android:text="MindSpore StyleTransfer" | ||||
| android:textColor="#ffffff" | android:textColor="#ffffff" | ||||
| android:textSize="20sp" /> | android:textSize="20sp" /> | ||||
| @@ -25,42 +26,117 @@ | |||||
| <FrameLayout | <FrameLayout | ||||
| android:layout_width="match_parent" | android:layout_width="match_parent" | ||||
| android:layout_height="wrap_content"> | |||||
| android:layout_height="300dp"> | |||||
| <ImageView | <ImageView | ||||
| android:id="@+id/img_origin" | android:id="@+id/img_origin" | ||||
| android:layout_width="256dp" | |||||
| android:layout_height="256dp" | |||||
| android:layout_width="wrap_content" | |||||
| android:layout_height="wrap_content" | |||||
| android:layout_gravity="center" | android:layout_gravity="center" | ||||
| android:layout_margin="10dp" | android:layout_margin="10dp" | ||||
| android:scaleType="fitXY" /> | android:scaleType="fitXY" /> | ||||
| <Button | |||||
| android:id="@+id/btn_image" | |||||
| <TextView | |||||
| android:id="@+id/tv_image" | |||||
| android:layout_width="206dp" | android:layout_width="206dp" | ||||
| android:layout_height="48dp" | android:layout_height="48dp" | ||||
| android:layout_gravity="center" | android:layout_gravity="center" | ||||
| android:background="@color/gray_btn" | |||||
| android:gravity="center" | |||||
| android:paddingLeft="4dp" | android:paddingLeft="4dp" | ||||
| android:text="Choose a Image" | android:text="Choose a Image" | ||||
| android:textAllCaps="false" | android:textAllCaps="false" | ||||
| android:textColor="@color/white" | android:textColor="@color/white" | ||||
| android:textSize="16sp" /> | |||||
| android:textSize="20sp" /> | |||||
| <ProgressBar | |||||
| android:id="@+id/progress" | |||||
| android:layout_width="80dp" | |||||
| android:layout_height="80dp" | |||||
| android:layout_gravity="center" | |||||
| android:indeterminateDrawable="@drawable/progressbar" | |||||
| android:visibility="invisible" /> | |||||
| </FrameLayout> | </FrameLayout> | ||||
| <LinearLayout | |||||
| android:layout_width="match_parent" | |||||
| android:layout_height="wrap_content" | |||||
| android:orientation="horizontal"> | |||||
| <Button | |||||
| android:layout_width="0dp" | |||||
| android:layout_height="48dp" | |||||
| android:layout_marginLeft="20dp" | |||||
| android:layout_marginRight="5dp" | |||||
| android:layout_weight="1" | |||||
| android:background="@color/gray_btn" | |||||
| android:gravity="center" | |||||
| android:onClick="onClickPhoto" | |||||
| android:text="PHOTO" | |||||
| android:textAllCaps="false" | |||||
| android:textColor="@color/white" | |||||
| android:textSize="12sp" /> | |||||
| <Button | |||||
| android:layout_width="0dp" | |||||
| android:layout_height="48dp" | |||||
| android:layout_marginLeft="5dp" | |||||
| android:layout_marginRight="5dp" | |||||
| android:layout_weight="1" | |||||
| android:background="@color/gray_btn" | |||||
| android:gravity="center" | |||||
| android:onClick="onClickCamera" | |||||
| android:text="CAMERA" | |||||
| android:textAllCaps="false" | |||||
| android:textColor="@color/white" | |||||
| android:textSize="12sp" /> | |||||
| <Button | |||||
| android:layout_width="0dp" | |||||
| android:layout_height="48dp" | |||||
| android:layout_marginLeft="5dp" | |||||
| android:layout_marginRight="5dp" | |||||
| android:layout_weight="1" | |||||
| android:background="@color/gray_btn" | |||||
| android:gravity="center" | |||||
| android:onClick="onClickRecovery" | |||||
| android:text="RECOVERY" | |||||
| android:textAllCaps="false" | |||||
| android:textColor="@color/white" | |||||
| android:textSize="12sp" /> | |||||
| <Button | |||||
| android:layout_width="0dp" | |||||
| android:layout_height="48dp" | |||||
| android:layout_marginLeft="5dp" | |||||
| android:layout_marginRight="20dp" | |||||
| android:layout_weight="1" | |||||
| android:background="@color/gray_btn" | |||||
| android:gravity="center" | |||||
| android:onClick="onClickSave" | |||||
| android:text="SAVE" | |||||
| android:textAllCaps="false" | |||||
| android:textColor="@color/white" | |||||
| android:textSize="12sp" /> | |||||
| </LinearLayout> | |||||
| <TextView | <TextView | ||||
| android:layout_width="wrap_content" | android:layout_width="wrap_content" | ||||
| android:layout_height="wrap_content" | android:layout_height="wrap_content" | ||||
| android:layout_marginLeft="20dp" | android:layout_marginLeft="20dp" | ||||
| android:layout_marginTop="20dp" | |||||
| android:text="Choose a Style" | android:text="Choose a Style" | ||||
| android:textColor="@color/white" | android:textColor="@color/white" | ||||
| android:textSize="20sp" /> | android:textSize="20sp" /> | ||||
| <androidx.recyclerview.widget.RecyclerView | <androidx.recyclerview.widget.RecyclerView | ||||
| android:layout_margin="20dp" | |||||
| android:id="@+id/recyclerview" | android:id="@+id/recyclerview" | ||||
| android:layout_width="match_parent" | android:layout_width="match_parent" | ||||
| android:layout_height="wrap_content" /> | |||||
| android:layout_height="wrap_content" | |||||
| android:layout_margin="20dp" | |||||
| android:fadeScrollbars="false" | |||||
| android:scrollbarSize="6dp" | |||||
| android:scrollbarStyle="outsideInset" | |||||
| android:scrollbarThumbVertical="@color/gray" | |||||
| android:scrollbars="vertical" /> | |||||
| </LinearLayout> | </LinearLayout> | ||||
| @@ -1,6 +1,5 @@ | |||||
| <?xml version="1.0" encoding="utf-8"?> | <?xml version="1.0" encoding="utf-8"?> | ||||
| <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | |||||
| xmlns:app="http://schemas.android.com/apk/res-auto" | |||||
| <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |||||
| xmlns:tools="http://schemas.android.com/tools" | xmlns:tools="http://schemas.android.com/tools" | ||||
| android:layout_width="wrap_content" | android:layout_width="wrap_content" | ||||
| android:layout_height="wrap_content" | android:layout_height="wrap_content" | ||||
| @@ -8,14 +7,8 @@ | |||||
| <ImageView | <ImageView | ||||
| android:id="@+id/image_view" | android:id="@+id/image_view" | ||||
| android:layout_width="150dp" | |||||
| android:layout_height="150dp" | |||||
| android:padding="0dp" | |||||
| android:scaleType="centerCrop" | |||||
| app:layout_constraintBottom_toBottomOf="parent" | |||||
| app:layout_constraintDimensionRatio="W,1:1" | |||||
| app:layout_constraintEnd_toEndOf="parent" | |||||
| app:layout_constraintStart_toStartOf="parent" | |||||
| app:layout_constraintTop_toTopOf="parent" | |||||
| tools:srcCompat="@tools:sample/backgrounds/scenic" /> | |||||
| </androidx.constraintlayout.widget.ConstraintLayout> | |||||
| android:layout_width="wrap_content" | |||||
| android:layout_height="120dp" | |||||
| android:scaleType="fitXY" | |||||
| tools:srcCompat="@drawable/logo2" /> | |||||
| </LinearLayout> | |||||
| @@ -1,5 +1,6 @@ | |||||
| <resources> | <resources> | ||||
| <string name="app_name">StyleTransfer</string> | <string name="app_name">StyleTransfer</string> | ||||
| <!-- TODO: Remove or change this placeholder text --> | <!-- TODO: Remove or change this placeholder text --> | ||||
| <string name="hello_blank_fragment">Hello blank fragment</string> | |||||
| <string name="no_pic_neededSave">Null Image needed to save</string> | |||||
| <string name="save_success">Save success</string> | |||||
| </resources> | </resources> | ||||
| @@ -0,0 +1,6 @@ | |||||
| <?xml version="1.0" encoding="utf-8"?> | |||||
| <paths> | |||||
| <external-path | |||||
| name="external_files" | |||||
| path="." /> | |||||
| </paths> | |||||
| @@ -17,16 +17,16 @@ | |||||
| <activity android:name=".TestActivity" | <activity android:name=".TestActivity" | ||||
| android:screenOrientation="portrait" | android:screenOrientation="portrait" | ||||
| android:theme="@style/Theme.AppCompat.NoActionBar"> | android:theme="@style/Theme.AppCompat.NoActionBar"> | ||||
| </activity> | |||||
| <activity android:name=".MainActivity" | |||||
| android:screenOrientation="portrait" | |||||
| android:theme="@style/Theme.AppCompat.NoActionBar"> | |||||
| <intent-filter> | <intent-filter> | ||||
| <action android:name="android.intent.action.MAIN" /> | <action android:name="android.intent.action.MAIN" /> | ||||
| <category android:name="android.intent.category.LAUNCHER" /> | <category android:name="android.intent.category.LAUNCHER" /> | ||||
| </intent-filter> | </intent-filter> | ||||
| </activity> | </activity> | ||||
| <activity android:name=".MainActivity" | |||||
| android:screenOrientation="portrait" | |||||
| android:theme="@style/Theme.AppCompat.NoActionBar"> | |||||
| </activity> | |||||
| </application> | </application> | ||||
| </manifest> | </manifest> | ||||
| @@ -172,7 +172,7 @@ public class Posenet { | |||||
| * returns: | * returns: | ||||
| * person: a Person object containing data about keypoint locations and confidence scores | * person: a Person object containing data about keypoint locations and confidence scores | ||||
| */ | */ | ||||
| Person estimateSinglePose(Bitmap bitmap) { | |||||
| public Person estimateSinglePose(Bitmap bitmap) { | |||||
| long estimationStartTimeNanos = SystemClock.elapsedRealtimeNanos(); | long estimationStartTimeNanos = SystemClock.elapsedRealtimeNanos(); | ||||
| ByteBuffer inputArray = this.initInputArray(bitmap); | ByteBuffer inputArray = this.initInputArray(bitmap); | ||||
| List<MSTensor> inputs = session.getInputs(); | List<MSTensor> inputs = session.getInputs(); | ||||