!11514 [MS][LITE]20210121 HiMindSpore demo update to v1.1.4 fail

From: @sishuikang
Reviewed-by: 
Signed-off-by:
pull/11514/MERGE
mindspore-ci-bot 4 years ago committed by Gitee
commit 53e6dc7191

@ -1,5 +1,4 @@
apply plugin: 'com.android.application'
android {
compileSdkVersion 30
buildToolsVersion "30.0.1"
@ -8,8 +7,8 @@ android {
applicationId "com.mindspore.himindspore"
minSdkVersion 21
targetSdkVersion 30
versionCode 5
versionName "1.1.3"
versionCode 6
versionName "1.1.4"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
javaCompileOptions {
@ -51,7 +50,7 @@ android {
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.appcompat:appcompat:1.3.0-alpha02'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'androidx.cardview:cardview:1.0.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'

@ -18,4 +18,14 @@
# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
#-renamesourcefileattribute SourceFile
-keep class cn.sharesdk.**{*;}
-keep class com.sina.**{*;}
-keep class **.R$* {*;}
-keep class **.R{*;}
-keep class com.mob.**{*;}
-keep class m.framework.**{*;}
-dontwarn cn.sharesdk.**
-dontwarn com.sina.**
-dontwarn com.mob.**
-dontwarn **.R$*

@ -17,7 +17,6 @@ package com.mindspore.himindspore;
import android.Manifest;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
@ -62,8 +61,9 @@ public class SplashActivity extends BaseActivity<MainPresenter> implements MainC
private TextView versionText;
private static final String CODE_URL = "https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/lite";
private static final String HELP_URL = "https://github.com/mindspore-ai/mindspore/issues";
private static final String HELP_URL = "https://gitee.com/mindspore/mindspore/issues/new?issue%5Bassignee_id%5D=0&issue%5Bmilestone_id%5D=0";
private static final String STAR_URL = "https://gitee.com/mindspore/mindspore";
private static final String APK_URL = "https://download.mindspore.cn/model_zoo/official/lite/apk/mindmain.html";
@Override
@ -85,7 +85,7 @@ public class SplashActivity extends BaseActivity<MainPresenter> implements MainC
PackageManager packageManager = this.getPackageManager();
PackageInfo packageInfo = packageManager.getPackageInfo(this.getPackageName(), 0);
now_version = packageInfo.versionCode;
versionText.setText("Version: " + packageInfo.versionName);
versionText.setText(getString(R.string.title_version) + packageInfo.versionName);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
@ -134,21 +134,18 @@ public class SplashActivity extends BaseActivity<MainPresenter> implements MainC
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.setMessage(getResources().getString(R.string.app_need_permission));
builder.setPositiveButton(getResources().getString(R.string.app_permission_by_hand), (dialog, 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.setNegativeButton(getResources().getString(R.string.cancel), null);
builder.show();
}
@ -238,6 +235,16 @@ public class SplashActivity extends BaseActivity<MainPresenter> implements MainC
openBrowser(STAR_URL);
}
public void onClickShare(View view) {
Intent share_intent = new Intent();
share_intent.setAction(Intent.ACTION_SEND);
share_intent.setType("text/plain");
share_intent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.title_share));
share_intent.putExtra(Intent.EXTRA_TEXT, getString(R.string.title_share_commend) + APK_URL);
share_intent = Intent.createChooser(share_intent, getString(R.string.title_share));
startActivity(share_intent);
}
public void openBrowser(String url) {
Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
@ -262,55 +269,40 @@ public class SplashActivity extends BaseActivity<MainPresenter> implements MainC
}
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(android.R.drawable.ic_dialog_info);
builder.setTitle("下载完成");
builder.setMessage("是否安装");
builder.setTitle(getResources().getString(R.string.app_download_success));
builder.setMessage(getResources().getString(R.string.app_need_install));
builder.setCancelable(false);
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Intent.ACTION_VIEW);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Uri contentUri = FileProvider.getUriForFile(SplashActivity.this, "com.mindspore.himindspore.fileprovider",
new File(getApkPath(), "HiMindSpore.apk"));
intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
} else {
intent.setDataAndType(Uri.fromFile(new File(getApkPath(), "HiMindSpore.apk")), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
startActivity(intent);
builder.setPositiveButton(getResources().getString(R.string.confirm), (dialog, which) -> {
Intent intent = new Intent(Intent.ACTION_VIEW);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Uri contentUri = FileProvider.getUriForFile(SplashActivity.this, "com.mindspore.himindspore.fileprovider",
new File(getApkPath(), "HiMindSpore.apk"));
intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
} else {
intent.setDataAndType(Uri.fromFile(new File(getApkPath(), "HiMindSpore.apk")), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
startActivity(intent);
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
builder.setNegativeButton(getResources().getString(R.string.cancel), (dialog, which) -> {
});
builder.create().show();
}
public void showUpdate(final UpdateInfoBean updateInfo) {
if (now_version == updateInfo.getVersionCode()) {
// Toast.makeText(this, "已经是最新版本", Toast.LENGTH_SHORT).show();
Log.d(TAG + "版本号是", "onResponse: " + now_version);
} else {
if (now_version != updateInfo.getVersionCode()) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(android.R.drawable.ic_dialog_info);
builder.setTitle("请升级新版本" + updateInfo.getVersionName());
builder.setTitle(getResources().getString(R.string.app_update_lastest) + updateInfo.getVersionName());
builder.setMessage(updateInfo.getMessage());
builder.setCancelable(false);
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Log.e(TAG, String.valueOf(Environment.MEDIA_MOUNTED));
downFile();
}
builder.setPositiveButton(getResources().getString(R.string.confirm), (dialog, which) -> {
Log.e(TAG, String.valueOf(Environment.MEDIA_MOUNTED));
downFile();
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
builder.setNegativeButton(getResources().getString(R.string.cancel), (dialog, which) -> {
});
builder.create().show();
}
@ -319,8 +311,8 @@ public class SplashActivity extends BaseActivity<MainPresenter> implements MainC
public void downFile() {
progressDialog = new ProgressDialog(this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setTitle("正在下载");
progressDialog.setMessage("请稍候...");
progressDialog.setTitle(getResources().getString(R.string.app_is_loading));
progressDialog.setMessage(getResources().getString(R.string.app_wait));
progressDialog.setProgressNumberFormat("%1d Mb/%2d Mb");
progressDialog.setProgress(0);
progressDialog.show();
@ -332,7 +324,7 @@ public class SplashActivity extends BaseActivity<MainPresenter> implements MainC
@Override
public void onDownLoadFail(Throwable throwable) {
Toast.makeText(SplashActivity.this, "下载失败", Toast.LENGTH_LONG).show();
Toast.makeText(SplashActivity.this, getResources().getString(R.string.app_load_fail), Toast.LENGTH_LONG).show();
}
@Override
@ -358,7 +350,6 @@ public class SplashActivity extends BaseActivity<MainPresenter> implements MainC
directoryPath = getFilesDir() + File.separator + "apk";
}
File file = new File(directoryPath);
Log.e("测试路径", directoryPath);
if (!file.exists()) {
file.mkdirs();
}
@ -366,5 +357,4 @@ public class SplashActivity extends BaseActivity<MainPresenter> implements MainC
}
}

@ -22,6 +22,10 @@ import com.alibaba.android.arouter.launcher.ARouter;
public class MyApplication extends Application {
private static final String appKey = "31f0c1ffe8168";
private static final String appSecret = "83e393ced2b9f31771fcb81e1b7a049a";
@Override
public void onCreate() {
super.onCreate();

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 KiB

@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">MindSpore</string>
<string name="app_need_permission">"MindSpore needs to access the "camera" and "external storage", please go to "application information -> permissions" to grant!"</string>
<string name="app_permission_by_hand"> "Go to manual authorization"</string>
<string name="cancel">"Cancel"</string>
<string name="confirm">"OK"</string>
<string name="app_download_success"> "download complete"</string>
<string name="app_need_install"> "Whether to install"</string>
<string name="app_is_loading"> "downloading"</string>
<string name="app_wait">"Please wait..."</string>
<string name="app_load_fail"> "download failed"</string>
<string name="app_update_lastest"> "Please upgrade to a new version"</string>
<string name="title_image">Image Classification</string>
<string name="title_image_garbage">Garbage Classification</string>
<string name="title_object">Photo Detection</string>
<string name="title_object_camera">Camera Detection</string>
<string name="title_pose_net">PoseNet</string>
<string name="title_style_transfer">Style Transfer</string>
<string name="title_segmentation">Image Segmentation</string>
<string name="title_source">Code</string>
<string name="title_scene">Scene Detection</string>
<string name="title_help">FeedBack</string>
<string name="title_star">Star</string>
<string name="title_version">"Version: "</string>
<string name="title_share">Share</string>
<string name="title_share_commend">MindSpore APPA one-stop collaborative learning, demonstration, and experience platform for beginners\t</string>
</resources>

@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">MindSpore</string>
<string name="app_need_permission">"MindSpore需要访问 “相机" 和 “外部存储器",请到 “应用信息 -> 权限" 中授予!"</string>
<string name="app_permission_by_hand">"去手动授权"</string>
<string name="cancel">"取消"</string>
<string name="confirm">"确定"</string>
<string name="app_download_success">"下载完成"</string>
<string name="app_need_install">"是否安装"</string>
<string name="app_is_loading">"正在下载"</string>
<string name="app_wait">"请稍候..."</string>
<string name="app_load_fail">"下载失败"</string>
<string name="app_update_lastest">"请升级新版本"</string>
<string name="title_image">图像分类</string>
<string name="title_image_garbage">垃圾分类</string>
<string name="title_object">照片检测</string>
<string name="title_object_camera">视频检测</string>
<string name="title_pose_net"> 骨骼检测</string>
<string name="title_style_transfer">风格迁移</string>
<string name="title_segmentation">图像分割</string>
<string name="title_source">代码</string>
<string name="title_scene">场景检测</string>
<string name="title_help">反馈</string>
<string name="title_star">打星</string>
<string name="title_version">版本号: </string>
<string name="title_share">分享</string>
<string name="title_share_commend">MindSpore APP初学者的一站式协同学习、演示、体验平台\t</string>
</resources>

@ -1,26 +1,29 @@
<resources>
<string name="app_name">HiMindSpore</string>
<string name="app_name_title">MindSpore</string>
<string name="switch_custom">custom</string>
<string name="app_name">MindSpore</string>
<string name="app_need_permission">"MindSpore需要访问 “相机” 和 “外部存储器”,请到 “应用信息 -> 权限” 中授予!"</string>
<string name="app_permission_by_hand">"去手动授权"</string>
<string name="cancel">"取消"</string>
<string name="confirm">"确定"</string>
<string name="app_download_success">"下载完成"</string>
<string name="app_need_install">"是否安装"</string>
<string name="app_is_loading">"正在下载"</string>
<string name="app_wait">"请稍候..."</string>
<string name="app_load_fail">"下载失败"</string>
<string name="app_update_lastest">"请升级新版本"</string>
<string name="title_image">Image Classification</string>
<string name="title_image_garbage">Garbage Classification</string>
<string name="title_object">Photo Detection</string>
<string name="title_object_camera">Camera Detection</string>
<string name="title_pose_net">PoseNet</string>
<string name="title_style_transfer">Style Transfer</string>
<string name="title_segmentation">Image Segmentation</string>
<string name="title_source">Code</string>
<string name="title_scene">Scene Detection</string>
<string name="title_help">FeedBack</string>
<string name="title_star">Star</string>
<string name="title_segmentation">Image Segmentation</string>
<string name="title_photo">Photo</string>
<string name="title_camera">Camera</string>
<string name="title_demo">Demo</string>
<string name="title_custom">Custom</string>
<string name="title_time">Inference Time</string>
<string name="title_version">Version: </string>
<string name="title_share">Share</string>
<string name="title_share_commend">MindSpore APP初学者的一站式协同学习、演示、体验平台\t</string>
</resources>

@ -9,9 +9,6 @@ buildscript {
}
dependencies {
classpath "com.android.tools.build:gradle:4.0.1"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}

@ -8,11 +8,12 @@ cmake_minimum_required(VERSION 3.4.1)
set(CMAKE_VERBOSE_MAKEFILE on)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/libs/${ANDROID_ABI})
set(MINDSPORELITE_VERSION mindspore-lite-1.0.1-runtime-arm64-cpu)
set(MINDSPORELITE_VERSION mindspore-lite-1.1.0-inference-android)
# ============== Set MindSpore Dependencies. =============
include_directories(${CMAKE_SOURCE_DIR}/src/main/cpp)
include_directories(${CMAKE_SOURCE_DIR}/src/main/cpp/${MINDSPORELITE_VERSION}/third_party/flatbuffers/include)
include_directories(${CMAKE_SOURCE_DIR}/src/main/cpp/${MINDSPORELITE_VERSION}/third_party/hiai_ddk/lib/aarch64)
include_directories(${CMAKE_SOURCE_DIR}/src/main/cpp/${MINDSPORELITE_VERSION})
include_directories(${CMAKE_SOURCE_DIR}/src/main/cpp/${MINDSPORELITE_VERSION}/include)
include_directories(${CMAKE_SOURCE_DIR}/src/main/cpp/${MINDSPORELITE_VERSION}/include/ir/dtype)
@ -21,14 +22,21 @@ include_directories(${CMAKE_SOURCE_DIR}/src/main/cpp/${MINDSPORELITE_VERSION}/mi
add_library(mindspore-lite SHARED IMPORTED)
add_library(minddata-lite SHARED IMPORTED)
#add_library(libmindspore-lite-fp16 SHARED IMPORTED )
add_library(hiai SHARED IMPORTED)
add_library(hiai_ir SHARED IMPORTED)
add_library(hiai_ir_build SHARED IMPORTED)
set_target_properties(mindspore-lite PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/src/main/cpp/${MINDSPORELITE_VERSION}/lib/libmindspore-lite.so)
${CMAKE_SOURCE_DIR}/src/main/cpp/${MINDSPORELITE_VERSION}/lib/aarch64/libmindspore-lite.so)
set_target_properties(minddata-lite PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/src/main/cpp/${MINDSPORELITE_VERSION}/minddata/lib/libminddata-lite.so)
#set_target_properties(libmindspore-lite-fp16 PROPERTIES IMPORTED_LOCATION
# ${CMAKE_SOURCE_DIR}/src/main/cpp/${MINDSPORELITE_VERSION}/lib/libmindspore-lite-fp16.so)
${CMAKE_SOURCE_DIR}/src/main/cpp/${MINDSPORELITE_VERSION}/minddata/lib/aarch64/libminddata-lite.so)
set_target_properties(hiai PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/src/main/cpp/${MINDSPORELITE_VERSION}/third_party/hiai_ddk/lib/aarch64/libhiai.so)
set_target_properties(hiai_ir PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/src/main/cpp/${MINDSPORELITE_VERSION}/third_party/hiai_ddk/lib/aarch64/libhiai_ir.so)
set_target_properties(hiai_ir_build PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/src/main/cpp/${MINDSPORELITE_VERSION}/third_party/hiai_ddk/lib/aarch64/libhiai_ir_build.so)
# --------------- MindSpore Lite set End. --------------------
@ -75,6 +83,9 @@ target_link_libraries( # Specifies the target library.
# --- mindspore ---
minddata-lite
mindspore-lite
hiai
hiai_ir
hiai_ir_build
# libmindspore-lite-fp16
# --- other dependencies.---

@ -71,7 +71,7 @@ apply from: 'download.gradle'
dependencies {
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.appcompat:appcompat:1.3.0-alpha02'
implementation 'com.google.android.material:material:1.2.1'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'

@ -4,13 +4,18 @@
* The libraries can be downloaded manually.
*/
def targetMindSporeInclude = "src/main/cpp/"
def mindsporeLite_Version = "mindspore-lite-1.0.1-runtime-arm64-cpu"
def mindsporeLite_Version = "mindspore-lite-1.1.0-inference-android"
def mindsporeLite_Version_AAR = "mindspore-lite-maven-1.1.0"
def targetModelFile = "src/main/assets/model/mobilenetv2.ms"
def mindSporeLibrary_arm64 = "src/main/cpp/${mindsporeLite_Version}.tar.gz"
def modelDownloadUrl = "https://download.mindspore.cn/model_zoo/official/lite/mobilenetv2_openimage_lite/mobilenetv2.ms"
def mindsporeLiteDownloadUrl = "https://ms-release.obs.cn-north-4.myhuaweicloud.com/1.0.1/lite/android_aarch64/${mindsporeLite_Version}.tar.gz"
def mindsporeLiteDownloadUrl = "https://ms-release.obs.cn-north-4.myhuaweicloud.com/1.1.0/MindSpore/lite/release/android/${mindsporeLite_Version}.tar.gz"
def mindSporeLibraryAAR = "src/main/cpp/${mindsporeLite_Version}/${mindsporeLite_Version_AAR}.zip"
def cleantargetMindSporeIncludeAAR = "../mindsporelibrary"
def targetMindSporeIncludeAAR = "../mindsporelibrary/"
def targetObjectModelFile = "src/main/assets/model/ssd.ms"
def targetGarbageModelFile = "src/main/assets/model/garbage_mobilenetv2.ms"
@ -66,15 +71,29 @@ task unzipMindSporeInclude(type: Copy, dependsOn: 'downloadMindSporeLibrary') {
}
task cleanUnusedmindsporeFiles(type: Delete, dependsOn: ['unzipMindSporeInclude']) {
delete fileTree("${cleantargetMindSporeInclude}").matching {
delete fileTree("${targetMindSporeInclude}").matching {
include "*.tar.gz"
}
}
task unzipMindSporeIncludeAAR(type: Copy, dependsOn: ['cleanUnusedmindsporeFiles']) {
doFirst {
println "Unzipping ${mindSporeLibraryAAR}"
}
from zipTree("${mindSporeLibraryAAR}")
into "${targetMindSporeIncludeAAR}"
}
task cleanUnusedmindsporeAARFiles(type: Delete, dependsOn: ['unzipMindSporeIncludeAAR']) {
delete fileTree("${cleantargetMindSporeIncludeAAR}").matching {
include "*.zip"
}
}
/*
* Using preBuild to download mindspore library and model file.
* Run before gradle build.
*/
if (file("src/main/cpp/${mindsporeLite_Version}/lib/libmindspore-lite.so").exists()) {
if (file("src/main/cpp/${mindsporeLite_Version}/lib/aarch64/libmindspore-lite.so").exists()) {
downloadMindSporeLibrary.enabled = false
unzipMindSporeInclude.enabled = false
cleanUnusedmindsporeFiles.enabled = false
@ -92,13 +111,22 @@ if (file("src/main/assets/model/ssd.ms").exists()) {
downloadObjectModelFile.enabled = false
}
if (file("../mindsporelibrary/mindspore/mindspore-lite/1.1.0/mindspore-lite-1.1.0.aar").exists()) {
downloadMindSporeLibrary.enabled = false
unzipMindSporeIncludeAAR.enabled = false
cleanUnusedmindsporeAARFiles.enabled = false
}
preBuild.dependsOn cleanCmakeCache
preBuild.dependsOn downloadModelFile
preBuild.dependsOn downloadObjectModelFile
preBuild.dependsOn downloadMindSporeLibrary
preBuild.dependsOn downloadGarbageModelFile
preBuild.dependsOn unzipMindSporeInclude
preBuild.dependsOn unzipMindSporeIncludeAAR
preBuild.dependsOn cleanUnusedmindsporeFiles
preBuild.dependsOn cleanUnusedmindsporeAARFiles
class DownloadUrlTask extends DefaultTask {
@Input
@ -111,4 +139,4 @@ class DownloadUrlTask extends DefaultTask {
void download() {
ant.get(src: sourceUrl, dest: target)
}
}
}

@ -35,42 +35,7 @@ using mindspore::dataset::LPixelType;
using mindspore::dataset::LDataType;
#define MS_PRINT(format, ...) __android_log_print(ANDROID_LOG_INFO, "MSJNI", format, ##__VA_ARGS__)
static const int RET_GARBAGE_SORT_SUM = 4;
static const char *labels_name_grbage_sort_map[RET_GARBAGE_SORT_SUM] = {"可回收物",
"干垃圾",
"有害垃圾",
"湿垃圾"};
static const int RET_GARBAGE_DETAILED_SUM = 26;
static const char *labels_name_grbage_detailed_map[RET_GARBAGE_DETAILED_SUM] = {
"塑料瓶",
"帽子",
"报纸",
"易拉罐",
"玻璃制品",
"玻璃瓶",
"硬纸板",
"篮球",
"纸张",
"金属制品",
"一次性筷子",
"打火机",
"扫把",
"旧镜子",
"牙刷",
"脏污衣服",
"贝壳",
"陶瓷碗",
"油漆桶",
"电池",
"荧光灯",
"药片胶囊",
"橙皮",
"菜叶",
"蛋壳",
"香蕉皮"};
char *CreateLocalModelBuffer(JNIEnv *env, jobject modelBuffer) {
jbyte *modelAddr = static_cast<jbyte *>(env->GetDirectBufferAddress(modelBuffer));
@ -86,7 +51,7 @@ char *CreateLocalModelBuffer(JNIEnv *env, jobject modelBuffer) {
* @return
*/
std::string
GarbageProcessRunnetResult(const int RET_CATEGORY_SUM, const char *const labels_name_map[],
GarbageProcessRunnetResult(const int RET_CATEGORY_SUM,
std::unordered_map<std::string, mindspore::tensor::MSTensor *> msOutputs) {
// Get the branch of the model output.
// Use iterators to get map elements.
@ -110,20 +75,7 @@ GarbageProcessRunnetResult(const int RET_CATEGORY_SUM, const char *const labels_
// Score for each category.
// Converted to text information that needs to be displayed in the APP.
std::string categoryScore = "";
if (maxIndex <= 9) {
categoryScore += labels_name_grbage_sort_map[0];
categoryScore += ":";
} else if (maxIndex > 9 && maxIndex <= 17) {
categoryScore += labels_name_grbage_sort_map[1];
categoryScore += ":";
} else if (maxIndex > 17 && maxIndex <= 21) {
categoryScore += labels_name_grbage_sort_map[2];
categoryScore += ":";
} else if (maxIndex > 21 && maxIndex <= 25) {
categoryScore += labels_name_grbage_sort_map[3];
categoryScore += ":";
}
categoryScore += labels_name_map[maxIndex];
categoryScore = std::to_string(maxIndex);
return categoryScore;
}
@ -317,7 +269,6 @@ Java_com_mindspore_imageobject_imageclassification_help_GarbageTrackingMobile_ru
}
std::string resultStr = GarbageProcessRunnetResult(::RET_GARBAGE_DETAILED_SUM,
::labels_name_grbage_detailed_map,
msOutputs);
const char *resultCharData = resultStr.c_str();

@ -123,7 +123,6 @@ ProcessRunnetResult(std::unordered_map<std::string, mindspore::tensor::MSTensor
auto branch1_tensor = iter->second;
MS_PRINT("%s %s", branch1_string.c_str(), branch2_string.c_str());
// ----------- 接口测试 --------------------------
float *tmpscores2 = reinterpret_cast<float *>(branch1_tensor->MutableData());
float *tmpdata = reinterpret_cast<float *>(branch2_tensor->MutableData());

@ -35,176 +35,7 @@ using mindspore::dataset::LPixelType;
using mindspore::dataset::LDataType;
#define MS_PRINT(format, ...) __android_log_print(ANDROID_LOG_INFO, "MSJNI", format, ##__VA_ARGS__)
static const int RET_CATEGORY_SUM = 365;
static const char *labels_name_map[RET_CATEGORY_SUM] = {"airfield", "airplane_cabin",
"airport_terminal", "alcove", "alley",
"amphitheater", "amusement_arcade",
"amusement_park",
"apartment_building/outdoor", "aquarium",
"aqueduct", "arcade", "arch",
"archaelogical_excavation", "archive",
"arena/hockey", "arena/performance",
"arena/rodeo", "army_base", "art_gallery",
"art_school", "art_studio", "artists_loft",
"assembly_line", "athletic_field/outdoor",
"atrium/public", "attic", "auditorium",
"auto_factory", "auto_showroom",
"badlands", "bakery/shop",
"balcony/exterior", "balcony/interior",
"ball_pit",
"ballroom", "bamboo_forest", "bank_vault",
"banquet_hall", "bar",
"barn", "barndoor", "baseball_field",
"basement", "basketball_court/indoor",
"bathroom", "bazaar/indoor",
"bazaar/outdoor", "beach", "beach_house",
"beauty_salon", "bedchamber", "bedroom",
"beer_garden", "beer_hall",
"berth", "biology_laboratory", "boardwalk",
"boat_deck", "boathouse",
"bookstore", "booth/indoor",
"botanical_garden", "bow_window/indoor",
"bowling_alley",
"boxing_ring", "bridge", "building_facade",
"bullring", "burial_chamber",
"bus_interior", "bus_station/indoor",
"butchers_shop", "butte", "cabin/outdoor",
"cafeteria", "campsite", "campus",
"canal/natural", "canal/urban",
"candy_store", "canyon", "car_interior",
"carrousel", "castle",
"catacomb", "cemetery", "chalet",
"chemistry_lab", "childs_room",
"church/indoor", "church/outdoor",
"classroom", "clean_room", "cliff",
"closet", "clothing_store", "coast",
"cockpit", "coffee_shop",
"computer_room", "conference_center",
"conference_room", "construction_site",
"corn_field",
"corral", "corridor", "cottage",
"courthouse", "courtyard",
"creek", "crevasse", "crosswalk", "dam",
"delicatessen",
"department_store", "desert/sand",
"desert/vegetation", "desert_road",
"diner/outdoor",
"dining_hall", "dining_room", "discotheque",
"doorway/outdoor", "dorm_room",
"downtown", "dressing_room", "driveway",
"drugstore", "elevator/door",
"elevator_lobby", "elevator_shaft",
"embassy", "engine_room", "entrance_hall",
"escalator/indoor", "excavation",
"fabric_store", "farm",
"fastfood_restaurant",
"field/cultivated", "field/wild",
"field_road", "fire_escape", "fire_station",
"fishpond", "flea_market/indoor",
"florist_shop/indoor", "food_court",
"football_field",
"forest/broadleaf", "forest_path",
"forest_road", "formal_garden", "fountain",
"galley", "garage/indoor", "garage/outdoor",
"gas_station", "gazebo/exterior",
"general_store/indoor",
"general_store/outdoor", "gift_shop",
"glacier", "golf_course",
"greenhouse/indoor", "greenhouse/outdoor",
"grotto", "gymnasium/indoor",
"hangar/indoor",
"hangar/outdoor", "harbor",
"hardware_store", "hayfield", "heliport",
"highway", "home_office", "home_theater",
"hospital", "hospital_room",
"hot_spring", "hotel/outdoor", "hotel_room",
"house", "hunting_lodge/outdoor",
"ice_cream_parlor", "ice_floe", "ice_shelf",
"ice_skating_rink/indoor",
"ice_skating_rink/outdoor",
"iceberg", "igloo", "industrial_area",
"inn/outdoor", "islet",
"jacuzzi/indoor", "jail_cell",
"japanese_garden", "jewelry_shop",
"junkyard",
"kasbah", "kennel/outdoor",
"kindergarden_classroom", "kitchen",
"lagoon",
"lake/natural", "landfill", "landing_deck",
"laundromat", "lawn",
"lecture_room", "legislative_chamber",
"library/indoor", "library/outdoor",
"lighthouse",
"living_room", "loading_dock", "lobby",
"lock_chamber", "locker_room",
"mansion", "manufactured_home",
"market/indoor", "market/outdoor", "marsh",
"martial_arts_gym", "mausoleum", "medina",
"mezzanine", "moat/water",
"mosque/outdoor", "motel", "mountain",
"mountain_path", "mountain_snowy",
"movie_theater/indoor", "museum/indoor",
"museum/outdoor", "music_studio",
"natural_history_museum",
"nursery", "nursing_home", "oast_house",
"ocean", "office",
"office_building", "office_cubicles",
"oilrig", "operating_room", "orchard",
"orchestra_pit", "pagoda", "palace",
"pantry", "park",
"parking_garage/indoor",
"parking_garage/outdoor", "parking_lot",
"pasture", "patio",
"pavilion", "pet_shop", "pharmacy",
"phone_booth", "physics_laboratory",
"picnic_area", "pier", "pizzeria",
"playground", "playroom",
"plaza", "pond", "porch", "promenade",
"pub/indoor",
"racecourse", "raceway", "raft",
"railroad_track", "rainforest",
"reception", "recreation_room",
"repair_shop", "residential_neighborhood",
"restaurant",
"restaurant_kitchen", "restaurant_patio",
"rice_paddy", "river", "rock_arch",
"roof_garden", "rope_bridge", "ruin",
"runway", "sandbox",
"sauna", "schoolhouse", "science_museum",
"server_room", "shed",
"shoe_shop", "shopfront",
"shopping_mall/indoor", "shower",
"ski_resort",
"ski_slope", "sky", "skyscraper", "slum",
"snowfield",
"soccer_field", "stable",
"stadium/baseball", "stadium/football",
"stadium/soccer",
"stage/indoor", "stage/outdoor",
"staircase", "storage_room", "street",
"subway_station/platform", "supermarket",
"sushi_bar", "swamp", "swimming_hole",
"swimming_pool/indoor",
"swimming_pool/outdoor",
"synagogue/outdoor", "television_room",
"television_studio",
"temple/asia", "throne_room",
"ticket_booth", "topiary_garden", "tower",
"toyshop", "train_interior",
"train_station/platform", "tree_farm",
"tree_house",
"trench", "tundra", "underwater/ocean_deep",
"utility_room", "valley",
"vegetable_garden", "veterinarians_office",
"viaduct", "village", "vineyard",
"volcano", "volleyball_court/outdoor",
"waiting_room", "water_park", "water_tower",
"waterfall", "watering_hole", "wave",
"wet_bar", "wheat_field",
"wind_farm", "windmill", "yard",
"youth_hostel", "zen_garden"};
char *SceneCreateLocalModelBuffer(JNIEnv *env, jobject modelBuffer) {
jbyte *modelAddr = static_cast<jbyte *>(env->GetDirectBufferAddress(modelBuffer));
@ -219,7 +50,7 @@ char *SceneCreateLocalModelBuffer(JNIEnv *env, jobject modelBuffer) {
* @param msOutputs
* @return
*/
std::string SceneProcessRunnetResult(const int RET_CATEGORY_SUM, const char *const labels_name_map[],
std::string SceneProcessRunnetResult(const int RET_CATEGORY_SUM,
std::unordered_map<std::string, mindspore::tensor::MSTensor *> msOutputs) {
// Get the branch of the model output.
// Use iterators to get map elements.
@ -251,7 +82,7 @@ std::string SceneProcessRunnetResult(const int RET_CATEGORY_SUM, const char *con
// Score for each category.
// Converted to text information that needs to be displayed in the APP.
std::string categoryScore = "";
categoryScore += labels_name_map[cat_loc];
categoryScore += std::to_string(cat_loc);
categoryScore += ":";
std::string score_str = std::to_string(scores);
categoryScore += score_str;
@ -448,8 +279,7 @@ Java_com_mindspore_imageobject_imageclassification_help_SceneTrackingMobile_runN
msOutputs.insert(std::pair<std::string, mindspore::tensor::MSTensor *>{name, temp_dat});
}
std::string resultStr = SceneProcessRunnetResult(::RET_CATEGORY_SUM,
::labels_name_map, msOutputs);
std::string resultStr = SceneProcessRunnetResult(::RET_CATEGORY_SUM, msOutputs);
const char *resultCharData = resultStr.c_str();
return (env)->NewStringUTF(resultCharData);

@ -64,23 +64,20 @@ public class ImageCameraActivity extends AppCompatActivity implements CameraPrev
int enterType;
private LinearLayout bottomLayout;
private List<RecognitionImageBean> recognitionObjectBeanList;
private CameraPreview cameraPreview;
private ImageTrackingMobile imageTrackingMobile;
private GarbageTrackingMobile garbageTrackingMobile;
private SceneTrackingMobile sceneTrackingMobile;
private RecognitionImageBean bean;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//inject
ARouter.getInstance().inject(this);
setContentView(R.layout.activity_image_camera);
cameraPreview = findViewById(R.id.image_camera_preview);
bottomLayout = findViewById(R.id.layout_bottom_content);
cameraPreview.setVisibility(View.VISIBLE);
@ -172,34 +169,48 @@ public class ImageCameraActivity extends AppCompatActivity implements CameraPrev
String[] resultArray = result.split(";");
for (String singleRecognitionResult : resultArray) {
String[] singleResult = singleRecognitionResult.split(":");
int lableIndex = Integer.parseInt(singleResult[0]);
float score = Float.parseFloat(singleResult[1]);
String[] IMAGEOBJECT = getResources().getStringArray(R.array.image_category);
if (score > 0.5) {
recognitionObjectBeanList.add(new RecognitionImageBean(singleResult[0], score));
recognitionObjectBeanList.add(new RecognitionImageBean(IMAGEOBJECT[lableIndex], score));
}
}
Collections.sort(recognitionObjectBeanList, new Comparator<RecognitionImageBean>() {
@Override
public int compare(RecognitionImageBean t1, RecognitionImageBean t2) {
return Float.compare(t2.getScore(), t1.getScore());
}
});
Collections.sort(recognitionObjectBeanList, (t1, t2) -> Float.compare(t2.getScore(), t1.getScore()));
}
runOnUiThread(() -> showResultsInBottomSheet(recognitionObjectBeanList, time));
} else if (TYPE_GARBAGE == enterType) {
runOnUiThread(() -> showResultsInBottomSheetGarbage(result, time));
int maxIndex = Integer.parseInt(result);
String[] GABAGETITLE = getResources().getStringArray(R.array.grbage_sort_map);
StringBuilder categoryScore = new StringBuilder();
if (maxIndex <= 9) {
categoryScore.append(GABAGETITLE[0]);
categoryScore.append(":");
} else if (maxIndex > 9 && maxIndex <= 17) {
categoryScore.append(GABAGETITLE[1]);
categoryScore.append(":");
} else if (maxIndex > 17 && maxIndex <= 21) {
categoryScore.append(GABAGETITLE[2]);
categoryScore.append(":");
} else if (maxIndex > 21 && maxIndex <= 25) {
categoryScore.append(GABAGETITLE[3]);
categoryScore.append(":");
}
categoryScore.append(getResources().getStringArray(R.array.grbage_sort_detailed_map)[maxIndex]);
String finalCategoryScore = categoryScore.toString();
runOnUiThread(() -> showResultsInBottomSheetGarbage(finalCategoryScore, time));
} else if (TYPE_SCENE == enterType) {
if (!result.equals("") && result.contains(":")) {
String[] resultArray = result.split(":");
bean = new RecognitionImageBean(resultArray[0], Float.valueOf(resultArray[1]));
int lableIndex = Integer.parseInt(resultArray[0]);
float score = Float.parseFloat(resultArray[1]);
String[] SCENEOBJECT = getResources().getStringArray(R.array.scene_category);
bean = new RecognitionImageBean(SCENEOBJECT[lableIndex], score);
}
runOnUiThread(() -> showResultsInBottomSheetScene(bean, time));
}
}
RecognitionImageBean bean;
@UiThread
protected void showResultsInBottomSheet(List<RecognitionImageBean> list, String time) {
bottomLayout.removeAllViews();
@ -259,14 +270,14 @@ public class ImageCameraActivity extends AppCompatActivity implements CameraPrev
bottomLayout.addView(horTextView);
HorTextView horTimeView = new HorTextView(this);
horTimeView.setLeftTitle("Inference Time");
horTimeView.setLeftTitle(getResources().getString(R.string.title_time));
horTimeView.setRightContent(time);
horTimeView.setBottomLineVisible(View.INVISIBLE);
bottomLayout.addView(horTimeView);
} else {
TextView textView = new TextView(this);
textView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
textView.setText("Keep moving.");
textView.setText(getResources().getString(R.string.title_keep));
textView.setGravity(Gravity.CENTER);
textView.setTextColor(Color.BLACK);
textView.setTextSize(30);
@ -277,12 +288,10 @@ public class ImageCameraActivity extends AppCompatActivity implements CameraPrev
private void showLoadView() {
TextView textView = new TextView(this);
textView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
textView.setText("Keep moving.");
textView.setText(getResources().getString(R.string.title_keep));
textView.setGravity(Gravity.CENTER);
textView.setTextColor(Color.WHITE);
textView.setTextSize(30);
bottomLayout.addView(textView);
}
}

@ -84,7 +84,6 @@ public class ObjectTrackingMobile {
*/
public boolean loadModelFromBuf(AssetManager assetManager) {
String ModelPath = "model/ssd.ms";
ByteBuffer buffer = loadModelFile(ModelPath);
netEnv = loadModel(assetManager, buffer, 2);
return true;

@ -16,7 +16,6 @@
package com.mindspore.imageobject.objectdetection.ui;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
@ -25,8 +24,6 @@ import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.util.Pair;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
@ -36,10 +33,11 @@ import androidx.appcompat.app.AppCompatActivity;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.mindspore.imageobject.R;
import com.mindspore.imageobject.objectdetection.bean.RecognitionObjectBean;
import com.mindspore.imageobject.objectdetection.help.ObjectTrackingMobile;
import com.mindspore.imageobject.util.BitmapUtils;
import com.mindspore.imageobject.util.DisplayUtil;
import com.mindspore.imageobject.objectdetection.help.ObjectTrackingMobile;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.List;
@ -57,13 +55,8 @@ public class ObjectPhotoActivity extends AppCompatActivity {
private ObjectTrackingMobile trackingMobile;
private List<RecognitionObjectBean> recognitionObjectBeanList;
private Integer maxWidthOfImage;
private Integer maxHeightOfImage;
boolean isLandScape;
private Bitmap originBitmap;
Uri imageUri;
private Uri imageUri;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -71,10 +64,7 @@ public class ObjectPhotoActivity extends AppCompatActivity {
setContentView(R.layout.activity_object_photo);
preview = findViewById(R.id.img_photo);
this.isLandScape = getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;
openGallay();
}
private void openGallay() {
@ -86,10 +76,9 @@ public class ObjectPhotoActivity extends AppCompatActivity {
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (RC_CHOOSE_PHOTO == requestCode && null != data && null != data.getData()) {
this.imageUri = data.getData();
showOriginImage();
this.imageUri = data.getData();
showOriginImage();
} else {
Toast.makeText(this, R.string.image_invalid, Toast.LENGTH_LONG).show();
finish();
@ -97,13 +86,10 @@ public class ObjectPhotoActivity extends AppCompatActivity {
}
private void showOriginImage() {
Pair<Integer, Integer> targetedSize = this.getTargetSize();
int targetWidth = targetedSize.first;
int maxHeight = targetedSize.second;
originBitmap = BitmapUtils.loadFromPath(this, imageUri, targetWidth, maxHeight).copy(Bitmap.Config.ARGB_8888, true);
// Determine how much to scale down the image.
Log.i(ObjectPhotoActivity.TAG, "resized image size width:" + originBitmap.getWidth() + ",height: " + originBitmap.getHeight());
File file = BitmapUtils.getFileFromMediaUri(this, imageUri);
Bitmap photoBmp = BitmapUtils.getBitmapFormUri(this, Uri.fromFile(file));
int degree = BitmapUtils.getBitmapDegree(file.getAbsolutePath());
originBitmap = BitmapUtils.rotateBitmapByDegree(photoBmp, degree).copy(Bitmap.Config.ARGB_8888, true);
if (originBitmap != null) {
initMindspore(originBitmap);
preview.setImageBitmap(originBitmap);
@ -171,41 +157,4 @@ public class ObjectPhotoActivity extends AppCompatActivity {
}
BitmapUtils.recycleBitmap(originBitmap);
}
// Returns max width of image.
private Integer getMaxWidthOfImage() {
if (this.maxWidthOfImage == null) {
if (this.isLandScape) {
this.maxWidthOfImage = ((View) this.preview.getParent()).getHeight();
} else {
this.maxWidthOfImage = ((View) this.preview.getParent()).getWidth();
}
}
return this.maxWidthOfImage;
}
// Returns max height of image.
private Integer getMaxHeightOfImage() {
if (this.maxHeightOfImage == null) {
if (this.isLandScape) {
this.maxHeightOfImage = ((View) this.preview.getParent()).getWidth();
} else {
this.maxHeightOfImage = ((View) this.preview.getParent()).getHeight();
}
}
return this.maxHeightOfImage;
}
// Gets the targeted size(width / height).
private Pair<Integer, Integer> getTargetSize() {
Integer targetWidth;
Integer targetHeight;
Integer maxWidth = this.getMaxWidthOfImage();
Integer maxHeight = this.getMaxHeightOfImage();
targetWidth = this.isLandScape ? maxHeight : maxWidth;
targetHeight = this.isLandScape ? maxWidth : maxHeight;
Log.i(TAG, "height:" + targetHeight + ",width:" + targetWidth);
return new Pair<>(targetWidth, targetHeight);
}
}

@ -16,15 +16,19 @@
package com.mindspore.imageobject.util;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.Log;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
@ -40,112 +44,122 @@ public class BitmapUtils {
}
}
private static String getImagePath(Activity activity, Uri uri) {
String[] projection = {MediaStore.Images.Media.DATA};
Cursor cursor = activity.managedQuery(uri, projection, null, null, null);
int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(columnIndex);
}
public static Bitmap loadFromPath(Activity activity, int id, int width, int height) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
InputStream is = activity.getResources().openRawResource(id);
int sampleSize = calculateInSampleSize(options, width, height);
options.inSampleSize = sampleSize;
options.inJustDecodeBounds = false;
return zoomImage(BitmapFactory.decodeStream(is), width, height);
}
public static Bitmap loadFromPath(Activity activity, Uri uri, int width, int height) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
public static Bitmap getBitmapFormUri(Activity ac, Uri uri) {
Bitmap bitmap = null;
try {
InputStream input = ac.getContentResolver().openInputStream(uri);
BitmapFactory.Options onlyBoundsOptions = new BitmapFactory.Options();
onlyBoundsOptions.inJustDecodeBounds = true;
onlyBoundsOptions.inDither = true;//optional
onlyBoundsOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;//optional
BitmapFactory.decodeStream(input, null, onlyBoundsOptions);
input.close();
int originalWidth = onlyBoundsOptions.outWidth;
int originalHeight = onlyBoundsOptions.outHeight;
if ((originalWidth == -1) || (originalHeight == -1))
return null;
float hh = 1920f;
float ww = 1080f;
int be = 1;
if (originalWidth > originalHeight && originalWidth > ww) {
be = (int) (originalWidth / ww);
} else if (originalWidth < originalHeight && originalHeight > hh) {
be = (int) (originalHeight / hh);
}
if (be <= 0) {
be = 1;
}
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmapOptions.inSampleSize = be;
bitmapOptions.inDither = true;//optional
bitmapOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;//optional
input = ac.getContentResolver().openInputStream(uri);
bitmap = BitmapFactory.decodeStream(input, null, bitmapOptions);
input.close();
String path = getImagePath(activity, uri);
BitmapFactory.decodeFile(path, options);
int sampleSize = calculateInSampleSize(options, width, height);
options.inSampleSize = sampleSize;
options.inJustDecodeBounds = false;
Bitmap bitmap = zoomImage(BitmapFactory.decodeFile(path, options), width, height);
return rotateBitmap(bitmap, getRotationAngle(path));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return compressImage(bitmap);
}
private static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
final int width = options.outWidth;
final int height = options.outHeight;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
// Calculate height and required height scale.
final int heightRatio = Math.round((float) height / (float) reqHeight);
// Calculate width and required width scale.
final int widthRatio = Math.round((float) width / (float) reqWidth);
// Take the larger of the values.
inSampleSize = heightRatio > widthRatio ? heightRatio : widthRatio;
public static Bitmap compressImage(Bitmap image) {
if (image != null) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
int options = 100;
while (baos.toByteArray().length / 1024 > 100) {
baos.reset();
image.compress(Bitmap.CompressFormat.JPEG, options, baos);
options -= 10;
}
ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);
return bitmap;
}else {
return null;
}
return inSampleSize;
}
// Scale pictures to screen width.
private static Bitmap zoomImage(Bitmap imageBitmap, int targetWidth, int maxHeight) {
float scaleFactor =
Math.max(
(float) imageBitmap.getWidth() / (float) targetWidth,
(float) imageBitmap.getHeight() / (float) maxHeight);
Bitmap resizedBitmap =
Bitmap.createScaledBitmap(
imageBitmap,
(int) (imageBitmap.getWidth() / scaleFactor),
(int) (imageBitmap.getHeight() / scaleFactor),
true);
return resizedBitmap;
public static File getFileFromMediaUri(Context ac, Uri uri) {
if (uri.getScheme().toString().compareTo("content") == 0) {
ContentResolver cr = ac.getContentResolver();
Cursor cursor = cr.query(uri, null, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
String filePath = cursor.getString(cursor.getColumnIndex("_data"));
cursor.close();
if (filePath != null) {
return new File(filePath);
}
}
} else if (uri.getScheme().toString().compareTo("file") == 0) {
return new File(uri.toString().replace("file://", ""));
}
return null;
}
/**
* Get the rotation angle of the photo.
*
* @param path photo path.
* @return angle.
*/
public static int getRotationAngle(String path) {
int rotation = 0;
public static int getBitmapDegree(String path) {
int degree = 0;
try {
ExifInterface exifInterface = new ExifInterface(path);
int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
rotation = 90;
degree = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotation = 180;
degree = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
rotation = 270;
break;
default:
degree = 270;
break;
}
} catch (IOException e) {
Log.e(TAG, "Failed to get rotation: " + e.getMessage());
e.printStackTrace();
}
return rotation;
return degree;
}
public static Bitmap rotateBitmap(Bitmap bitmap, int angle) {
public static Bitmap rotateBitmapByDegree(Bitmap bm, int degree) {
Bitmap returnBm = null;
Matrix matrix = new Matrix();
matrix.postRotate(angle);
Bitmap result = null;
matrix.postRotate(degree);
try {
result = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
returnBm = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);
} catch (OutOfMemoryError e) {
Log.e(TAG, "Failed to rotate bitmap: " + e.getMessage());
}
if (result == null) {
return bitmap;
if (returnBm == null) {
returnBm = bm;
}
if (bm != returnBm) {
bm.recycle();
}
return result;
return returnBm;
}
}

@ -26,7 +26,7 @@
android:drawableStart="@drawable/logo2"
android:drawablePadding="5dp"
android:gravity="center_vertical"
android:text="@string/app_name_title"
android:text="@string/app_name"
android:textColor="@color/white"
android:textSize="20sp" />
@ -40,6 +40,4 @@
android:background="@color/colorPrimary"
android:gravity="center"
android:orientation="vertical" />
</RelativeLayout>

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">MindSpore</string>
<string name="title_time">推理时间</string>
<string name="title_keep">正在识别</string>
<string name="image_invalid">您选择的图像路径无效。请再次选择</string>
<string name="train_invalid">对不起,此图片中没有任何物体。请尝试另一张图片</string>
</resources>

Some files were not shown because too many files have changed in this diff Show More

Loading…
Cancel
Save