|
|
|
@ -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,11 +57,13 @@ import java.util.List;
|
|
|
|
|
public class CameraActivity extends AppCompatActivity{
|
|
|
|
|
private static final String TAG = "CameraActivity";
|
|
|
|
|
|
|
|
|
|
private static final String BUNDLE_FRAGMENTS_KEY = "android:support:fragments";
|
|
|
|
|
|
|
|
|
|
private static final int PERMISSIONS_REQUEST = 1;
|
|
|
|
|
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 PERMISSION_CAMERA = Manifest.permission.CAMERA;
|
|
|
|
|
private static final String BUNDLE_FRAGMENTS_KEY = "android:support:fragments";
|
|
|
|
|
|
|
|
|
|
private LinearLayout bottomLayout;
|
|
|
|
|
|
|
|
|
@ -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;
|
|
|
|
|
/**
|
|
|
|
|
* 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 {
|
|
|
|
|
return true;
|
|
|
|
|
addCameraFragment();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
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() {
|
|
|
|
|
@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();
|