fix image classification premissions

This commit is contained in:
hukang 2020-12-14 16:13:59 +08:00
parent 740ea71fc1
commit ac9c48afc0
16 changed files with 107 additions and 69 deletions

View File

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

View File

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

View File

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

View File

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

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mindspore.himindsporedemo"
package="com.mindspore.classification"
android:versionCode="1"
android:versionName="1.0">
@ -20,7 +20,7 @@
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".widget.CameraActivity"
android:name="com.mindspore.classification.widget.CameraActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

View File

@ -343,7 +343,7 @@ bool PreProcessImageData(const LiteMat &lite_mat_bgr, LiteMat *lite_norm_mat_ptr
*/
extern "C"
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 model_buffer,
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.
*/
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,
jobject srcBitmap) {
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
Java_com_mindspore_himindsporedemo_gallery_classify_TrackingMobile_unloadModel(JNIEnv *env,
Java_com_mindspore_classification_gallery_classify_TrackingMobile_unloadModel(JNIEnv *env,
jclass type,
jlong netEnv) {
MS_PRINT("MindSpore release net.");

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -17,7 +17,7 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"
tools:context="com.mindspore.himindsporedemo.widget.CameraActivity" />
tools:context="com.mindspore.classification.widget.CameraActivity" />
<androidx.appcompat.widget.Toolbar

View File

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

View File

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