Created
March 16, 2017 21:52
-
-
Save sreejithraman/995e80ce08bccebbee2a87a486a99043 to your computer and use it in GitHub Desktop.
The Missing Documentation: Using Face Detector with Files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int rotateAngle; | |
void rotatePhoto(){ | |
rotateAngle += 90f; | |
if (rotateAngle == 360f) { | |
rotateAngle = 0; | |
} | |
photoImageView.setRotation(rotateAngle); // just rotate the image view for the user | |
} | |
int detectFaces(File photoFile){ | |
BitmapFactory.Options options=new BitmapFactory.Options(); | |
options.inPreferredConfig = Bitmap.Config.RGB_565; // we need to set it to this otherwise FaceDetector won't work | |
Bitmap photoBitmap = BitmapFactory.decodeFile(photoFile.getPath(), options); | |
FaceDetector faceDetector = new FaceDetector(photoBitmap.getWidth(), photoBitmap.getHeight(), 1); | |
FaceDetector.Face[] faces = new FaceDetector.Face[1];//array to pass into face detector...faces will be stored in it (creepy, i know) | |
return faceDetector.findFaces(photoBitmap, faces); | |
} | |
File rotatePhoto(File photoFile){ | |
File rotatedPhotoFile = ...; // just create a temp file however you want | |
Bitmap photoBitmap = BitmapFactory.decodeFile(photoFile.getPath()); // we don't need to set it to RGB_565 for rotation | |
Matrix matrix = new Matrix(); | |
matrix.postRotate(rotateAngle); | |
photoBitmap = Bitmap.createBitmap(photoBitmap, 0, 0, photoBitmap.getWidth(), photoBitmap.getHeight(), matrix, true); | |
try { | |
tempPhotoFile.createNewFile(); //saves the file in the cache dir | |
FileOutputStream fos = new FileOutputStream(tempPhotoFile); | |
photoBitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos);//jpeg format | |
fos.close(); | |
photoFile = tempPhotoFile; | |
} catch (Exception e) { | |
// do something... | |
} | |
} | |
void uploadPhoto(File photoFile){ | |
// we need to detect faces before we upload, FaceDetector won't work on photos in which faces aren't upright | |
int faces = detectFaces(photoFile); | |
File uploadFile = rotatePhoto(photoFile); | |
uploadPhotoToServer(uploadFile, faces); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment