fix image classification premissions

pull/9908/head
hukang 4 years ago
parent 740ea71fc1
commit ac9c48afc0

@ -66,7 +66,7 @@ app
│ | └── MindSporeNetnative.h # header file │ | └── MindSporeNetnative.h # header file
│ | │ |
│ ├── java # application code at the Java layer │ ├── java # application code at the Java layer
│ │ └── com.mindspore.himindsporedemo │ │ └── com.mindspore.classification
│ │ ├── gallery.classify # implementation related to image processing and MindSpore JNI calling │ │ ├── gallery.classify # implementation related to image processing and MindSpore JNI calling
│ │ │ └── ... │ │ │ └── ...
│ │ └── widget # implementation related to camera enabling and drawing │ │ └── widget # implementation related to camera enabling and drawing

@ -75,7 +75,7 @@ app
| | └── MsNetWork.cpp # MindSpre接口封装 | | └── MsNetWork.cpp # MindSpre接口封装
│ | │ |
│ ├── java # java层应用代码 │ ├── java # java层应用代码
│ │ └── com.mindspore.himindsporedemo │ │ └── com.mindspore.classification
│ │ ├── gallery.classify # 图像处理及MindSpore JNI调用相关实现 │ │ ├── gallery.classify # 图像处理及MindSpore JNI调用相关实现
│ │ │ └── ... │ │ │ └── ...
│ │ └── widget # 开启摄像头及绘制相关实现 │ │ └── widget # 开启摄像头及绘制相关实现

@ -5,7 +5,7 @@ android {
buildToolsVersion "30.0.1" buildToolsVersion "30.0.1"
defaultConfig { defaultConfig {
applicationId "com.mindspore.himindsporedemo" applicationId "com.mindspore.classification"
minSdkVersion 21 minSdkVersion 21
targetSdkVersion 30 targetSdkVersion 30
versionCode 1 versionCode 1

@ -14,7 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
package com.mindspore.himindsporedemo; package com.mindspore.classification;
import android.content.Context; import android.content.Context;
@ -37,6 +37,6 @@ public class ExampleInstrumentedTest {
public void useAppContext() { public void useAppContext() {
// Context of the app under test. // Context of the app under test.
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext(); Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
assertEquals("com.mindspore.himindsporedemo", appContext.getPackageName()); assertEquals("com.mindspore.classification", appContext.getPackageName());
} }
} }

@ -1,6 +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"
package="com.mindspore.himindsporedemo" package="com.mindspore.classification"
android:versionCode="1" android:versionCode="1"
android:versionName="1.0"> android:versionName="1.0">
@ -20,7 +20,7 @@
android:supportsRtl="true" android:supportsRtl="true"
android:theme="@style/AppTheme"> android:theme="@style/AppTheme">
<activity <activity
android:name=".widget.CameraActivity" android:name="com.mindspore.classification.widget.CameraActivity"
android:screenOrientation="portrait"> android:screenOrientation="portrait">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />

@ -343,7 +343,7 @@ bool PreProcessImageData(const LiteMat &lite_mat_bgr, LiteMat *lite_norm_mat_ptr
*/ */
extern "C" extern "C"
JNIEXPORT jlong JNICALL JNIEXPORT jlong JNICALL
Java_com_mindspore_himindsporedemo_gallery_classify_TrackingMobile_loadModel(JNIEnv *env, Java_com_mindspore_classification_gallery_classify_TrackingMobile_loadModel(JNIEnv *env,
jobject thiz, jobject thiz,
jobject model_buffer, jobject model_buffer,
jint num_thread) { jint num_thread) {
@ -396,7 +396,7 @@ Java_com_mindspore_himindsporedemo_gallery_classify_TrackingMobile_loadModel(JNI
* sending a picture to the model and run inference. * sending a picture to the model and run inference.
*/ */
extern "C" JNIEXPORT jstring JNICALL extern "C" JNIEXPORT jstring JNICALL
Java_com_mindspore_himindsporedemo_gallery_classify_TrackingMobile_runNet(JNIEnv *env, jclass type, Java_com_mindspore_classification_gallery_classify_TrackingMobile_runNet(JNIEnv *env, jclass type,
jlong netEnv, jlong netEnv,
jobject srcBitmap) { jobject srcBitmap) {
LiteMat lite_mat_bgr, lite_norm_mat_cut; LiteMat lite_mat_bgr, lite_norm_mat_cut;
@ -469,7 +469,7 @@ Java_com_mindspore_himindsporedemo_gallery_classify_TrackingMobile_runNet(JNIEnv
} }
extern "C" JNIEXPORT jboolean JNICALL extern "C" JNIEXPORT jboolean JNICALL
Java_com_mindspore_himindsporedemo_gallery_classify_TrackingMobile_unloadModel(JNIEnv *env, Java_com_mindspore_classification_gallery_classify_TrackingMobile_unloadModel(JNIEnv *env,
jclass type, jclass type,
jlong netEnv) { jlong netEnv) {
MS_PRINT("MindSpore release net."); MS_PRINT("MindSpore release net.");

@ -14,7 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
package com.mindspore.himindsporedemo.gallery.classify; package com.mindspore.classification.gallery.classify;
import android.util.Size; import android.util.Size;
import java.util.Comparator; import java.util.Comparator;

@ -14,7 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
package com.mindspore.himindsporedemo.gallery.classify; package com.mindspore.classification.gallery.classify;
public class RecognitionObjectBean { public class RecognitionObjectBean {

@ -14,10 +14,9 @@
* limitations under the License. * limitations under the License.
*/ */
package com.mindspore.himindsporedemo.gallery.classify; package com.mindspore.classification.gallery.classify;
import android.content.Context; import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.util.Log; import android.util.Log;

@ -14,11 +14,10 @@
* limitations under the License. * limitations under the License.
*/ */
package com.mindspore.himindsporedemo.widget; package com.mindspore.classification.widget;
import android.content.Context; import android.content.Context;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.util.Log;
import android.view.TextureView; import android.view.TextureView;
public class AutoFitTextureView extends TextureView { public class AutoFitTextureView extends TextureView {
@ -64,7 +63,7 @@ public class AutoFitTextureView extends TextureView {
if (0 == mRatioWidth || 0 == mRatioHeight) { if (0 == mRatioWidth || 0 == mRatioHeight) {
setMeasuredDimension(width, height); setMeasuredDimension(width, height);
} else { } else {
if (width < height * mRatioWidth / mRatioHeight) { if (width > height * mRatioWidth / mRatioHeight) {
setMeasuredDimension(width, width * mRatioHeight / mRatioWidth); setMeasuredDimension(width, width * mRatioHeight / mRatioWidth);
} else { } else {
setMeasuredDimension(height * mRatioWidth / mRatioHeight, height); setMeasuredDimension(height * mRatioWidth / mRatioHeight, height);

@ -14,13 +14,17 @@
* limitations under the License. * limitations under the License.
*/ */
package com.mindspore.himindsporedemo.widget; package com.mindspore.classification.widget;
import android.Manifest; import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.graphics.Color; import android.graphics.Color;
import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.provider.Settings;
import android.util.Log; import android.util.Log;
import android.view.Gravity; import android.view.Gravity;
import android.view.View; import android.view.View;
@ -30,11 +34,16 @@ import android.widget.LinearLayout;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.UiThread; import androidx.annotation.UiThread;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import com.mindspore.himindsporedemo.R; import com.mindspore.classification.R;
import com.mindspore.himindsporedemo.gallery.classify.RecognitionObjectBean; import com.mindspore.classification.gallery.classify.RecognitionObjectBean;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
@ -48,11 +57,13 @@ import java.util.List;
public class CameraActivity extends AppCompatActivity{ public class CameraActivity extends AppCompatActivity{
private static final String TAG = "CameraActivity"; private static final String TAG = "CameraActivity";
private static final String BUNDLE_FRAGMENTS_KEY = "android:support:fragments"; 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 PERMISSIONS_REQUEST = 1; private static final int REQUEST_PERMISSION = 1;
private static final int REQUEST_PERMISSION_AGAIN = 2;
private boolean isAllGranted;
private static final String PERMISSION_CAMERA = Manifest.permission.CAMERA; private static final String BUNDLE_FRAGMENTS_KEY = "android:support:fragments";
private LinearLayout bottomLayout; private LinearLayout bottomLayout;
@ -67,63 +78,95 @@ public class CameraActivity extends AppCompatActivity{
// Clear the state of the fragment when rebuilding. // Clear the state of the fragment when rebuilding.
savedInstanceState.remove(BUNDLE_FRAGMENTS_KEY); savedInstanceState.remove(BUNDLE_FRAGMENTS_KEY);
} }
super.onCreate(null); super.onCreate(null);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.activity_camera); setContentView(R.layout.activity_camera);
if (hasPermission()) {
setFragment();
} else {
requestPermission();
}
bottomLayout = findViewById(R.id.layout_bottom_content); bottomLayout = findViewById(R.id.layout_bottom_content);
requestPermissions();
} }
@Override private void requestPermissions() {
public void onRequestPermissionsResult(final int requestCode, final String[] permissions, if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
final int[] grantResults) { isAllGranted = checkPermissionAllGranted(PERMISSIONS);
super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (!isAllGranted) {
if (requestCode == PERMISSIONS_REQUEST) { ActivityCompat.requestPermissions(this, PERMISSIONS, REQUEST_PERMISSION);
if (allPermissionsGranted(grantResults)) {
setFragment();
} else { } else {
requestPermission(); addCameraFragment();
} }
} else {
isAllGranted = true;
addCameraFragment();
} }
} }
private static boolean allPermissionsGranted(final int[] grantResults) {
for (int result : grantResults) { private boolean checkPermissionAllGranted(String[] permissions) {
if (result != PackageManager.PERMISSION_GRANTED) { for (String permission : permissions) {
if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
return false; return false;
} }
} }
return true; return true;
} }
private boolean hasPermission() { /**
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { * Authority application result callback
return checkSelfPermission(PERMISSION_CAMERA) == PackageManager.PERMISSION_GRANTED; */
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (REQUEST_PERMISSION == requestCode) {
isAllGranted = true;
for (int grant : grantResults) {
if (grant != PackageManager.PERMISSION_GRANTED) {
isAllGranted = false;
break;
}
}
if (!isAllGranted) {
openAppDetails();
} else { } else {
return true; addCameraFragment();
}
} }
} }
private void requestPermission() { private void openAppDetails() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { AlertDialog.Builder builder = new AlertDialog.Builder(this);
if (shouldShowRequestPermissionRationale(PERMISSION_CAMERA)) { builder.setMessage("PoseNet 需要访问 “相机” 和 “外部存储器”,请到 “应用信息 -> 权限” 中授予!");
Toast.makeText(CameraActivity.this, "Camera permission is required for this demo", Toast.LENGTH_LONG) builder.setPositiveButton("去手动授权", new DialogInterface.OnClickListener() {
.show(); @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);
startActivityForResult(intent, REQUEST_PERMISSION_AGAIN);
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
builder.show();
} }
requestPermissions(new String[]{PERMISSION_CAMERA}, PERMISSIONS_REQUEST);
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (REQUEST_PERMISSION_AGAIN == requestCode) {
requestPermissions();
} }
} }
protected void setFragment() { protected void addCameraFragment() {
CameraFragment cameraFragment = CameraFragment.newInstance(new CameraFragment.RecognitionDataCallBack() { CameraFragment cameraFragment = CameraFragment.newInstance(new CameraFragment.RecognitionDataCallBack() {
@Override @Override
public void onRecognitionDataCallBack(String result, final String time) { public void onRecognitionDataCallBack(String result, final String time) {
@ -136,8 +179,6 @@ public class CameraActivity extends AppCompatActivity{
.commitAllowingStateLoss(); .commitAllowingStateLoss();
} }
private void dealRecognitionData(String result, final String time) { private void dealRecognitionData(String result, final String time) {
if (recognitionObjectBeanList != null) { if (recognitionObjectBeanList != null) {
recognitionObjectBeanList.clear(); recognitionObjectBeanList.clear();

@ -14,7 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
package com.mindspore.himindsporedemo.widget; package com.mindspore.classification.widget;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.app.Activity; import android.app.Activity;
@ -49,16 +49,15 @@ import android.view.Surface;
import android.view.TextureView; import android.view.TextureView;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.Toast; import android.widget.Toast;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import com.mindspore.himindsporedemo.R; import com.mindspore.classification.R;
import com.mindspore.himindsporedemo.gallery.classify.CompareSizesByArea; import com.mindspore.classification.gallery.classify.CompareSizesByArea;
import com.mindspore.himindsporedemo.gallery.classify.TrackingMobile; import com.mindspore.classification.gallery.classify.TrackingMobile;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;

@ -14,7 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
package com.mindspore.himindsporedemo.widget; package com.mindspore.classification.widget;
import android.content.Context; import android.content.Context;
import android.util.AttributeSet; import android.util.AttributeSet;
@ -25,7 +25,7 @@ import android.widget.TextView;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import com.mindspore.himindsporedemo.R; import com.mindspore.classification.R;
public class HorTextView extends LinearLayout { public class HorTextView extends LinearLayout {
private TextView tvLeftTitle, tvRightContent; private TextView tvLeftTitle, tvRightContent;

@ -17,7 +17,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.himindsporedemo.widget.CameraActivity" /> tools:context="com.mindspore.classification.widget.CameraActivity" />
<androidx.appcompat.widget.Toolbar <androidx.appcompat.widget.Toolbar

@ -5,7 +5,7 @@
android:background="@color/white" android:background="@color/white"
android:orientation="vertical"> android:orientation="vertical">
<com.mindspore.himindsporedemo.widget.AutoFitTextureView <com.mindspore.classification.widget.AutoFitTextureView
android:id="@+id/texture" android:id="@+id/texture"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" /> android:layout_height="match_parent" />

@ -14,7 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
package com.mindspore.himindsporedemo; package com.mindspore.classification;
import org.junit.Test; import org.junit.Test;
Loading…
Cancel
Save