├── app ├── .gitignore ├── src │ ├── main │ │ ├── res │ │ │ ├── drawable │ │ │ │ ├── op.png │ │ │ │ ├── cat2.png │ │ │ │ ├── dog.png │ │ │ │ ├── face.png │ │ │ │ ├── hair.png │ │ │ │ ├── hat.png │ │ │ │ ├── hat2.png │ │ │ │ ├── mask.png │ │ │ │ ├── snap.png │ │ │ │ ├── camera.png │ │ │ │ ├── flash.png │ │ │ │ ├── mask2.png │ │ │ │ ├── mask3.png │ │ │ │ ├── glasses2.png │ │ │ │ ├── glasses3.png │ │ │ │ ├── glasses4.png │ │ │ │ ├── glasses5.png │ │ │ │ ├── no_filter.png │ │ │ │ ├── face_select.png │ │ │ │ ├── flip_camera.png │ │ │ │ ├── time_clock.png │ │ │ │ ├── transparent.png │ │ │ │ ├── btn_camera_all.png │ │ │ │ ├── round_background.xml │ │ │ │ ├── round_background_select.xml │ │ │ │ └── ic_launcher_background.xml │ │ │ ├── mipmap-hdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-mdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xhdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xxhdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xxxhdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── values │ │ │ │ ├── colors.xml │ │ │ │ ├── strings.xml │ │ │ │ └── styles.xml │ │ │ ├── mipmap-anydpi-v26 │ │ │ │ ├── ic_launcher.xml │ │ │ │ └── ic_launcher_round.xml │ │ │ ├── drawable-v24 │ │ │ │ └── ic_launcher_foreground.xml │ │ │ └── layout │ │ │ │ └── activity_face_filter.xml │ │ ├── AndroidManifest.xml │ │ └── java │ │ │ └── com │ │ │ └── gsrathoreniks │ │ │ └── facefilter │ │ │ ├── TextGraphic.java │ │ │ ├── FaceGraphic.java │ │ │ ├── camera │ │ │ ├── CameraSourcePreview.java │ │ │ └── GraphicOverlay.java │ │ │ └── FaceFilterActivity.java │ ├── test │ │ └── java │ │ │ └── com │ │ │ └── gsrathoreniks │ │ │ └── facefilter │ │ │ └── ExampleUnitTest.java │ └── androidTest │ │ └── java │ │ └── com │ │ └── gsrathoreniks │ │ └── facefilter │ │ └── ExampleInstrumentedTest.java ├── proguard-rules.pro └── build.gradle ├── settings.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── screenshot └── android-face-filters.png ├── .gitignore ├── .github └── ISSUE_TEMPLATE │ ├── feature_request.md │ ├── support.md │ └── bug_report.md ├── gradle.properties ├── README.md ├── gradlew.bat ├── gradlew └── LICENSE /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app' 2 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/op.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gaetanozappi/Android-face-filters/HEAD/app/src/main/res/drawable/op.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/cat2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gaetanozappi/Android-face-filters/HEAD/app/src/main/res/drawable/cat2.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/dog.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gaetanozappi/Android-face-filters/HEAD/app/src/main/res/drawable/dog.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/face.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gaetanozappi/Android-face-filters/HEAD/app/src/main/res/drawable/face.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/hair.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gaetanozappi/Android-face-filters/HEAD/app/src/main/res/drawable/hair.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/hat.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gaetanozappi/Android-face-filters/HEAD/app/src/main/res/drawable/hat.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/hat2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gaetanozappi/Android-face-filters/HEAD/app/src/main/res/drawable/hat2.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/mask.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gaetanozappi/Android-face-filters/HEAD/app/src/main/res/drawable/mask.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/snap.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gaetanozappi/Android-face-filters/HEAD/app/src/main/res/drawable/snap.png -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gaetanozappi/Android-face-filters/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /app/src/main/res/drawable/camera.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gaetanozappi/Android-face-filters/HEAD/app/src/main/res/drawable/camera.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/flash.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gaetanozappi/Android-face-filters/HEAD/app/src/main/res/drawable/flash.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/mask2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gaetanozappi/Android-face-filters/HEAD/app/src/main/res/drawable/mask2.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/mask3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gaetanozappi/Android-face-filters/HEAD/app/src/main/res/drawable/mask3.png -------------------------------------------------------------------------------- /screenshot/android-face-filters.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gaetanozappi/Android-face-filters/HEAD/screenshot/android-face-filters.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/glasses2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gaetanozappi/Android-face-filters/HEAD/app/src/main/res/drawable/glasses2.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/glasses3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gaetanozappi/Android-face-filters/HEAD/app/src/main/res/drawable/glasses3.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/glasses4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gaetanozappi/Android-face-filters/HEAD/app/src/main/res/drawable/glasses4.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/glasses5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gaetanozappi/Android-face-filters/HEAD/app/src/main/res/drawable/glasses5.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/no_filter.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gaetanozappi/Android-face-filters/HEAD/app/src/main/res/drawable/no_filter.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/face_select.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gaetanozappi/Android-face-filters/HEAD/app/src/main/res/drawable/face_select.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/flip_camera.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gaetanozappi/Android-face-filters/HEAD/app/src/main/res/drawable/flip_camera.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/time_clock.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gaetanozappi/Android-face-filters/HEAD/app/src/main/res/drawable/time_clock.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/transparent.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gaetanozappi/Android-face-filters/HEAD/app/src/main/res/drawable/transparent.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/btn_camera_all.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gaetanozappi/Android-face-filters/HEAD/app/src/main/res/drawable/btn_camera_all.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gaetanozappi/Android-face-filters/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gaetanozappi/Android-face-filters/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gaetanozappi/Android-face-filters/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gaetanozappi/Android-face-filters/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gaetanozappi/Android-face-filters/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gaetanozappi/Android-face-filters/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gaetanozappi/Android-face-filters/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gaetanozappi/Android-face-filters/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gaetanozappi/Android-face-filters/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | .externalNativeBuild 10 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gaetanozappi/Android-face-filters/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Sun Dec 10 02:11:49 IST 2017 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-4.1-all.zip 7 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/round_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/round_background_select.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 10 | -------------------------------------------------------------------------------- /app/src/test/java/com/gsrathoreniks/facefilter/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.gsrathoreniks.facefilter; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * @see Testing documentation 11 | */ 12 | public class ExampleUnitTest { 13 | @Test 14 | public void addition_isCorrect() throws Exception { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | FaceFilter 3 | OK 4 | Access to the camera is needed for detection 5 | This application cannot run because it does not have the camera permission. The application will now exit. 6 | Face detector dependencies cannot be downloaded due to low device storage 7 | 8 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature request 3 | about: Suggest an idea for this project 4 | 5 | --- 6 | 7 | **Is your feature request related to a problem? Please describe.** 8 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] 9 | 10 | **Describe the solution you'd like** 11 | A clear and concise description of what you want to happen. 12 | 13 | **Describe alternatives you've considered** 14 | A clear and concise description of any alternative solutions or features you've considered. 15 | 16 | **Additional context** 17 | Add any other context or screenshots about the feature request here. 18 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 14 | 15 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | org.gradle.jvmargs=-Xmx1536m 13 | 14 | # When configured, Gradle will run in incubating parallel mode. 15 | # This option should only be used with decoupled projects. More details, visit 16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 17 | # org.gradle.parallel=true 18 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/support.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Support 3 | about: Request some help with using this package 4 | 5 | --- 6 | 7 | 8 | - [ ] Review the documentation: https://github.com/gaetanozappi/Android-face-filters 9 | - [ ] Search for existing issues (including closed ones): https://github.com/gaetanozappi/Android-face-filters/issues 10 | 11 | 12 | ## Environment 13 | 14 | 15 | ## Description 16 | Describe your issue in detail. Include screenshots if needed. 17 | 18 | ## Demo 19 | You can use https://snack.expo.io/ to create a demo that can help users to better understand your problem. 20 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/gsrathoreniks/facefilter/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.gsrathoreniks.facefilter; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumented test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.gsrathoreniks.facefilter", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | 5 | --- 6 | 7 | 8 | - [ ] Review the documentation: https://github.com/gaetanozappi/Android-face-filters 9 | - [ ] Search for existing issues (including closed issues): https://github.com/gaetanozappi/Android-face-filters/issues 10 | 11 | 12 | ## Environment 13 | 14 | 15 | ## Description 16 | Describe your issue in detail. Include screenshots if needed. 17 | 18 | 19 | ## Reproducible Demo 20 | Let us know how to reproduce the issue. Include a code sample, share a project. 21 | Please follow the guidelines for providing a MCVE: https://stackoverflow.com/help/mcve 22 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 26 5 | defaultConfig { 6 | applicationId "com.gsrathoreniks.facefilter" 7 | minSdkVersion 18 8 | targetSdkVersion 26 9 | versionCode 1 10 | versionName "1.0" 11 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 12 | } 13 | buildTypes { 14 | release { 15 | minifyEnabled false 16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 17 | } 18 | } 19 | } 20 | 21 | dependencies { 22 | implementation fileTree(dir: 'libs', include: ['*.jar']) 23 | implementation 'com.android.support:appcompat-v7:26.1.0' 24 | implementation 'com.android.support.constraint:constraint-layout:1.0.2' 25 | testImplementation 'junit:junit:4.12' 26 | 27 | compile 'com.android.support:design:26.1.0' 28 | compile 'com.google.android.gms:play-services-vision:9.4.0+' 29 | androidTestImplementation 'com.android.support.test:runner:1.0.1' 30 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1' 31 | } 32 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Android: Face Filters 2 | 3 | [![github home](https://img.shields.io/badge/gaetanozappi-android--face--filters-blue.svg?style=flat)](https://github.com/gaetanozappi/android-face-filters) 4 | 5 | [![github issues](https://img.shields.io/github/issues/gaetanozappi/Android-face-filters.svg?style=flat)](https://github.com/gaetanozappi/Android-face-filters/issues) 6 | [![github closed issues](https://img.shields.io/github/issues-closed/gaetanozappi/Android-face-filters.svg?style=flat&colorB=44cc11)](https://github.com/gaetanozappi/Android-face-filters/issues?q=is%3Aissue+is%3Aclosed) 7 | [![Issue Stats](https://img.shields.io/issuestats/i/github/gaetanozappi/Android-face-filters.svg?style=flat&colorB=44cc11)](http://github.com/gaetanozappi/Android-face-filters/issues) 8 | [![github license](https://img.shields.io/github/license/gaetanozappi/Android-face-filters.svg)]() 9 | 10 | ![PNG](screenshot/android-face-filters.png) 11 | 12 | - [License](#-license) 13 | 14 | Snapchat and Instagram like Face Filters. 15 | 16 | It is based on the following project: 17 | 18 | https://github.com/googlesamples/android-vision/tree/master/visionSamples/FaceTracker 19 | 20 | ## 📜 License 21 | This library is provided under the Apache License. 22 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 17 | 19 | 20 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /app/src/main/java/com/gsrathoreniks/facefilter/TextGraphic.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) The Android Open Source Project 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 | package com.gsrathoreniks.facefilter; 17 | 18 | import android.graphics.Bitmap; 19 | import android.graphics.Canvas; 20 | import android.graphics.Color; 21 | import android.graphics.Paint; 22 | 23 | import com.gsrathoreniks.facefilter.camera.GraphicOverlay; 24 | 25 | import java.text.DateFormat; 26 | import java.text.SimpleDateFormat; 27 | import java.util.Calendar; 28 | 29 | /** 30 | * Graphic instance for rendering face position, orientation, and landmarks within an associated 31 | * graphic overlay view. 32 | */ 33 | class TextGraphic extends GraphicOverlay.Graphic { 34 | 35 | TextGraphic(GraphicOverlay overlay) { 36 | super(overlay); 37 | } 38 | 39 | /** 40 | * Updates the face instance from the detection of the most recent frame. Invalidates the 41 | * relevant portions of the overlay to trigger a redraw. 42 | */ 43 | void updateText(int c) { 44 | postInvalidate(); 45 | } 46 | 47 | /** 48 | * Draws the face annotations for position on the supplied canvas. 49 | */ 50 | @Override 51 | public void draw(Canvas canvas) { 52 | DateFormat df = new SimpleDateFormat("HH:mm"); 53 | String date = df.format(Calendar.getInstance().getTime()); 54 | Bitmap cc = drawTextBitmap(date, Color.WHITE,100,150,false,500,500); 55 | canvas.drawBitmap(cc, 0, 10, new Paint()); 56 | } 57 | 58 | public static Bitmap drawTextBitmap(String string, int color, int alpha, int size, boolean underline, int width , int height) { 59 | Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 60 | Canvas canvas = new Canvas(result); 61 | //canvas.drawBitmap(); 62 | Paint paint = new Paint(); 63 | paint.setColor(color); 64 | paint.setAlpha(alpha); 65 | paint.setTextSize(size); 66 | paint.setAntiAlias(true); 67 | paint.setUnderlineText(underline); 68 | canvas.drawText(string, 100, 150, paint); 69 | return result; 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /app/src/main/java/com/gsrathoreniks/facefilter/FaceGraphic.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) The Android Open Source Project 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 | package com.gsrathoreniks.facefilter; 17 | 18 | import android.graphics.Bitmap; 19 | import android.graphics.BitmapFactory; 20 | import android.graphics.Canvas; 21 | import android.graphics.Paint; 22 | 23 | import com.google.android.gms.vision.face.Face; 24 | import com.gsrathoreniks.facefilter.camera.GraphicOverlay; 25 | 26 | /** 27 | * Graphic instance for rendering face position, orientation, and landmarks within an associated 28 | * graphic overlay view. 29 | */ 30 | class FaceGraphic extends GraphicOverlay.Graphic { 31 | private static final float FACE_POSITION_RADIUS = 10.0f; 32 | private static final float ID_TEXT_SIZE = 40.0f; 33 | private static final float ID_Y_OFFSET = 50.0f; 34 | private static final float ID_X_OFFSET = -50.0f; 35 | private static final float GENERIC_POS_OFFSET = 20.0f; 36 | private static final float GENERIC_NEG_OFFSET = -20.0f; 37 | private static final float BOX_STROKE_WIDTH = 5.0f; 38 | 39 | private static final int MASK[] = { 40 | R.drawable.transparent, 41 | R.drawable.hair, 42 | R.drawable.op, 43 | R.drawable.snap, 44 | R.drawable.glasses2, 45 | R.drawable.glasses3, 46 | R.drawable.glasses4, 47 | R.drawable.glasses5, 48 | R.drawable.mask, 49 | R.drawable.mask2, 50 | R.drawable.mask3, 51 | R.drawable.dog, 52 | R.drawable.cat2 53 | }; 54 | 55 | /* 56 | private static final int COLOR_CHOICES[] = { 57 | Color.BLUE, 58 | Color.CYAN, 59 | Color.GREEN, 60 | Color.MAGENTA, 61 | Color.RED, 62 | Color.WHITE, 63 | Color.YELLOW 64 | }; 65 | private static int mCurrentColorIndex = 0; 66 | private Paint mFacePositionPaint; 67 | private Paint mIdPaint; 68 | private Paint mBoxPaint;*/ 69 | 70 | private volatile Face mFace; 71 | private int mFaceId; 72 | private float mFaceHappiness; 73 | private Bitmap bitmap; 74 | private Bitmap op; 75 | 76 | FaceGraphic(GraphicOverlay overlay,int c) { 77 | super(overlay); 78 | /* 79 | mCurrentColorIndex = (mCurrentColorIndex + 1) % COLOR_CHOICES.length; 80 | final int selectedColor = COLOR_CHOICES[mCurrentColorIndex]; 81 | 82 | mFacePositionPaint = new Paint(); 83 | mFacePositionPaint.setColor(selectedColor); 84 | 85 | mIdPaint = new Paint(); 86 | mIdPaint.setColor(selectedColor); 87 | mIdPaint.setTextSize(ID_TEXT_SIZE); 88 | 89 | mBoxPaint = new Paint(); 90 | mBoxPaint.setColor(selectedColor); 91 | mBoxPaint.setStyle(Paint.Style.STROKE); 92 | mBoxPaint.setStrokeWidth(BOX_STROKE_WIDTH); 93 | */ 94 | bitmap = BitmapFactory.decodeResource(getOverlay().getContext().getResources(),MASK[c]); 95 | op = bitmap; 96 | } 97 | 98 | void setId(int id) { 99 | mFaceId = id; 100 | } 101 | 102 | /** 103 | * Updates the face instance from the detection of the most recent frame. Invalidates the 104 | * relevant portions of the overlay to trigger a redraw. 105 | */ 106 | void updateFace(Face face,int c) { 107 | mFace = face; 108 | bitmap = BitmapFactory.decodeResource(getOverlay().getContext().getResources(), MASK[c]); 109 | op = bitmap; 110 | op = Bitmap.createScaledBitmap(op, (int) scaleX(face.getWidth()), 111 | (int) scaleY(((bitmap.getHeight() * face.getWidth()) / bitmap.getWidth())), false); 112 | postInvalidate(); 113 | } 114 | 115 | /** 116 | * Draws the face annotations for position on the supplied canvas. 117 | */ 118 | @Override 119 | public void draw(Canvas canvas) { 120 | Face face = mFace; 121 | if(face == null) return; 122 | // Draws a circle at the position of the detected face, with the face's track id below. 123 | float x = translateX(face.getPosition().x + face.getWidth() / 2); 124 | float y = translateY(face.getPosition().y + face.getHeight() / 2); 125 | /* 126 | canvas.drawCircle(x, y, FACE_POSITION_RADIUS, mFacePositionPaint); 127 | canvas.drawText("id: " + mFaceId, x + ID_X_OFFSET, y + ID_Y_OFFSET, mIdPaint); 128 | canvas.drawText("happiness: " + String.format("%.2f", face.getIsSmilingProbability()), x - ID_X_OFFSET, y - ID_Y_OFFSET, mIdPaint); 129 | canvas.drawText("right eye: " + String.format("%.2f", face.getIsRightEyeOpenProbability()), x + ID_X_OFFSET * 2, y + ID_Y_OFFSET * 2, mIdPaint); 130 | canvas.drawText("left eye: " + String.format("%.2f", face.getIsLeftEyeOpenProbability()), x - ID_X_OFFSET*2, y - ID_Y_OFFSET*2, mIdPaint); 131 | */ 132 | float xOffset = scaleX(face.getWidth() / 2.0f); 133 | float yOffset = scaleY(face.getHeight() / 2.0f); 134 | float left = x - xOffset; 135 | float top = y - yOffset; 136 | //float right = x + xOffset; 137 | //float bottom = y + yOffset; 138 | //canvas.drawRect(left, top, right, bottom, mBoxPaint); 139 | canvas.drawBitmap(op, left, top, new Paint()); 140 | } 141 | /* 142 | private float getNoseAndMouthDistance(PointF nose, PointF mouth) { 143 | return (float) Math.hypot(mouth.x - nose.x, mouth.y - nose.y); 144 | }*/ 145 | } 146 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /app/src/main/java/com/gsrathoreniks/facefilter/camera/CameraSourcePreview.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) The Android Open Source Project 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 | package com.gsrathoreniks.facefilter.camera; 17 | 18 | import android.content.Context; 19 | import android.content.res.Configuration; 20 | import android.util.AttributeSet; 21 | import android.util.Log; 22 | import android.view.SurfaceHolder; 23 | import android.view.SurfaceView; 24 | import android.view.ViewGroup; 25 | 26 | import com.google.android.gms.common.images.Size; 27 | import com.google.android.gms.vision.CameraSource; 28 | 29 | import java.io.IOException; 30 | 31 | public class CameraSourcePreview extends ViewGroup { 32 | private static final String TAG = "CameraSourcePreview"; 33 | 34 | private Context mContext; 35 | private SurfaceView mSurfaceView; 36 | private boolean mStartRequested; 37 | private boolean mSurfaceAvailable; 38 | private CameraSource mCameraSource; 39 | 40 | private GraphicOverlay mOverlay; 41 | 42 | public CameraSourcePreview(Context context, AttributeSet attrs) { 43 | super(context, attrs); 44 | mContext = context; 45 | mStartRequested = false; 46 | mSurfaceAvailable = false; 47 | 48 | mSurfaceView = new SurfaceView(context); 49 | mSurfaceView.getHolder().addCallback(new SurfaceCallback()); 50 | addView(mSurfaceView); 51 | } 52 | 53 | public void start(CameraSource cameraSource) throws IOException { 54 | if (cameraSource == null) { 55 | stop(); 56 | } 57 | 58 | mCameraSource = cameraSource; 59 | 60 | if (mCameraSource != null) { 61 | mStartRequested = true; 62 | startIfReady(); 63 | } 64 | } 65 | 66 | public void start(CameraSource cameraSource, GraphicOverlay overlay) throws IOException { 67 | mOverlay = overlay; 68 | start(cameraSource); 69 | } 70 | 71 | public void stop() { 72 | if (mCameraSource != null) { 73 | mCameraSource.stop(); 74 | } 75 | } 76 | 77 | public void release() { 78 | if (mCameraSource != null) { 79 | mCameraSource.release(); 80 | mCameraSource = null; 81 | } 82 | } 83 | 84 | private void startIfReady() throws IOException { 85 | if (mStartRequested && mSurfaceAvailable) { 86 | mCameraSource.start(mSurfaceView.getHolder()); 87 | if (mOverlay != null) { 88 | Size size = mCameraSource.getPreviewSize(); 89 | int min = Math.min(size.getWidth(), size.getHeight()); 90 | int max = Math.max(size.getWidth(), size.getHeight()); 91 | if (isPortraitMode()) { 92 | // Swap width and height sizes when in portrait, since it will be rotated by 93 | // 90 degrees 94 | mOverlay.setCameraInfo(min, max, mCameraSource.getCameraFacing()); 95 | } else { 96 | mOverlay.setCameraInfo(max, min, mCameraSource.getCameraFacing()); 97 | } 98 | mOverlay.clear(); 99 | } 100 | mStartRequested = false; 101 | } 102 | } 103 | 104 | private class SurfaceCallback implements SurfaceHolder.Callback { 105 | @Override 106 | public void surfaceCreated(SurfaceHolder surface) { 107 | mSurfaceAvailable = true; 108 | try { 109 | startIfReady(); 110 | } catch (IOException e) { 111 | Log.e(TAG, "Could not start camera source.", e); 112 | } 113 | } 114 | 115 | @Override 116 | public void surfaceDestroyed(SurfaceHolder surface) { 117 | mSurfaceAvailable = false; 118 | } 119 | 120 | @Override 121 | public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { 122 | } 123 | } 124 | 125 | @Override 126 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { 127 | int width = 320; 128 | int height = 240; 129 | if (mCameraSource != null) { 130 | Size size = mCameraSource.getPreviewSize(); 131 | if (size != null) { 132 | width = size.getWidth(); 133 | height = size.getHeight(); 134 | } 135 | } 136 | 137 | // Swap width and height sizes when in portrait, since it will be rotated 90 degrees 138 | if (isPortraitMode()) { 139 | int tmp = width; 140 | width = height; 141 | height = tmp; 142 | } 143 | 144 | final int layoutWidth = right - left; 145 | final int layoutHeight = bottom - top; 146 | 147 | // Computes height and width for potentially doing fit width. 148 | int childWidth = layoutWidth; 149 | int childHeight = layoutHeight; 150 | //(int)(((float) layoutWidth / (float) width) * height); 151 | /* 152 | // If height is too tall using fit width, does fit height instead. 153 | if (childHeight > layoutHeight) { 154 | childHeight = layoutHeight; 155 | childWidth = (int)(((float) layoutHeight / (float) height) * width); 156 | }*/ 157 | 158 | for (int i = 0; i < getChildCount(); ++i) { 159 | getChildAt(i).layout(0, 0, childWidth, childHeight); 160 | } 161 | 162 | try { 163 | startIfReady(); 164 | } catch (IOException e) { 165 | Log.e(TAG, "Could not start camera source.", e); 166 | } 167 | } 168 | 169 | private boolean isPortraitMode() { 170 | int orientation = mContext.getResources().getConfiguration().orientation; 171 | if (orientation == Configuration.ORIENTATION_LANDSCAPE) { 172 | return false; 173 | } 174 | if (orientation == Configuration.ORIENTATION_PORTRAIT) { 175 | return true; 176 | } 177 | 178 | Log.d(TAG, "isPortraitMode returning false by default"); 179 | return false; 180 | } 181 | } 182 | -------------------------------------------------------------------------------- /app/src/main/java/com/gsrathoreniks/facefilter/camera/GraphicOverlay.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) The Android Open Source Project 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 | package com.gsrathoreniks.facefilter.camera; 17 | 18 | import android.content.Context; 19 | import android.graphics.Canvas; 20 | import android.util.AttributeSet; 21 | import android.view.View; 22 | 23 | import com.google.android.gms.vision.CameraSource; 24 | 25 | import java.util.HashSet; 26 | import java.util.Set; 27 | 28 | /** 29 | * A view which renders a series of custom graphics to be overlayed on top of an associated preview 30 | * (i.e., the camera preview). The creator can add graphics objects, update the objects, and remove 31 | * them, triggering the appropriate drawing and invalidation within the view.

