54
54
* overlay graphics to indicate the position, size, and ID of each face.
55
55
*/
56
56
public final class FaceTrackerActivity extends AppCompatActivity {
57
- private static final String TAG = "FaceTracker " ;
57
+ private static final String TAG = "FR " ;
58
58
59
59
private CameraSource mCameraSource = null ;
60
60
61
61
private CameraSourcePreview mPreview ;
62
62
private GraphicOverlay mGraphicOverlay ;
63
- // private Button mBtnDetect;
63
+ // private Button mBtnDetect;
64
64
private CustomDetector customDetector ;
65
65
//private FaceDetector mPictureDetector;
66
66
67
67
private static final int RC_HANDLE_GMS = 9001 ;
68
68
// permission request codes need to be < 256
69
- private static final int RC_HANDLE_CAMERA_PERM = 2 ;
70
- private static final int REQUEST_WRITE_STORAGE = 112 ;
69
+ private static final int RC_HANDLE_CAMERA_AND_SDCARD_PERM = 7 ;
71
70
72
71
private FaceRecognizer mFaceRecognizer ;
73
72
@@ -88,26 +87,14 @@ public void onCreate(Bundle icicle) {
88
87
// mBtnDetect = (Button) findViewById(R.id.btnDetect);
89
88
mFaceRecognizer = new FaceRecognizer ();
90
89
91
- // Check for the sdcard write permission. If the
92
- // permission is not granted yet, request permission.
93
- int rc = ActivityCompat .checkSelfPermission (this , Manifest .permission .WRITE_EXTERNAL_STORAGE );
94
- if (rc == PackageManager .PERMISSION_GRANTED ) {
95
- //createCameraSource();
96
- Log .w (TAG , "SDCard write permission is granted." );
97
- } else {
98
- //requestCameraPermission();
99
- requestSdCardPermission ();
100
- Log .w (TAG , "SDCard write permission is NOT granted." );
101
- }
102
-
103
-
104
90
// Check for the camera permission before accessing the camera. If the
105
91
// permission is not granted yet, request permission.
106
- rc = ActivityCompat .checkSelfPermission (this , Manifest .permission .CAMERA );
107
- if (rc == PackageManager .PERMISSION_GRANTED ) {
92
+ int rc = ActivityCompat .checkSelfPermission (this , Manifest .permission .CAMERA );
93
+ int rs = ActivityCompat .checkSelfPermission (this , Manifest .permission .READ_EXTERNAL_STORAGE );
94
+ if (rc == PackageManager .PERMISSION_GRANTED && rs == PackageManager .PERMISSION_GRANTED ) {
108
95
createCameraSource ();
109
96
} else {
110
- requestCameraPermission ();
97
+ requestCameraAndSdCardPermission ();
111
98
}
112
99
}
113
100
@@ -116,41 +103,15 @@ public void onCreate(Bundle icicle) {
116
103
* showing a "Snackbar" message of why the permission is needed then
117
104
* sending the request.
118
105
*/
119
- private void requestCameraPermission () {
120
- Log .w (TAG , "Camera permission is not granted. Requesting permission" );
106
+ private void requestCameraAndSdCardPermission () {
107
+ Log .w (TAG , "Camera and sdcard permissions are not granted. Requesting permission" );
121
108
122
- final String [] permissions = new String []{Manifest .permission .CAMERA };
109
+ final String [] permissions = new String []{Manifest .permission .CAMERA ,
110
+ Manifest .permission .READ_EXTERNAL_STORAGE };
123
111
124
112
if (!ActivityCompat .shouldShowRequestPermissionRationale (this ,
125
113
Manifest .permission .CAMERA )) {
126
- ActivityCompat .requestPermissions (this , permissions , RC_HANDLE_CAMERA_PERM );
127
- return ;
128
- }
129
-
130
- final Activity thisActivity = this ;
131
-
132
- View .OnClickListener listener = new View .OnClickListener () {
133
- @ Override
134
- public void onClick (View view ) {
135
- ActivityCompat .requestPermissions (thisActivity , permissions ,
136
- RC_HANDLE_CAMERA_PERM );
137
- }
138
- };
139
-
140
- Snackbar .make (mGraphicOverlay , R .string .permission_camera_rationale ,
141
- Snackbar .LENGTH_INDEFINITE )
142
- .setAction (R .string .ok , listener )
143
- .show ();
144
- }
145
-
146
- private void requestSdCardPermission () {
147
- Log .w (TAG , "SdCard permission is not granted. Requesting permission" );
148
-
149
- final String [] permissions = new String []{Manifest .permission .WRITE_EXTERNAL_STORAGE };
150
-
151
- if (!ActivityCompat .shouldShowRequestPermissionRationale (this ,
152
- Manifest .permission .WRITE_EXTERNAL_STORAGE )) {
153
- ActivityCompat .requestPermissions (this , permissions , REQUEST_WRITE_STORAGE );
114
+ ActivityCompat .requestPermissions (this , permissions , RC_HANDLE_CAMERA_AND_SDCARD_PERM );
154
115
return ;
155
116
}
156
117
@@ -160,11 +121,11 @@ private void requestSdCardPermission() {
160
121
@ Override
161
122
public void onClick (View view ) {
162
123
ActivityCompat .requestPermissions (thisActivity , permissions ,
163
- REQUEST_WRITE_STORAGE );
124
+ RC_HANDLE_CAMERA_AND_SDCARD_PERM );
164
125
}
165
126
};
166
127
167
- Snackbar .make (mGraphicOverlay , R .string .permission_sdcard_rationale ,
128
+ Snackbar .make (mGraphicOverlay , R .string .permission_camera_and_sdcard_rationale ,
168
129
Snackbar .LENGTH_INDEFINITE )
169
130
.setAction (R .string .ok , listener )
170
131
.show ();
@@ -310,7 +271,7 @@ protected void onDestroy() {
310
271
*/
311
272
@ Override
312
273
public void onRequestPermissionsResult (int requestCode , String [] permissions , int [] grantResults ) {
313
- if (requestCode != RC_HANDLE_CAMERA_PERM ) {
274
+ if (requestCode != RC_HANDLE_CAMERA_AND_SDCARD_PERM ) {
314
275
Log .d (TAG , "Got unexpected permission result: " + requestCode );
315
276
super .onRequestPermissionsResult (requestCode , permissions , grantResults );
316
277
return ;
@@ -334,8 +295,8 @@ public void onClick(DialogInterface dialog, int id) {
334
295
};
335
296
336
297
AlertDialog .Builder builder = new AlertDialog .Builder (this );
337
- builder .setTitle ("Face Tracker sample " )
338
- .setMessage (R .string .no_camera_permission )
298
+ builder .setTitle ("FR demo " )
299
+ .setMessage (R .string .no_camera_sdcard_permission )
339
300
.setPositiveButton (R .string .ok , listener )
340
301
.show ();
341
302
}
@@ -435,8 +396,4 @@ public void onDone() {
435
396
mOverlay .remove (mFaceGraphic );
436
397
}
437
398
}
438
-
439
- //public native int loadResources();
440
- //public native String recognizeFromImage(Bitmap bmp);
441
- //public native int nativeAdd(int a, int b);
442
399
}
0 commit comments