├── library ├── .gitignore ├── src │ ├── main │ │ ├── AndroidManifest.xml │ │ └── java │ │ │ └── com │ │ │ └── rohitarya │ │ │ └── fresco │ │ │ └── facedetection │ │ │ └── processor │ │ │ ├── core │ │ │ └── FrescoFaceDetector.java │ │ │ └── FaceCenterCrop.java │ └── androidTest │ │ └── java │ │ └── com │ │ └── rohitarya │ │ └── fresco │ │ └── facedetection │ │ └── processor │ │ └── ApplicationTest.java ├── proguard-rules.pro └── build.gradle ├── settings.gradle ├── images ├── original_image1.jpg ├── original_image2.jpg ├── original_image3.jpg ├── original_image4.jpg ├── result_image1.jpg ├── result_image2.jpg ├── result_image3.jpg └── result_image4.jpg ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradle.properties ├── CHANGELOG.md ├── .gitignore ├── gradlew.bat ├── README.md └── gradlew /library/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':library' 2 | -------------------------------------------------------------------------------- /images/original_image1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aryarohit07/FrescoFaceDetectionProcessor/HEAD/images/original_image1.jpg -------------------------------------------------------------------------------- /images/original_image2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aryarohit07/FrescoFaceDetectionProcessor/HEAD/images/original_image2.jpg -------------------------------------------------------------------------------- /images/original_image3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aryarohit07/FrescoFaceDetectionProcessor/HEAD/images/original_image3.jpg -------------------------------------------------------------------------------- /images/original_image4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aryarohit07/FrescoFaceDetectionProcessor/HEAD/images/original_image4.jpg -------------------------------------------------------------------------------- /images/result_image1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aryarohit07/FrescoFaceDetectionProcessor/HEAD/images/result_image1.jpg -------------------------------------------------------------------------------- /images/result_image2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aryarohit07/FrescoFaceDetectionProcessor/HEAD/images/result_image2.jpg -------------------------------------------------------------------------------- /images/result_image3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aryarohit07/FrescoFaceDetectionProcessor/HEAD/images/result_image3.jpg -------------------------------------------------------------------------------- /images/result_image4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aryarohit07/FrescoFaceDetectionProcessor/HEAD/images/result_image4.jpg -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aryarohit07/FrescoFaceDetectionProcessor/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | COMPILE_SDK_VERSION=23 4 | BUILD_TOOLS_VERSION=23.0.2 5 | TARGET_SDK_VERSION=23 6 | MIN_SDK_VERSION=11 7 | 8 | VERSION_CODE = 2 9 | 10 | VERSION_NAME=0.1.1 11 | 12 | GOOGLE_PLAY_VERSION=9.4.0 13 | FRESCO_VERSION=0.12.0 -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Mon Dec 28 10:00:20 PST 2015 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip 7 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | Version 0.1.1 *(16-08-2016)* 2 | ---------------------------- 3 | 4 | Detector has now application context. 5 | 6 | 7 | Version 0.0.1 *(15-08-2016)* 8 | ---------------------------- 9 | 10 | Initial release. 11 | 12 | Library dependencies: 13 | ----- 14 | ```java 15 | com.google.android.gms:play-services-vision:9.4.0 16 | com.facebook.fresco:fresco:0.12.0 17 | ``` 18 | -------------------------------------------------------------------------------- /library/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 7 | 8 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | 10 | .DS_Store 11 | Thumbs.db 12 | 13 | # gradle files 14 | .gradle 15 | 16 | # Intellij project files 17 | .idea 18 | *.iml 19 | 20 | # generated files 21 | bin/ 22 | gen/ 23 | obj/ 24 | apk/ 25 | target/ 26 | build/ 27 | 28 | # Local configuration file (sdk path, etc) 29 | local.properties 30 | 31 | # Proguard folder generated by Eclipse 32 | proguard/ 33 | -------------------------------------------------------------------------------- /library/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /home/rohit/Android/Sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /library/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | 3 | ext { 4 | PUBLISH_GROUP_ID = 'com.github.aryarohit07' 5 | PUBLISH_ARTIFACT_ID = 'fresco-facedetection-processor' 6 | PUBLISH_VERSION = VERSION_NAME 7 | } 8 | 9 | android { 10 | compileSdkVersion COMPILE_SDK_VERSION as int 11 | buildToolsVersion BUILD_TOOLS_VERSION 12 | 13 | defaultConfig { 14 | minSdkVersion MIN_SDK_VERSION as int 15 | targetSdkVersion TARGET_SDK_VERSION as int 16 | versionCode VERSION_CODE as int 17 | versionName VERSION_NAME 18 | } 19 | buildTypes { 20 | release { 21 | minifyEnabled false 22 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 23 | } 24 | } 25 | } 26 | 27 | dependencies { 28 | compile "com.google.android.gms:play-services-vision:${GOOGLE_PLAY_VERSION}" 29 | compile "com.facebook.fresco:fresco:${FRESCO_VERSION}" 30 | } 31 | 32 | apply from: 'https://raw.githubusercontent.com/blundell/release-android-library/master/android-release-aar.gradle' -------------------------------------------------------------------------------- /library/src/androidTest/java/com/rohitarya/fresco/facedetection/processor/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Rohit Arya 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.rohitarya.fresco.facedetection.processor; 18 | 19 | import android.app.Application; 20 | import android.test.ApplicationTestCase; 21 | 22 | /** 23 | * Testing Fundamentals 24 | */ 25 | public class ApplicationTest extends ApplicationTestCase { 26 | public ApplicationTest() { 27 | super(Application.class); 28 | } 29 | } -------------------------------------------------------------------------------- /library/src/main/java/com/rohitarya/fresco/facedetection/processor/core/FrescoFaceDetector.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Rohit Arya 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.rohitarya.fresco.facedetection.processor.core; 18 | 19 | import android.content.Context; 20 | 21 | import com.google.android.gms.vision.face.FaceDetector; 22 | 23 | /** 24 | * Created by Rohit Arya (http://rohitarya.com) on 2/8/16. 25 | */ 26 | public class FrescoFaceDetector { 27 | 28 | private static volatile FaceDetector faceDetector; 29 | private static Context mContext; 30 | 31 | public static Context getContext() { 32 | if(mContext == null) { 33 | throw new RuntimeException("Initialize FrescoFaceDetector by calling FrescoFaceDetector.initialize(context)."); 34 | } 35 | return mContext; 36 | } 37 | 38 | public static void initialize(Context context) { 39 | if (context == null) { 40 | throw new IllegalArgumentException("Context must not be null."); 41 | } 42 | mContext = context.getApplicationContext(); // To make it independent of activity lifecycle 43 | } 44 | 45 | private static void initDetector() { 46 | if(faceDetector == null) { 47 | synchronized ((FrescoFaceDetector.class)) { 48 | if(faceDetector == null) { 49 | faceDetector = new 50 | FaceDetector.Builder(getContext()) 51 | .setTrackingEnabled(false) 52 | .build(); 53 | } 54 | } 55 | } 56 | } 57 | 58 | public static FaceDetector getFaceDetector() { 59 | if(mContext==null) { 60 | throw new RuntimeException("Initialize FrescoFaceDetector by calling FrescoFaceDetector.initialize(context)."); 61 | } 62 | initDetector(); 63 | return faceDetector; 64 | } 65 | 66 | public static void releaseDetector() { 67 | if(faceDetector!=null) { 68 | faceDetector.release(); 69 | faceDetector = null; 70 | } 71 | mContext = null; 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # Fresco face detection processor 3 | 4 | [ ![Download](https://api.bintray.com/packages/aryarohit07/android/fresco-facedetection-processor/images/download.svg) ](https://bintray.com/aryarohit07/android/fresco-facedetection-processor/_latestVersion) 5 | 6 | ### An Android image processor library providing cropping above Face Detection (Face Centering) for [Fresco](https://github.com/facebook/fresco) 7 | 8 | Are you using **Picasso**? [PicassoFaceDetectionTransformation](https://github.com/aryarohit07/PicassoFaceDetectionTransformation). 9 | 10 | 11 | Are you using **Glide**? [GlideFaceDetectionTransformation](https://github.com/aryarohit07/GlideFaceDetectionTransformation). 12 | 13 | Results 14 | ------ 15 | 16 | **Original Image** 17 | 18 | ![original image 1](/images/original_image1.jpg?raw=true ) 19 | 20 | **Results after cropping** 21 | 22 | ![resulting image 1](/images/result_image1.jpg?raw=true) 23 | 24 | 25 | **Original Image** 26 | 27 | ![original image 2](/images/original_image2.jpg?raw=true ) 28 | 29 | **Results after cropping** 30 | 31 | ![resulting image 2](/images/result_image2.jpg?raw=true) 32 | 33 | 34 | **Original Image** 35 | 36 | ![original image 3](/images/original_image3.jpg?raw=true ) 37 | 38 | **Results after cropping** 39 | 40 | ![resulting image 3](/images/result_image3.jpg?raw=true) 41 | 42 | **Original Image** 43 | 44 | ![original image 4](/images/original_image4.jpg?raw=true ) 45 | 46 | **Results after cropping** 47 | 48 | ![resulting image 4](/images/result_image4.jpg?raw=true) 49 | 50 | You can read more on [my Medium article](https://medium.freecodecamp.com/face-centering-android-library-build-on-top-of-google-vision-api-f88661b97959). 51 | 52 | 53 | ### How to use it? 54 | 55 | STEP 1: 56 | 57 | Grab via Gradle 58 | 59 | ``` 60 | repositories { 61 | jcenter() 62 | } 63 | dependencies { 64 | compile 'com.github.aryarohit07:fresco-facedetection-processor:0.1.1' 65 | } 66 | ``` 67 | 68 | Or via Maven 69 | 70 | ``` 71 | 72 | com.github.aryarohit07 73 | fresco-facedetection-processor 74 | 0.1.1 75 | 76 | ``` 77 | 78 | STEP 2: 79 | 80 | Initialize the detector (May be in `onCreate()` method) 81 | 82 | ```java 83 | FrescoFaceDetector.initialize(context); 84 | ``` 85 | 86 | STEP 3: 87 | 88 | Set fresco processor 89 | 90 | 91 | ```java 92 | ImageRequest request = ImageRequestBuilder 93 | .newBuilderWithSource(uri) 94 | .setPostprocessor(new FaceCenterCrop(900, 900)) 95 | .build(); 96 | 97 | PipelineDraweeController controller = (PipelineDraweeController) 98 | Fresco.newDraweeControllerBuilder() 99 | .setImageRequest(request) 100 | .setOldController(image2.getController()) 101 | // other setters as you need 102 | .build(); 103 | mSimpleDraweeView.setController(controller); 104 | ``` 105 | 106 | STEP 4: 107 | 108 | The face detector uses native resources in order to do detection. For this reason, it is necessary to release the detector instance once it is no longer needed (May be in `onDestory()` method) 109 | 110 | ```java 111 | FrescoFaceDetector.releaseDetector(); 112 | ``` 113 | 114 | **Note:** If no face is detected, it will fallback to CENTER CROP. 115 | 116 | Library dependencies: 117 | ------ 118 | ```java 119 | com.google.android.gms:play-services-vision:9.4.0 120 | com.facebook.fresco:fresco:0.12.0 121 | ``` 122 | 123 | **If you liked it, please Star it.** 124 | 125 | 126 | TODO 127 | ---- 128 | * Making it generic for any point. 129 | 130 | **Performance:** 131 | Time taken to detect faces in the original image. 132 | 133 | 134 | | width | height | time taken(ms) | 135 | |-------|--------|----------------| 136 | | 640 | 360 | 60-150 | 137 | | 900 | 600 | 100-200 | 138 | | 1280 | 720 | 250-350 | 139 | | 1920 | 1080 | 350-400 | 140 | | 2048 | 1536 | 500-550 | 141 | 142 | License 143 | ------- 144 | 145 | Copyright 2016 Rohit Arya 146 | 147 | Licensed under the Apache License, Version 2.0 (the "License"); 148 | you may not use this file except in compliance with the License. 149 | You may obtain a copy of the License at 150 | 151 | http://www.apache.org/licenses/LICENSE-2.0 152 | 153 | Unless required by applicable law or agreed to in writing, software 154 | distributed under the License is distributed on an "AS IS" BASIS, 155 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 156 | See the License for the specific language governing permissions and 157 | limitations under the License. 158 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # Attempt to set APP_HOME 46 | # Resolve links: $0 may be a link 47 | PRG="$0" 48 | # Need this for relative symlinks. 49 | while [ -h "$PRG" ] ; do 50 | ls=`ls -ld "$PRG"` 51 | link=`expr "$ls" : '.*-> \(.*\)$'` 52 | if expr "$link" : '/.*' > /dev/null; then 53 | PRG="$link" 54 | else 55 | PRG=`dirname "$PRG"`"/$link" 56 | fi 57 | done 58 | SAVED="`pwd`" 59 | cd "`dirname \"$PRG\"`/" >/dev/null 60 | APP_HOME="`pwd -P`" 61 | cd "$SAVED" >/dev/null 62 | 63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 64 | 65 | # Determine the Java command to use to start the JVM. 66 | if [ -n "$JAVA_HOME" ] ; then 67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 68 | # IBM's JDK on AIX uses strange locations for the executables 69 | JAVACMD="$JAVA_HOME/jre/sh/java" 70 | else 71 | JAVACMD="$JAVA_HOME/bin/java" 72 | fi 73 | if [ ! -x "$JAVACMD" ] ; then 74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 75 | 76 | Please set the JAVA_HOME variable in your environment to match the 77 | location of your Java installation." 78 | fi 79 | else 80 | JAVACMD="java" 81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 82 | 83 | Please set the JAVA_HOME variable in your environment to match the 84 | location of your Java installation." 85 | fi 86 | 87 | # Increase the maximum file descriptors if we can. 88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 89 | MAX_FD_LIMIT=`ulimit -H -n` 90 | if [ $? -eq 0 ] ; then 91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 92 | MAX_FD="$MAX_FD_LIMIT" 93 | fi 94 | ulimit -n $MAX_FD 95 | if [ $? -ne 0 ] ; then 96 | warn "Could not set maximum file descriptor limit: $MAX_FD" 97 | fi 98 | else 99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 100 | fi 101 | fi 102 | 103 | # For Darwin, add options to specify how the application appears in the dock 104 | if $darwin; then 105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 106 | fi 107 | 108 | # For Cygwin, switch paths to Windows format before running java 109 | if $cygwin ; then 110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 112 | JAVACMD=`cygpath --unix "$JAVACMD"` 113 | 114 | # We build the pattern for arguments to be converted via cygpath 115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 116 | SEP="" 117 | for dir in $ROOTDIRSRAW ; do 118 | ROOTDIRS="$ROOTDIRS$SEP$dir" 119 | SEP="|" 120 | done 121 | OURCYGPATTERN="(^($ROOTDIRS))" 122 | # Add a user-defined pattern to the cygpath arguments 123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 125 | fi 126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 127 | i=0 128 | for arg in "$@" ; do 129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 131 | 132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 134 | else 135 | eval `echo args$i`="\"$arg\"" 136 | fi 137 | i=$((i+1)) 138 | done 139 | case $i in 140 | (0) set -- ;; 141 | (1) set -- "$args0" ;; 142 | (2) set -- "$args0" "$args1" ;; 143 | (3) set -- "$args0" "$args1" "$args2" ;; 144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 150 | esac 151 | fi 152 | 153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 154 | function splitJvmOpts() { 155 | JVM_OPTS=("$@") 156 | } 157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 159 | 160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 161 | -------------------------------------------------------------------------------- /library/src/main/java/com/rohitarya/fresco/facedetection/processor/FaceCenterCrop.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Rohit Arya 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.rohitarya.fresco.facedetection.processor; 18 | 19 | import android.content.res.Resources; 20 | import android.graphics.Bitmap; 21 | import android.graphics.Canvas; 22 | import android.graphics.PointF; 23 | import android.graphics.RectF; 24 | import android.util.SparseArray; 25 | 26 | import com.facebook.common.references.CloseableReference; 27 | import com.facebook.imagepipeline.bitmaps.PlatformBitmapFactory; 28 | import com.facebook.imagepipeline.request.BasePostprocessor; 29 | import com.google.android.gms.vision.Frame; 30 | import com.google.android.gms.vision.face.Face; 31 | import com.google.android.gms.vision.face.FaceDetector; 32 | import com.rohitarya.fresco.facedetection.processor.core.FrescoFaceDetector; 33 | 34 | /** 35 | * Created by Rohit Arya (http://rohitarya.com) on 2/8/16. 36 | */ 37 | public class FaceCenterCrop extends BasePostprocessor { 38 | 39 | protected int width, height; 40 | 41 | public static final int PIXEL = 0; 42 | public static final int DP = 1; 43 | 44 | public FaceCenterCrop(int width, int height) { 45 | this.width = width; 46 | this.height = height; 47 | } 48 | 49 | public FaceCenterCrop(int width, int height, int unit) { 50 | if (unit == PIXEL) { 51 | this.width = width; 52 | this.height = height; 53 | } else if (unit == DP) { 54 | Resources resources = FrescoFaceDetector.getContext().getResources(); 55 | this.width = resources.getDimensionPixelSize(width); 56 | this.height = resources.getDimensionPixelSize(height); 57 | } else { 58 | throw new IllegalArgumentException("unit should either be FaceCenterCrop.PIXEL, FaceCenterCrop.DP"); 59 | } 60 | } 61 | 62 | @Override 63 | public CloseableReference process(Bitmap sourceBitmap, PlatformBitmapFactory bitmapFactory) { 64 | 65 | if (width == 0 || height == 0) { 66 | throw new IllegalArgumentException("width or height should not be zero!"); 67 | } 68 | 69 | float scaleX = (float) width / sourceBitmap.getWidth(); 70 | float scaleY = (float) height / sourceBitmap.getHeight(); 71 | 72 | if (scaleX != scaleY) { 73 | 74 | Bitmap.Config config = 75 | sourceBitmap.getConfig() != null ? sourceBitmap.getConfig() : Bitmap.Config.ARGB_8888; 76 | CloseableReference bitmapRef = bitmapFactory.createBitmap(width, height, config); 77 | 78 | try { 79 | Bitmap destBitmap = bitmapRef.get(); 80 | 81 | float scale = Math.max(scaleX, scaleY); 82 | 83 | float left = 0f; 84 | float top = 0f; 85 | 86 | float scaledWidth = width, scaledHeight = height; 87 | 88 | PointF focusPoint = new PointF(); 89 | 90 | detectFace(sourceBitmap, focusPoint); 91 | 92 | if (scaleX < scaleY) { 93 | 94 | scaledWidth = scale * sourceBitmap.getWidth(); 95 | 96 | float faceCenterX = scale * focusPoint.x; 97 | left = getLeftPoint(width, scaledWidth, faceCenterX); 98 | 99 | } else { 100 | 101 | scaledHeight = scale * sourceBitmap.getHeight(); 102 | 103 | float faceCenterY = scale * focusPoint.y; 104 | top = getTopPoint(height, scaledHeight, faceCenterY); 105 | } 106 | 107 | RectF targetRect = new RectF(left, top, left + scaledWidth, top + scaledHeight); 108 | Canvas canvas = new Canvas(destBitmap); 109 | canvas.drawBitmap(sourceBitmap, null, targetRect, null); 110 | 111 | return CloseableReference.cloneOrNull(bitmapRef); 112 | } catch (Exception e) { 113 | e.printStackTrace(); 114 | return super.process(sourceBitmap, bitmapFactory); 115 | } finally { 116 | CloseableReference.closeSafely(bitmapRef); 117 | } 118 | } else { 119 | return super.process(sourceBitmap, bitmapFactory); 120 | } 121 | } 122 | 123 | /** 124 | * Calculates a point (focus point) in the bitmap, around which cropping needs to be performed. 125 | * 126 | * @param bitmap Bitmap in which faces are to be detected. 127 | * @param centerOfAllFaces To store the center point. 128 | */ 129 | private void detectFace(Bitmap bitmap, PointF centerOfAllFaces) { 130 | FaceDetector faceDetector = FrescoFaceDetector.getFaceDetector(); 131 | if (!faceDetector.isOperational()) { 132 | centerOfAllFaces.set(bitmap.getWidth() / 2, bitmap.getHeight() / 2); // center crop 133 | return; 134 | } 135 | Frame frame = new Frame.Builder().setBitmap(bitmap).build(); 136 | SparseArray faces = faceDetector.detect(frame); 137 | final int totalFaces = faces.size(); 138 | if (totalFaces > 0) { 139 | float sumX = 0f; 140 | float sumY = 0f; 141 | for (int i = 0; i < totalFaces; i++) { 142 | PointF faceCenter = new PointF(); 143 | getFaceCenter(faces.get(faces.keyAt(i)), faceCenter); 144 | sumX = sumX + faceCenter.x; 145 | sumY = sumY + faceCenter.y; 146 | } 147 | centerOfAllFaces.set(sumX / totalFaces, sumY / totalFaces); 148 | return; 149 | } 150 | centerOfAllFaces.set(bitmap.getWidth() / 2, bitmap.getHeight() / 2); // center crop 151 | } 152 | 153 | /** 154 | * Calculates center of a given face 155 | * 156 | * @param face Face 157 | * @param center Center of the face 158 | */ 159 | private void getFaceCenter(Face face, PointF center) { 160 | float x = face.getPosition().x; 161 | float y = face.getPosition().y; 162 | float width = face.getWidth(); 163 | float height = face.getHeight(); 164 | center.set(x + (width / 2), y + (height / 2)); // face center in original bitmap 165 | } 166 | 167 | private float getTopPoint(int height, float scaledHeight, float faceCenterY) { 168 | if (faceCenterY <= height / 2) { // Face is near the top edge 169 | return 0f; 170 | } else if ((scaledHeight - faceCenterY) <= height / 2) { // face is near bottom edge 171 | return height - scaledHeight; 172 | } else { 173 | return (height / 2) - faceCenterY; 174 | } 175 | } 176 | 177 | private float getLeftPoint(int width, float scaledWidth, float faceCenterX) { 178 | if (faceCenterX <= width / 2) { // face is near the left edge. 179 | return 0f; 180 | } else if ((scaledWidth - faceCenterX) <= width / 2) { // face is near right edge 181 | return (width - scaledWidth); 182 | } else { 183 | return (width / 2) - faceCenterX; 184 | } 185 | } 186 | } 187 | --------------------------------------------------------------------------------