32 | *

33 | * Supports scaling and mirroring of the graphics relative the camera's preview properties. The 34 | * idea is that detection items are expressed in terms of a preview size, but need to be scaled up 35 | * to the full view size, and also mirrored in the case of the front-facing camera.

36 | *

37 | * Associated {@link Graphic} items should use the following methods to convert to view coordinates 38 | * for the graphics that are drawn: 39 | *

    40 | *
  1. {@link Graphic#scaleX(float)} and {@link Graphic#scaleY(float)} adjust the size of the 41 | * supplied value from the preview scale to the view scale.
  2. 42 | *
  3. {@link Graphic#translateX(float)} and {@link Graphic#translateY(float)} adjust the coordinate 43 | * from the preview's coordinate system to the view coordinate system.
  4. 44 | *
45 | */ 46 | public class GraphicOverlay extends View { 47 | private final Object mLock = new Object(); 48 | private int mPreviewWidth; 49 | private float mWidthScaleFactor = 1.0f; 50 | private int mPreviewHeight; 51 | private float mHeightScaleFactor = 1.0f; 52 | private int mFacing = CameraSource.CAMERA_FACING_BACK; 53 | private Set mGraphics = new HashSet<>(); 54 | 55 | /** 56 | * Base class for a custom graphics object to be rendered within the graphic overlay. Subclass 57 | * this and implement the {@link Graphic#draw(Canvas)} method to define the 58 | * graphics element. Add instances to the overlay using {@link GraphicOverlay#add(Graphic)}. 59 | */ 60 | public static abstract class Graphic { 61 | private GraphicOverlay mOverlay; 62 | 63 | public Graphic(GraphicOverlay overlay) { 64 | mOverlay = overlay; 65 | } 66 | 67 | /** 68 | * Draw the graphic on the supplied canvas. Drawing should use the following methods to 69 | * convert to view coordinates for the graphics that are drawn: 70 | *
    71 | *
  1. {@link Graphic#scaleX(float)} and {@link Graphic#scaleY(float)} adjust the size of 72 | * the supplied value from the preview scale to the view scale.
  2. 73 | *
  3. {@link Graphic#translateX(float)} and {@link Graphic#translateY(float)} adjust the 74 | * coordinate from the preview's coordinate system to the view coordinate system.
  4. 75 | *
76 | * 77 | * @param canvas drawing canvas 78 | */ 79 | public abstract void draw(Canvas canvas); 80 | 81 | /** 82 | * Adjusts a horizontal value of the supplied value from the preview scale to the view 83 | * scale. 84 | */ 85 | public float scaleX(float horizontal) { 86 | return horizontal * mOverlay.mWidthScaleFactor; 87 | } 88 | 89 | /** 90 | * Adjusts a vertical value of the supplied value from the preview scale to the view scale. 91 | */ 92 | public float scaleY(float vertical) { 93 | return vertical * mOverlay.mHeightScaleFactor; 94 | } 95 | 96 | /** 97 | * Adjusts the x coordinate from the preview's coordinate system to the view coordinate 98 | * system. 99 | */ 100 | public float translateX(float x) { 101 | if (mOverlay.mFacing == CameraSource.CAMERA_FACING_FRONT) { 102 | return mOverlay.getWidth() - scaleX(x); 103 | } else { 104 | return scaleX(x); 105 | } 106 | } 107 | 108 | /** 109 | * Adjusts the y coordinate from the preview's coordinate system to the view coordinate 110 | * system. 111 | */ 112 | public float translateY(float y) { 113 | return scaleY(y); 114 | } 115 | 116 | public void postInvalidate() { 117 | mOverlay.postInvalidate(); 118 | } 119 | 120 | public GraphicOverlay getOverlay() { 121 | return mOverlay; 122 | } 123 | } 124 | 125 | public GraphicOverlay(Context context, AttributeSet attrs) { 126 | super(context, attrs); 127 | } 128 | 129 | /** 130 | * Removes all graphics from the overlay. 131 | */ 132 | public void clear() { 133 | synchronized (mLock) { 134 | mGraphics.clear(); 135 | } 136 | postInvalidate(); 137 | } 138 | 139 | /** 140 | * Adds a graphic to the overlay. 141 | */ 142 | public void add(Graphic graphic) { 143 | synchronized (mLock) { 144 | mGraphics.add(graphic); 145 | } 146 | postInvalidate(); 147 | } 148 | 149 | /** 150 | * Removes a graphic from the overlay. 151 | */ 152 | public void remove(Graphic graphic) { 153 | synchronized (mLock) { 154 | mGraphics.remove(graphic); 155 | } 156 | postInvalidate(); 157 | } 158 | 159 | /** 160 | * Sets the camera attributes for size and facing direction, which informs how to transform 161 | * image coordinates later. 162 | */ 163 | public void setCameraInfo(int previewWidth, int previewHeight, int facing) { 164 | synchronized (mLock) { 165 | mPreviewWidth = previewWidth; 166 | mPreviewHeight = previewHeight; 167 | mFacing = facing; 168 | } 169 | postInvalidate(); 170 | } 171 | 172 | /** 173 | * Draws the overlay with its associated graphic objects. 174 | */ 175 | @Override 176 | protected void onDraw(Canvas canvas) { 177 | super.onDraw(canvas); 178 | 179 | synchronized (mLock) { 180 | if ((mPreviewWidth != 0) && (mPreviewHeight != 0)) { 181 | mWidthScaleFactor = (float) canvas.getWidth() / (float) mPreviewWidth; 182 | mHeightScaleFactor = (float) canvas.getHeight() / (float) mPreviewHeight; 183 | } 184 | 185 | for (Graphic graphic : mGraphics) { 186 | graphic.draw(canvas); 187 | } 188 | } 189 | } 190 | } 191 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_face_filter.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 14 | 15 | 19 | 20 | 24 | 25 | 26 | 27 | 32 | 33 | 37 | 38 | 49 | 50 | 61 | 62 | 71 | 72 | 82 | 83 | 84 | 85 | 93 | 94 | 98 | 99 | 108 | 109 | 118 | 119 | 128 | 129 | 138 | 139 | 148 | 149 | 158 | 159 | 168 | 169 | 178 | 179 | 188 | 189 | 198 | 199 | 208 | 209 | 218 | 219 | 228 | 229 | 230 | 231 | 232 | 233 | 234 | 235 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright @ [Gaetano Zappi] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /app/src/main/java/com/gsrathoreniks/facefilter/FaceFilterActivity.java: -------------------------------------------------------------------------------- 1 | package com.gsrathoreniks.facefilter; 2 | 3 | import android.Manifest; 4 | import android.app.Activity; 5 | import android.app.AlertDialog; 6 | import android.app.Dialog; 7 | import android.content.ContentValues; 8 | import android.content.Context; 9 | import android.content.DialogInterface; 10 | import android.content.pm.PackageManager; 11 | import android.graphics.Bitmap; 12 | import android.graphics.BitmapFactory; 13 | import android.graphics.Camera; 14 | import android.graphics.Color; 15 | import android.graphics.Matrix; 16 | import android.os.Bundle; 17 | import android.os.Environment; 18 | import android.provider.MediaStore; 19 | import android.support.design.widget.Snackbar; 20 | import android.support.v4.app.ActivityCompat; 21 | import android.support.v7.app.AppCompatActivity; 22 | import android.util.Log; 23 | import android.view.View; 24 | import android.widget.ImageButton; 25 | import android.widget.Toast; 26 | 27 | import com.google.android.gms.common.ConnectionResult; 28 | import com.google.android.gms.common.GoogleApiAvailability; 29 | import com.google.android.gms.vision.CameraSource; 30 | import com.google.android.gms.vision.MultiProcessor; 31 | import com.google.android.gms.vision.Tracker; 32 | import com.google.android.gms.vision.face.Face; 33 | import com.google.android.gms.vision.face.FaceDetector; 34 | import com.gsrathoreniks.facefilter.camera.CameraSourcePreview; 35 | import com.gsrathoreniks.facefilter.camera.GraphicOverlay; 36 | 37 | import java.io.ByteArrayOutputStream; 38 | import java.io.File; 39 | import java.io.FileOutputStream; 40 | import java.io.IOException; 41 | 42 | import static android.view.View.GONE; 43 | 44 | public class FaceFilterActivity extends AppCompatActivity { 45 | TextGraphic mTextGraphic; 46 | 47 | private final Thread observer = new Thread("observer") { 48 | 49 | { 50 | setDaemon(true); 51 | } 52 | 53 | public void run() { 54 | 55 | while( !isInterrupted() ) { 56 | /* 57 | TextGraphic mTextGraphic = new TextGraphic(mGraphicOverlay); 58 | mGraphicOverlay.add(mTextGraphic);*/ 59 | //mTextGraphic.updateText(2); 60 | } 61 | 62 | }; 63 | }; 64 | 65 | private static final String TAG = "FaceTracker"; 66 | 67 | private CameraSource mCameraSource = null; 68 | private int typeFace = 0; 69 | private int typeFlash = 0; 70 | private boolean flashmode = false; 71 | private Camera camera; 72 | 73 | private static final int MASK[] = { 74 | R.id.no_filter, 75 | R.id.hair, 76 | R.id.op, 77 | R.id.snap, 78 | R.id.glasses2, 79 | R.id.glasses3, 80 | R.id.glasses4, 81 | R.id.glasses5, 82 | R.id.mask, 83 | R.id.mask2, 84 | R.id.mask3, 85 | R.id.dog, 86 | R.id.cat2 87 | }; 88 | 89 | private CameraSourcePreview mPreview; 90 | private GraphicOverlay mGraphicOverlay; 91 | 92 | private static final int RC_HANDLE_GMS = 9001; 93 | // permission request codes need to be < 256 94 | private static final int RC_HANDLE_CAMERA_PERM = 2; 95 | 96 | //============================================================================================== 97 | // Activity Methods 98 | //============================================================================================== 99 | 100 | /** 101 | * Initializes the UI and initiates the creation of a face detector. 102 | */ 103 | @Override 104 | public void onCreate(Bundle icicle) { 105 | super.onCreate(icicle); 106 | setContentView(R.layout.activity_face_filter); 107 | 108 | mPreview = (CameraSourcePreview) findViewById(R.id.preview); 109 | mGraphicOverlay = (GraphicOverlay) findViewById(R.id.faceOverlay); 110 | //mTextGraphic = new TextGraphic(mGraphicOverlay); 111 | //mGraphicOverlay.add(mTextGraphic); 112 | 113 | ImageButton face = (ImageButton) findViewById(R.id.face); 114 | face.setOnClickListener(new View.OnClickListener() { 115 | public void onClick(View v) { 116 | if(findViewById(R.id.scrollView).getVisibility() == GONE){ 117 | findViewById(R.id.scrollView).setVisibility(View.VISIBLE); 118 | ((ImageButton) findViewById(R.id.face)).setImageResource(R.drawable.face_select); 119 | }else{ 120 | findViewById(R.id.scrollView).setVisibility(GONE); 121 | ((ImageButton) findViewById(R.id.face)).setImageResource(R.drawable.face); 122 | } 123 | } 124 | }); 125 | 126 | ImageButton no_filter = (ImageButton) findViewById(R.id.no_filter); 127 | no_filter.setOnClickListener(new View.OnClickListener() { 128 | public void onClick(View v) { 129 | findViewById(MASK[typeFace]).setBackgroundResource(R.drawable.round_background); 130 | typeFace = 0; 131 | findViewById(MASK[typeFace]).setBackgroundResource(R.drawable.round_background_select); 132 | } 133 | }); 134 | 135 | ImageButton hair = (ImageButton) findViewById(R.id.hair); 136 | hair.setOnClickListener(new View.OnClickListener() { 137 | public void onClick(View v) { 138 | findViewById(MASK[typeFace]).setBackgroundResource(R.drawable.round_background); 139 | typeFace = 1; 140 | findViewById(MASK[typeFace]).setBackgroundResource(R.drawable.round_background_select); 141 | } 142 | }); 143 | 144 | ImageButton op = (ImageButton) findViewById(R.id.op); 145 | op.setOnClickListener(new View.OnClickListener() { 146 | public void onClick(View v) { 147 | findViewById(MASK[typeFace]).setBackgroundResource(R.drawable.round_background); 148 | typeFace = 2; 149 | findViewById(MASK[typeFace]).setBackgroundResource(R.drawable.round_background_select); 150 | } 151 | }); 152 | 153 | ImageButton snap = (ImageButton) findViewById(R.id.snap); 154 | snap.setOnClickListener(new View.OnClickListener() { 155 | public void onClick(View v) { 156 | findViewById(MASK[typeFace]).setBackgroundResource(R.drawable.round_background); 157 | typeFace = 3; 158 | findViewById(MASK[typeFace]).setBackgroundResource(R.drawable.round_background_select); 159 | } 160 | }); 161 | 162 | ImageButton glasses2 = (ImageButton) findViewById(R.id.glasses2); 163 | glasses2.setOnClickListener(new View.OnClickListener() { 164 | public void onClick(View v) { 165 | findViewById(MASK[typeFace]).setBackgroundResource(R.drawable.round_background); 166 | typeFace = 4; 167 | findViewById(MASK[typeFace]).setBackgroundResource(R.drawable.round_background_select); 168 | } 169 | }); 170 | 171 | ImageButton glasses3 = (ImageButton) findViewById(R.id.glasses3); 172 | glasses3.setOnClickListener(new View.OnClickListener() { 173 | public void onClick(View v) { 174 | findViewById(MASK[typeFace]).setBackgroundResource(R.drawable.round_background); 175 | typeFace = 5; 176 | findViewById(MASK[typeFace]).setBackgroundResource(R.drawable.round_background_select); 177 | } 178 | }); 179 | 180 | ImageButton glasses4 = (ImageButton) findViewById(R.id.glasses4); 181 | glasses4.setOnClickListener(new View.OnClickListener() { 182 | public void onClick(View v) { 183 | findViewById(MASK[typeFace]).setBackgroundResource(R.drawable.round_background); 184 | typeFace = 6; 185 | findViewById(MASK[typeFace]).setBackgroundResource(R.drawable.round_background_select); 186 | } 187 | }); 188 | 189 | ImageButton glasses5 = (ImageButton) findViewById(R.id.glasses5); 190 | glasses5.setOnClickListener(new View.OnClickListener() { 191 | public void onClick(View v) { 192 | findViewById(MASK[typeFace]).setBackgroundResource(R.drawable.round_background); 193 | typeFace = 7; 194 | findViewById(MASK[typeFace]).setBackgroundResource(R.drawable.round_background_select); 195 | } 196 | }); 197 | 198 | ImageButton mask = (ImageButton) findViewById(R.id.mask); 199 | mask.setOnClickListener(new View.OnClickListener() { 200 | public void onClick(View v) { 201 | findViewById(MASK[typeFace]).setBackgroundResource(R.drawable.round_background); 202 | typeFace = 8; 203 | findViewById(MASK[typeFace]).setBackgroundResource(R.drawable.round_background_select); 204 | } 205 | }); 206 | 207 | ImageButton mask2 = (ImageButton) findViewById(R.id.mask2); 208 | mask2.setOnClickListener(new View.OnClickListener() { 209 | public void onClick(View v) { 210 | findViewById(MASK[typeFace]).setBackgroundResource(R.drawable.round_background); 211 | typeFace = 9; 212 | findViewById(MASK[typeFace]).setBackgroundResource(R.drawable.round_background_select); 213 | } 214 | }); 215 | 216 | ImageButton mask3 = (ImageButton) findViewById(R.id.mask3); 217 | mask3.setOnClickListener(new View.OnClickListener() { 218 | public void onClick(View v) { 219 | findViewById(MASK[typeFace]).setBackgroundResource(R.drawable.round_background); 220 | typeFace = 10; 221 | findViewById(MASK[typeFace]).setBackgroundResource(R.drawable.round_background_select); 222 | } 223 | }); 224 | 225 | ImageButton dog = (ImageButton) findViewById(R.id.dog); 226 | dog.setOnClickListener(new View.OnClickListener() { 227 | public void onClick(View v) { 228 | findViewById(MASK[typeFace]).setBackgroundResource(R.drawable.round_background); 229 | typeFace = 11; 230 | findViewById(MASK[typeFace]).setBackgroundResource(R.drawable.round_background_select); 231 | } 232 | }); 233 | 234 | ImageButton cat2 = (ImageButton) findViewById(R.id.cat2); 235 | cat2.setOnClickListener(new View.OnClickListener() { 236 | public void onClick(View v) { 237 | findViewById(MASK[typeFace]).setBackgroundResource(R.drawable.round_background); 238 | typeFace = 12; 239 | findViewById(MASK[typeFace]).setBackgroundResource(R.drawable.round_background_select); 240 | } 241 | }); 242 | 243 | ImageButton button = (ImageButton) findViewById(R.id.change); 244 | button.setOnClickListener(new View.OnClickListener() { 245 | public void onClick(View v) { 246 | } 247 | }); 248 | 249 | final ImageButton flash = (ImageButton) findViewById(R.id.flash); 250 | flash.setOnClickListener(new View.OnClickListener() { 251 | public void onClick(View v) { 252 | int blue; 253 | if(flashmode==false){ 254 | flashmode = true; 255 | blue = 0; 256 | }else{ 257 | flashmode = false; 258 | blue = 255; 259 | } 260 | flash.setColorFilter(Color.argb(255, 255, 255, blue)); 261 | } 262 | }); 263 | 264 | ImageButton camera = (ImageButton) findViewById(R.id.camera); 265 | camera.setOnClickListener(new View.OnClickListener() { 266 | public void onClick(View v) { 267 | //takeImage(); 268 | onPause(); 269 | } 270 | }); 271 | 272 | 273 | 274 | // Check for the camera permission before accessing the camera. If the 275 | // permission is not granted yet, request permission. 276 | int rc = ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA); 277 | if (rc == PackageManager.PERMISSION_GRANTED) { 278 | createCameraSource(); 279 | } else { 280 | requestCameraPermission(); 281 | } 282 | } 283 | 284 | private void takeImage() { 285 | try{ 286 | //openCamera(CameraInfo.CAMERA_FACING_BACK); 287 | //releaseCameraSource(); 288 | //releaseCamera(); 289 | //openCamera(CameraInfo.CAMERA_FACING_BACK); 290 | //setUpCamera(camera); 291 | //Thread.sleep(1000); 292 | mCameraSource.takePicture(null, new CameraSource.PictureCallback() { 293 | 294 | private File imageFile; 295 | @Override 296 | public void onPictureTaken(byte[] bytes) { 297 | try { 298 | // convert byte array into bitmap 299 | Bitmap loadedImage = null; 300 | Bitmap rotatedBitmap = null; 301 | loadedImage = BitmapFactory.decodeByteArray(bytes, 0, 302 | bytes.length); 303 | 304 | // rotate Image 305 | Matrix rotateMatrix = new Matrix(); 306 | rotateMatrix.postRotate(getWindowManager().getDefaultDisplay().getRotation()); 307 | rotatedBitmap = Bitmap.createBitmap(loadedImage, 0, 0, 308 | loadedImage.getWidth(), loadedImage.getHeight(), 309 | rotateMatrix, false); 310 | String state = Environment.getExternalStorageState(); 311 | File folder = null; 312 | if (state.contains(Environment.MEDIA_MOUNTED)) { 313 | folder = new File(Environment 314 | .getExternalStorageDirectory()+"/faceFilter"); 315 | } else { 316 | folder = new File(Environment 317 | .getExternalStorageDirectory() + "/faceFilter"); 318 | } 319 | 320 | boolean success = true; 321 | if (!folder.exists()) { 322 | success = folder.mkdirs(); 323 | } 324 | if (success) { 325 | java.util.Date date = new java.util.Date(); 326 | imageFile = new File(folder.getAbsolutePath() 327 | + File.separator 328 | //+ new Timestamp(date.getTime()).toString() 329 | + "Image.jpg"); 330 | 331 | imageFile.createNewFile(); 332 | } else { 333 | Toast.makeText(getBaseContext(), "Image Not saved", 334 | Toast.LENGTH_SHORT).show(); 335 | return; 336 | } 337 | 338 | ByteArrayOutputStream ostream = new ByteArrayOutputStream(); 339 | 340 | // save image into gallery 341 | rotatedBitmap = resize(rotatedBitmap, 800, 600); 342 | rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, ostream); 343 | 344 | FileOutputStream fout = new FileOutputStream(imageFile); 345 | fout.write(ostream.toByteArray()); 346 | fout.close(); 347 | ContentValues values = new ContentValues(); 348 | 349 | values.put(MediaStore.Images.Media.DATE_TAKEN, 350 | System.currentTimeMillis()); 351 | values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg"); 352 | values.put(MediaStore.MediaColumns.DATA, 353 | imageFile.getAbsolutePath()); 354 | 355 | setResult(Activity.RESULT_OK); //add this 356 | finish(); 357 | } catch (Exception e) { 358 | e.printStackTrace(); 359 | } 360 | } 361 | }); 362 | 363 | }catch (Exception ex){ 364 | } 365 | 366 | } 367 | 368 | private Bitmap resize(Bitmap image, int maxWidth, int maxHeight) { 369 | if (maxHeight > 0 && maxWidth > 0) { 370 | int width = image.getWidth(); 371 | int height = image.getHeight(); 372 | float ratioBitmap = (float) width / (float) height; 373 | float ratioMax = (float) maxWidth / (float) maxHeight; 374 | 375 | int finalWidth = maxWidth; 376 | int finalHeight = maxHeight; 377 | if (ratioMax > 1) { 378 | finalWidth = (int) ((float) maxHeight * ratioBitmap); 379 | } else { 380 | finalHeight = (int) ((float) maxWidth / ratioBitmap); 381 | } 382 | image = Bitmap.createScaledBitmap(image, finalWidth, finalHeight, true); 383 | return image; 384 | } else { 385 | return image; 386 | } 387 | } 388 | 389 | /** 390 | * Handles the requesting of the camera permission. This includes 391 | * showing a "Snackbar" message of why the permission is needed then 392 | * sending the request. 393 | */ 394 | private void requestCameraPermission() { 395 | Log.w(TAG, "Camera permission is not granted. Requesting permission"); 396 | 397 | final String[] permissions = new String[]{Manifest.permission.CAMERA}; 398 | 399 | if (!ActivityCompat.shouldShowRequestPermissionRationale(this, 400 | Manifest.permission.CAMERA)) { 401 | ActivityCompat.requestPermissions(this, permissions, RC_HANDLE_CAMERA_PERM); 402 | return; 403 | } 404 | 405 | final Activity thisActivity = this; 406 | 407 | View.OnClickListener listener = new View.OnClickListener() { 408 | @Override 409 | public void onClick(View view) { 410 | ActivityCompat.requestPermissions(thisActivity, permissions, 411 | RC_HANDLE_CAMERA_PERM); 412 | } 413 | }; 414 | 415 | Snackbar.make(mGraphicOverlay, R.string.permission_camera_rationale, 416 | Snackbar.LENGTH_INDEFINITE) 417 | .setAction(R.string.ok, listener) 418 | .show(); 419 | } 420 | 421 | /** 422 | * Creates and starts the camera. Note that this uses a higher resolution in comparison 423 | * to other detection examples to enable the barcode detector to detect small barcodes 424 | * at long distances. 425 | */ 426 | private void createCameraSource() { 427 | 428 | Context context = getApplicationContext(); 429 | FaceDetector detector = new FaceDetector.Builder(context) 430 | .setClassificationType(FaceDetector.ALL_CLASSIFICATIONS) 431 | .setLandmarkType(FaceDetector.ALL_LANDMARKS) 432 | .setMode(FaceDetector.ACCURATE_MODE) 433 | .build(); 434 | 435 | detector.setProcessor( 436 | new MultiProcessor.Builder<>(new GraphicFaceTrackerFactory()) 437 | .build()); 438 | 439 | //new MultiProcessor.Builder<>(new GraphicTextTrackerFactory()).build(); 440 | 441 | if (!detector.isOperational()) { 442 | // Note: The first time that an app using face API is installed on a device, GMS will 443 | // download a native library to the device in order to do detection. Usually this 444 | // completes before the app is run for the first time. But if that download has not yet 445 | // completed, then the above call will not detect any faces. 446 | // 447 | // isOperational() can be used to check if the required native library is currently 448 | // available. The detector will automatically become operational once the library 449 | // download completes on device. 450 | Log.w(TAG, "Face detector dependencies are not yet available."); 451 | } 452 | 453 | mCameraSource = new CameraSource.Builder(context, detector) 454 | .setRequestedPreviewSize(640, 480) 455 | .setAutoFocusEnabled(true) 456 | .setFacing(CameraSource.CAMERA_FACING_BACK) 457 | .setRequestedFps(30.0f) 458 | .build(); 459 | //observer.start(); 460 | /* 461 | TextGraphic mTextGraphic = new TextGraphic(mGraphicOverlay); 462 | mGraphicOverlay.add(mTextGraphic); 463 | mTextGraphic.updateText(2);*/ 464 | } 465 | 466 | /** 467 | * Restarts the camera. 468 | */ 469 | @Override 470 | protected void onResume() { 471 | super.onResume(); 472 | 473 | startCameraSource(); 474 | } 475 | 476 | /** 477 | * Stops the camera. 478 | */ 479 | @Override 480 | protected void onPause() { 481 | super.onPause(); 482 | mPreview.stop(); 483 | } 484 | 485 | /** 486 | * Releases the resources associated with the camera source, the associated detector, and the 487 | * rest of the processing pipeline. 488 | */ 489 | @Override 490 | protected void onDestroy() { 491 | super.onDestroy(); 492 | if (mCameraSource != null) { 493 | mCameraSource.release(); 494 | } 495 | } 496 | 497 | /** 498 | * Callback for the result from requesting permissions. This method 499 | * is invoked for every call on {@link #requestPermissions(String[], int)}. 500 | *

501 | * Note: It is possible that the permissions request interaction 502 | * with the user is interrupted. In this case you will receive empty permissions 503 | * and results arrays which should be treated as a cancellation. 504 | *

505 | * 506 | * @param requestCode The request code passed in {@link #requestPermissions(String[], int)}. 507 | * @param permissions The requested permissions. Never null. 508 | * @param grantResults The grant results for the corresponding permissions 509 | * which is either {@link PackageManager#PERMISSION_GRANTED} 510 | * or {@link PackageManager#PERMISSION_DENIED}. Never null. 511 | * @see #requestPermissions(String[], int) 512 | */ 513 | @Override 514 | public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { 515 | if (requestCode != RC_HANDLE_CAMERA_PERM) { 516 | Log.d(TAG, "Got unexpected permission result: " + requestCode); 517 | super.onRequestPermissionsResult(requestCode, permissions, grantResults); 518 | return; 519 | } 520 | 521 | if (grantResults.length != 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 522 | Log.d(TAG, "Camera permission granted - initialize the camera source"); 523 | // we have permission, so create the camerasource 524 | createCameraSource(); 525 | return; 526 | } 527 | 528 | Log.e(TAG, "Permission not granted: results len = " + grantResults.length + 529 | " Result code = " + (grantResults.length > 0 ? grantResults[0] : "(empty)")); 530 | 531 | DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() { 532 | public void onClick(DialogInterface dialog, int id) { 533 | finish(); 534 | } 535 | }; 536 | 537 | AlertDialog.Builder builder = new AlertDialog.Builder(this); 538 | builder.setTitle("Face Tracker sample") 539 | .setMessage(R.string.no_camera_permission) 540 | .setPositiveButton(R.string.ok, listener) 541 | .show(); 542 | } 543 | 544 | //============================================================================================== 545 | // Camera Source Preview 546 | //============================================================================================== 547 | 548 | /** 549 | * Starts or restarts the camera source, if it exists. If the camera source doesn't exist yet 550 | * (e.g., because onResume was called before the camera source was created), this will be called 551 | * again when the camera source is created. 552 | */ 553 | private void startCameraSource() { 554 | 555 | // check that the device has play services available. 556 | int code = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable( 557 | getApplicationContext()); 558 | if (code != ConnectionResult.SUCCESS) { 559 | Dialog dlg = 560 | GoogleApiAvailability.getInstance().getErrorDialog(this, code, RC_HANDLE_GMS); 561 | dlg.show(); 562 | } 563 | 564 | if (mCameraSource != null) { 565 | try { 566 | mPreview.start(mCameraSource, mGraphicOverlay); 567 | } catch (IOException e) { 568 | Log.e(TAG, "Unable to start camera source.", e); 569 | mCameraSource.release(); 570 | mCameraSource = null; 571 | } 572 | } 573 | } 574 | 575 | private class GraphicTextTrackerFactory implements MultiProcessor.Factory { 576 | @Override 577 | public Tracker create(String face) { 578 | return new GraphicTextTracker(mGraphicOverlay); 579 | } 580 | } 581 | 582 | private class GraphicTextTracker extends Tracker { 583 | private GraphicOverlay mOverlay; 584 | private TextGraphic mTextGraphic ; 585 | 586 | GraphicTextTracker(GraphicOverlay overlay) { 587 | mOverlay = overlay; 588 | mTextGraphic = new TextGraphic(overlay); 589 | } 590 | 591 | public void onUpdate() { 592 | mOverlay.add(mTextGraphic); 593 | mTextGraphic.updateText(3); 594 | } 595 | 596 | @Override 597 | public void onDone() { 598 | mOverlay.remove(mTextGraphic); 599 | } 600 | } 601 | 602 | //============================================================================================== 603 | // Graphic Face Tracker 604 | //============================================================================================== 605 | 606 | /** 607 | * Factory for creating a face tracker to be associated with a new face. The multiprocessor 608 | * uses this factory to create face trackers as needed -- one for each individual. 609 | */ 610 | private class GraphicFaceTrackerFactory implements MultiProcessor.Factory { 611 | @Override 612 | public Tracker create(Face face) { 613 | return new GraphicFaceTracker(mGraphicOverlay); 614 | } 615 | } 616 | 617 | /** 618 | * Face tracker for each detected individual. This maintains a face graphic within the app's 619 | * associated face overlay. 620 | */ 621 | private class GraphicFaceTracker extends Tracker { 622 | private GraphicOverlay mOverlay; 623 | private FaceGraphic mFaceGraphic; 624 | 625 | GraphicFaceTracker(GraphicOverlay overlay) { 626 | mOverlay = overlay; 627 | mFaceGraphic = new FaceGraphic(overlay,typeFace); 628 | } 629 | 630 | /** 631 | * Start tracking the detected face instance within the face overlay. 632 | */ 633 | @Override 634 | public void onNewItem(int faceId, Face item) { 635 | mFaceGraphic.setId(faceId); 636 | } 637 | 638 | /** 639 | * Update the position/characteristics of the face within the overlay. 640 | */ 641 | @Override 642 | public void onUpdate(FaceDetector.Detections detectionResults, Face face) { 643 | mOverlay.add(mFaceGraphic); 644 | mFaceGraphic.updateFace(face,typeFace); 645 | } 646 | 647 | /** 648 | * Hide the graphic when the corresponding face was not detected. This can happen for 649 | * intermediate frames temporarily (e.g., if the face was momentarily blocked from 650 | * view). 651 | */ 652 | @Override 653 | public void onMissing(FaceDetector.Detections detectionResults) { 654 | mOverlay.remove(mFaceGraphic); 655 | } 656 | 657 | /** 658 | * Called when the face is assumed to be gone for good. Remove the graphic annotation from 659 | * the overlay. 660 | */ 661 | @Override 662 | public void onDone() { 663 | mOverlay.remove(mFaceGraphic); 664 | } 665 | } 666 | } --------------------------------------------------------------------------------