5454 * overlay graphics to indicate the position, size, and ID of each face.
5555 */
5656public final class FaceTrackerActivity extends AppCompatActivity {
57- private static final String TAG = "FaceTracker " ;
57+ private static final String TAG = "FR " ;
5858
5959 private CameraSource mCameraSource = null ;
6060
6161 private CameraSourcePreview mPreview ;
6262 private GraphicOverlay mGraphicOverlay ;
63- // private Button mBtnDetect;
63+ // private Button mBtnDetect;
6464 private CustomDetector customDetector ;
6565 //private FaceDetector mPictureDetector;
6666
6767 private static final int RC_HANDLE_GMS = 9001 ;
6868 // 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 ;
7170
7271 private FaceRecognizer mFaceRecognizer ;
7372
@@ -88,26 +87,14 @@ public void onCreate(Bundle icicle) {
8887// mBtnDetect = (Button) findViewById(R.id.btnDetect);
8988 mFaceRecognizer = new FaceRecognizer ();
9089
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-
10490 // Check for the camera permission before accessing the camera. If the
10591 // 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 ) {
10895 createCameraSource ();
10996 } else {
110- requestCameraPermission ();
97+ requestCameraAndSdCardPermission ();
11198 }
11299 }
113100
@@ -116,41 +103,15 @@ public void onCreate(Bundle icicle) {
116103 * showing a "Snackbar" message of why the permission is needed then
117104 * sending the request.
118105 */
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" );
121108
122- final String [] permissions = new String []{Manifest .permission .CAMERA };
109+ final String [] permissions = new String []{Manifest .permission .CAMERA ,
110+ Manifest .permission .READ_EXTERNAL_STORAGE };
123111
124112 if (!ActivityCompat .shouldShowRequestPermissionRationale (this ,
125113 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 );
154115 return ;
155116 }
156117
@@ -160,11 +121,11 @@ private void requestSdCardPermission() {
160121 @ Override
161122 public void onClick (View view ) {
162123 ActivityCompat .requestPermissions (thisActivity , permissions ,
163- REQUEST_WRITE_STORAGE );
124+ RC_HANDLE_CAMERA_AND_SDCARD_PERM );
164125 }
165126 };
166127
167- Snackbar .make (mGraphicOverlay , R .string .permission_sdcard_rationale ,
128+ Snackbar .make (mGraphicOverlay , R .string .permission_camera_and_sdcard_rationale ,
168129 Snackbar .LENGTH_INDEFINITE )
169130 .setAction (R .string .ok , listener )
170131 .show ();
@@ -310,7 +271,7 @@ protected void onDestroy() {
310271 */
311272 @ Override
312273 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 ) {
314275 Log .d (TAG , "Got unexpected permission result: " + requestCode );
315276 super .onRequestPermissionsResult (requestCode , permissions , grantResults );
316277 return ;
@@ -334,8 +295,8 @@ public void onClick(DialogInterface dialog, int id) {
334295 };
335296
336297 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 )
339300 .setPositiveButton (R .string .ok , listener )
340301 .show ();
341302 }
@@ -435,8 +396,4 @@ public void onDone() {
435396 mOverlay .remove (mFaceGraphic );
436397 }
437398 }
438-
439- //public native int loadResources();
440- //public native String recognizeFromImage(Bitmap bmp);
441- //public native int nativeAdd(int a, int b);
442399}
0 commit comments