!9903 [MS][LITE]20201208 posenet demo exits unexpectedly

From: @sishuikang
Reviewed-by: @zhanghaibo5,@zhang_xue_tong
Signed-off-by: @zhanghaibo5
This commit is contained in:
mindspore-ci-bot 2020-12-17 19:23:30 +08:00 committed by Gitee
commit f4c5f6fa94
3 changed files with 112 additions and 43 deletions

View File

@ -3,6 +3,16 @@
package="com.mindspore.posenetdemo">
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEM" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />

View File

@ -15,6 +15,10 @@
*/
package com.mindspore.posenetdemo;
import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
@ -24,10 +28,18 @@ import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.hardware.camera2.CameraCharacteristics;
import android.media.Image;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.view.SurfaceView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.core.util.Pair;
import java.nio.ByteBuffer;
@ -57,6 +69,11 @@ public class MainActivity extends AppCompatActivity implements CameraDataDealLis
new Pair(LEFT_HIP, LEFT_KNEE), new Pair(LEFT_KNEE, LEFT_ANKLE),
new Pair(RIGHT_HIP, RIGHT_KNEE), new Pair(RIGHT_KNEE, RIGHT_ANKLE));
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;
/**
* Model input shape for images.
@ -81,12 +98,91 @@ public class MainActivity extends AppCompatActivity implements CameraDataDealLis
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addCameraFragment();
requestPermissions();
}
private void requestPermissions() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
isAllGranted = checkPermissionAllGranted(PERMISSIONS);
if (!isAllGranted) {
ActivityCompat.requestPermissions(this, PERMISSIONS, REQUEST_PERMISSION);
} else {
addCameraFragment();
}
} else {
isAllGranted = true;
addCameraFragment();
}
}
private boolean checkPermissionAllGranted(String[] permissions) {
for (String permission : permissions) {
if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
return true;
}
/**
* Authority application result callback
*/
@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 {
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();
}
}
private void addCameraFragment() {
posenet = new Posenet(this);
poseNetFragment = PoseNetFragment.newInstance();
poseNetFragment.setCameraDataDealListener(this);
// poseNetFragment.setFacingCamera(lensFacing);

View File

@ -15,10 +15,9 @@
*/
package com.mindspore.posenetdemo;
import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.ImageFormat;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCaptureSession;
@ -40,11 +39,9 @@ import android.view.Surface;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.fragment.app.Fragment;
import java.util.Arrays;
@ -57,7 +54,8 @@ import java.util.concurrent.TimeUnit;
*/
public class PoseNetFragment extends Fragment {
private final static int REQUEST_CAMERA_PERMISSION = 1;
private static final String TAG = "PoseNetFragment";
private String cameraId = "1";
private SurfaceView surfaceView;
private CameraCaptureSession captureSession;
@ -75,9 +73,6 @@ public class PoseNetFragment extends Fragment {
private Semaphore cameraOpenCloseLock = new Semaphore(1);//使用信号量 Semaphore 进行多线程任务调度
private boolean flashSupported;
private static final String TAG = "PoseNetFragment";
private final CameraDevice.StateCallback mStateCallback = new CameraDevice.StateCallback() {
@Override
@ -105,7 +100,6 @@ public class PoseNetFragment extends Fragment {
}
};
private CameraCaptureSession.CaptureCallback captureCallback = new CameraCaptureSession.CaptureCallback() {
@Override
public void onCaptureProgressed(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, @NonNull CaptureResult partialResult) {
@ -164,35 +158,7 @@ public class PoseNetFragment extends Fragment {
super.onDestroy();
}
private void requestCameraPermission() {
if (shouldShowRequestPermissionRationale(Manifest.permission.CAMERA)) {
Toast.makeText(getContext(), "This app needs camera permission.", Toast.LENGTH_LONG).show();
} else {
requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_PHONE_STATE, Manifest.permission.CAMERA}, REQUEST_CAMERA_PERMISSION);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_CAMERA_PERMISSION) {
if (allPermissionsGranted(grantResults)) {
Toast.makeText(getContext(), "This app needs camera permission.", Toast.LENGTH_LONG).show();
}
} else {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
private boolean allPermissionsGranted(int[] grantResults) {
for (int grantResult : grantResults) {
if (grantResult == PackageManager.PERMISSION_DENIED) {
return false;
}
}
return true;
}
/**
* Sets up member variables related to camera.
@ -227,7 +193,6 @@ public class PoseNetFragment extends Fragment {
// We've found a viable camera and finished setting up member variables,
// so we don't need to iterate through other available cameras.
return;
}
} catch (CameraAccessException e) {
e.printStackTrace();
@ -239,10 +204,8 @@ public class PoseNetFragment extends Fragment {
/**
* Opens the camera specified by [PosenetActivity.cameraId].
*/
@SuppressLint("MissingPermission")
private void openCamera() {
if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
this.requestCameraPermission();
}
setUpCameraOutputs();
CameraManager manager = (CameraManager) getContext().getSystemService(Context.CAMERA_SERVICE);
try {