|
|
|
@ -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);
|
|
|
|
|
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);
|
|
|
|
|