Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions app/src/main/java/com/example/android/emojify/Emojifier.java
Original file line number Diff line number Diff line change
@@ -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<Face> 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();
}
}
10 changes: 4 additions & 6 deletions app/src/main/java/com/example/android/emojify/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@
<string name="go">GO</string>
<string name="permission_denied">Permission denied</string>
<string name="imageview_description">The imageview that contains the emojified picture</string>
<string name="no_faces_message">No Faces Detected</string>
</resources>