diff --git a/app/src/main/java/com/example/android/emojify/Emojifier.java b/app/src/main/java/com/example/android/emojify/Emojifier.java new file mode 100644 index 00000000..e743f2d6 --- /dev/null +++ b/app/src/main/java/com/example/android/emojify/Emojifier.java @@ -0,0 +1,64 @@ +/* +* Copyright (C) 2017 The Android Open Source Project +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +package com.example.android.emojify; + +import android.content.Context; +import android.graphics.Bitmap; +import android.util.Log; +import android.util.SparseArray; +import android.widget.Toast; + +import com.google.android.gms.vision.Frame; +import com.google.android.gms.vision.face.Face; +import com.google.android.gms.vision.face.FaceDetector; + +class Emojifier { + + private static final String LOG_TAG = Emojifier.class.getSimpleName(); + + /** + * Method for detecting faces in a bitmap. + * + * @param context The application context. + * @param picture The picture in which to detect the faces. + */ + static void detectFaces(Context context, Bitmap picture) { + + // Create the face detector, disable tracking and enable classifications + FaceDetector detector = new FaceDetector.Builder(context) + .setTrackingEnabled(false) + .setClassificationType(FaceDetector.ALL_CLASSIFICATIONS) + .build(); + + // Build the frame + Frame frame = new Frame.Builder().setBitmap(picture).build(); + + // Detect the faces + SparseArray faces = detector.detect(frame); + + // Log the number of faces + Log.d(LOG_TAG, "detectFaces: number of faces = " + faces.size()); + + // If there are no faces detected, show a Toast message + if(faces.size() == 0){ + Toast.makeText(context, R.string.no_faces_message, Toast.LENGTH_SHORT).show(); + } + + // Release the detector + detector.release(); + } +} diff --git a/app/src/main/java/com/example/android/emojify/MainActivity.java b/app/src/main/java/com/example/android/emojify/MainActivity.java index 4f3179c3..413a498e 100644 --- a/app/src/main/java/com/example/android/emojify/MainActivity.java +++ b/app/src/main/java/com/example/android/emojify/MainActivity.java @@ -41,9 +41,6 @@ public class MainActivity extends AppCompatActivity { - // TODO (1): Create a Java class called Emojifier - // TODO (2): Create a static method in the Emojifier class called detectFaces() which detects and logs the number of faces in a given bitmap. - private static final int REQUEST_IMAGE_CAPTURE = 1; private static final int REQUEST_STORAGE_PERMISSION = 1; @@ -183,9 +180,10 @@ private void processAndSetImage() { // Resample the saved image to fit the ImageView mResultsBitmap = BitmapUtils.resamplePic(this, mTempPhotoPath); - - // TODO (3): Call the new detectFaces() method, passing in the resampled bitmap to detect the faces in the picture. - + + // Detect the faces + Emojifier.detectFaces(this, mResultsBitmap); + // Set the new bitmap to the ImageView mImageView.setImageBitmap(mResultsBitmap); } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 623c8520..334c966e 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -23,4 +23,5 @@ GO Permission denied The imageview that contains the emojified picture + No Faces Detected