├── .gitignore ├── .idea ├── codeStyles │ └── Project.xml ├── markdown-navigator │ └── profiles_settings.xml ├── misc.xml ├── modules.xml ├── runConfigurations.xml └── vcs.xml ├── LICENSE ├── README.md ├── app ├── .gitignore ├── art │ ├── barcode.png │ ├── faces.png │ └── labels.png ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── io │ │ └── husaynhakeem │ │ └── mlkit_sample │ │ └── ExampleInstrumentedTest.kt │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── io │ │ │ └── husaynhakeem │ │ │ └── mlkit_sample │ │ │ ├── MLKitApplication.kt │ │ │ ├── core │ │ │ ├── api │ │ │ │ ├── BarcodeDetector.kt │ │ │ │ ├── FaceDetector.kt │ │ │ │ ├── ImageLabeler.kt │ │ │ │ ├── LandmarkDetector.kt │ │ │ │ ├── MLKitApi.kt │ │ │ │ ├── MLkitApiFactory.kt │ │ │ │ └── TextDetector.kt │ │ │ ├── model │ │ │ │ ├── MLKitApiType.kt │ │ │ │ └── OptionModels.kt │ │ │ ├── ui │ │ │ │ └── CenteredHorizontalLayoutManager.kt │ │ │ ├── visionimage │ │ │ │ ├── BitmapVisionImageGenerator.kt │ │ │ │ ├── FileVisionImageGenerator.kt │ │ │ │ └── FirebaseVisionImageGenerator.kt │ │ │ └── wrapper │ │ │ │ └── SharedPreferencesWrapper.kt │ │ │ └── ui │ │ │ ├── MainActivity.kt │ │ │ ├── MainViewModel.kt │ │ │ ├── MainViewState.kt │ │ │ ├── data │ │ │ └── UserOptionsRepository.kt │ │ │ ├── dialog │ │ │ ├── ImagePickerDialog.kt │ │ │ └── MLKitApiAboutDialog.kt │ │ │ ├── recycler │ │ │ ├── UserOptionViewHolder.kt │ │ │ └── UserOptionsAdapter.kt │ │ │ └── usecase │ │ │ └── DisplayMLKitAboutDialogUseCase.kt │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ ├── ic_barcode_detector.png │ │ ├── ic_face_detector.png │ │ ├── ic_github.png │ │ ├── ic_image_labeler.png │ │ ├── ic_landmark_detector.png │ │ ├── ic_launcher_background.xml │ │ ├── ic_new_image.xml │ │ ├── ic_text_detector.png │ │ └── white_rounded_background.xml │ │ ├── layout │ │ ├── activity_main.xml │ │ └── item_user_option.xml │ │ ├── menu │ │ └── menu_main.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.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 │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── io │ └── husaynhakeem │ └── mlkit_sample │ └── ExampleUnitTest.kt ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | # Built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # Files for the ART/Dalvik VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # Generated files 12 | bin/ 13 | gen/ 14 | out/ 15 | 16 | # Gradle files 17 | .gradle/ 18 | build/ 19 | 20 | # Local configuration file (sdk path, etc) 21 | local.properties 22 | 23 | # Proguard folder generated by Eclipse 24 | proguard/ 25 | 26 | # Log Files 27 | *.log 28 | 29 | # Android Studio Navigation editor temp files 30 | .navigation/ 31 | 32 | # Android Studio captures folder 33 | captures/ 34 | 35 | # IntelliJ 36 | *.iml 37 | .idea/workspace.xml 38 | .idea/tasks.xml 39 | .idea/gradle.xml 40 | .idea/assetWizardSettings.xml 41 | .idea/dictionaries 42 | .idea/libraries 43 | .idea/caches 44 | 45 | # Keystore files 46 | # Uncomment the following line if you do not want to check your keystore files in. 47 | #*.jks 48 | 49 | # External native build folder generated in Android Studio 2.2 and later 50 | .externalNativeBuild 51 | 52 | # Google Services (e.g. APIs or Firebase) 53 | google-services.json 54 | 55 | # Freeline 56 | freeline.py 57 | freeline/ 58 | freeline_project_description.json 59 | 60 | # fastlane 61 | fastlane/report.xml 62 | fastlane/Preview.html 63 | fastlane/screenshots 64 | fastlane/test_output 65 | fastlane/readme.md 66 | -------------------------------------------------------------------------------- /.idea/codeStyles/Project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 15 | 16 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /.idea/markdown-navigator/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 36 | 37 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 93 | 104 | 105 | 106 | 107 | 108 | 109 | 111 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Husayn Hakeem 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Android ML Kit Sample 2 | 3 | This is a sample Android application I built in order to practice [Firebase's Machine learning kit](https://firebase.google.com/products/ml-kit/) (Firebase ML Kit). 4 | 5 | ## Related articles 6 | After building this project, I wrote 2 articles about Firebase's ML Kit, [the first](https://proandroiddev.com/firebase-machine-learning-kit-101-738baea0253f) is more or an introduction to the topic, while [the second](https://proandroiddev.com/firebase-machine-learning-kit-101-f6ab9e7d03c3) deals more specifically with using ML Kit's APIs in this application, and technologies/design patterns it uses in general. 7 | 8 | 9 | ## Libraries used in this project 10 | This project uses dependencies from the new Android extension libraries (AndroidX). It also follows a MVVM architecture. 11 | - [LiveData](https://developer.android.com/topic/libraries/architecture/livedata) 12 | - [ViewModel](https://developer.android.com/topic/libraries/architecture/viewmodel) 13 | - [Firebase ML Kit](https://firebase.google.com/docs/ml-kit/) 14 | - [ImagePicker](https://github.com/esafirm/android-image-picker) 15 | 16 | 17 | ## Demo 18 | ![alt text](https://github.com/husaynhakeem/Android-ML-Kit-Sample/blob/master/app/art/barcode.png) 19 | ![alt text](https://github.com/husaynhakeem/Android-ML-Kit-Sample/blob/master/app/art/faces.png) 20 | ![alt text](https://github.com/husaynhakeem/Android-ML-Kit-Sample/blob/master/app/art/labels.png) 21 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | # Built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # Files for the ART/Dalvik VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # Generated files 12 | bin/ 13 | gen/ 14 | out/ 15 | 16 | # Gradle files 17 | .gradle/ 18 | build/ 19 | 20 | # Local configuration file (sdk path, etc) 21 | local.properties 22 | 23 | # Proguard folder generated by Eclipse 24 | proguard/ 25 | 26 | # Log Files 27 | *.log 28 | 29 | # Android Studio Navigation editor temp files 30 | .navigation/ 31 | 32 | # Android Studio captures folder 33 | captures/ 34 | 35 | # IntelliJ 36 | *.iml 37 | .idea/workspace.xml 38 | .idea/tasks.xml 39 | .idea/gradle.xml 40 | .idea/assetWizardSettings.xml 41 | .idea/dictionaries 42 | .idea/libraries 43 | .idea/caches 44 | 45 | # Keystore files 46 | # Uncomment the following line if you do not want to check your keystore files in. 47 | #*.jks 48 | 49 | # External native build folder generated in Android Studio 2.2 and later 50 | .externalNativeBuild 51 | 52 | # Google Services (e.g. APIs or Firebase) 53 | google-services.json 54 | 55 | # Freeline 56 | freeline.py 57 | freeline/ 58 | freeline_project_description.json 59 | 60 | # fastlane 61 | fastlane/report.xml 62 | fastlane/Preview.html 63 | fastlane/screenshots 64 | fastlane/test_output 65 | fastlane/readme.md 66 | -------------------------------------------------------------------------------- /app/art/barcode.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/husaynhakeem/Android-ML-Kit-Sample/8e8f3766da920ace0f5e9f0e83d95679cb98f935/app/art/barcode.png -------------------------------------------------------------------------------- /app/art/faces.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/husaynhakeem/Android-ML-Kit-Sample/8e8f3766da920ace0f5e9f0e83d95679cb98f935/app/art/faces.png -------------------------------------------------------------------------------- /app/art/labels.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/husaynhakeem/Android-ML-Kit-Sample/8e8f3766da920ace0f5e9f0e83d95679cb98f935/app/art/labels.png -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | apply plugin: 'kotlin-android' 3 | apply plugin: 'kotlin-android-extensions' 4 | 5 | android { 6 | compileSdkVersion 'android-P' 7 | defaultConfig { 8 | applicationId "io.husaynhakeem.mlkit_sample" 9 | minSdkVersion 21 10 | targetSdkVersion 27 11 | versionCode 1 12 | versionName "1.0" 13 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 14 | } 15 | buildTypes { 16 | release { 17 | minifyEnabled false 18 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 19 | } 20 | } 21 | } 22 | 23 | dependencies { 24 | implementation fileTree(dir: 'libs', include: ['*.jar']) 25 | implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" 26 | 27 | implementation 'androidx.appcompat:appcompat:1.0.0-alpha1' 28 | implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0-alpha1' 29 | 30 | testImplementation 'junit:junit:4.12' 31 | androidTestImplementation 'androidx.test:runner:1.1.0-alpha1' 32 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha1' 33 | 34 | // Material 35 | implementation "com.google.android.material:material:$materialVersion" 36 | 37 | // Firebase 38 | implementation "com.google.firebase:firebase-core:$firebaseCoreVersion" 39 | implementation "com.google.firebase:firebase-ml-vision:$firebaseMLKitVersion" 40 | implementation "com.google.firebase:firebase-ml-vision-image-label-model:$firebaseMLKitVersion" 41 | 42 | // Camera/Gallery photo picker 43 | implementation "com.github.esafirm.android-image-picker:imagepicker:$imagePickerVersion" 44 | } 45 | 46 | apply plugin: 'com.google.gms.google-services' -------------------------------------------------------------------------------- /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/io/husaynhakeem/mlkit_sample/ExampleInstrumentedTest.kt: -------------------------------------------------------------------------------- 1 | package io.husaynhakeem.mlkit_sample 2 | 3 | import androidx.test.InstrumentationRegistry 4 | import androidx.test.runner.AndroidJUnit4 5 | 6 | import org.junit.Test 7 | import org.junit.runner.RunWith 8 | 9 | import org.junit.Assert.* 10 | 11 | /** 12 | * Instrumented test, which will execute on an Android device. 13 | * 14 | * See [testing documentation](http://d.android.com/tools/testing). 15 | */ 16 | @RunWith(AndroidJUnit4::class) 17 | class ExampleInstrumentedTest { 18 | @Test 19 | fun useAppContext() { 20 | // Context of the app under test. 21 | val appContext = InstrumentationRegistry.getTargetContext() 22 | assertEquals("io.husaynhakeem.mlkit_sample", appContext.packageName) 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /app/src/main/java/io/husaynhakeem/mlkit_sample/MLKitApplication.kt: -------------------------------------------------------------------------------- 1 | package io.husaynhakeem.mlkit_sample 2 | 3 | import android.app.Application 4 | 5 | 6 | class MLKitApplication: Application() { 7 | 8 | override fun onCreate() { 9 | super.onCreate() 10 | instance = this 11 | } 12 | 13 | companion object { 14 | lateinit var instance : MLKitApplication 15 | } 16 | } -------------------------------------------------------------------------------- /app/src/main/java/io/husaynhakeem/mlkit_sample/core/api/BarcodeDetector.kt: -------------------------------------------------------------------------------- 1 | package io.husaynhakeem.mlkit_sample.core.api 2 | 3 | import com.google.android.gms.tasks.Task 4 | import com.google.firebase.ml.vision.barcode.FirebaseVisionBarcode 5 | import com.google.firebase.ml.vision.barcode.FirebaseVisionBarcode.* 6 | import com.google.firebase.ml.vision.barcode.FirebaseVisionBarcodeDetector 7 | import io.husaynhakeem.mlkit_sample.core.visionimage.BitmapVisionImageGenerator 8 | 9 | 10 | class BarcodeDetector : MLKitApi>() { 11 | 12 | override val processor: FirebaseVisionBarcodeDetector 13 | get() = firebaseVisionInstance.visionBarcodeDetector 14 | 15 | override fun detectInImage(image: String, onSuccess: (String) -> Unit, onFailure: (String) -> Unit): Task> { 16 | return processor.detectInImage(BitmapVisionImageGenerator(image).get()) 17 | } 18 | 19 | override fun onDetectionSuccess(result: List) = with(StringBuilder()) { 20 | result.forEach { 21 | append("Barcode raw value: ${it.rawValue}\n") 22 | append("Barcode display value: ${it.displayValue}\n") 23 | append("Barcode format: ${it.format}\n") 24 | 25 | when (it.valueType) { 26 | TYPE_WIFI -> append(onBarcodeOfTypeWifi(it.wifi)) 27 | TYPE_CALENDAR_EVENT -> append(onBarcodeOfTypeCalendarEvent(it.calendarEvent)) 28 | TYPE_CONTACT_INFO -> append(onBarcodeOfTypeContactInfo(it.contactInfo)) 29 | TYPE_DRIVER_LICENSE -> append(onBarcodeOfTypeDriverLicense(it.driverLicense)) 30 | TYPE_EMAIL -> append(onBarcodeOfTypeEmail(it.email)) 31 | TYPE_GEO -> append(onBarcodeOfTypeGeoPoint(it.geoPoint)) 32 | TYPE_PHONE -> append(onBarcodeOfTypePhone(it.phone)) 33 | TYPE_SMS -> append(onBarcodeOfTypeSms(it.sms)) 34 | TYPE_URL -> append(onBarcodeOfTypeUrl(it.url)) 35 | TYPE_TEXT -> append("Barcode of type text") 36 | TYPE_PRODUCT -> append("Barcode of type product") 37 | TYPE_ISBN -> append("Barcode of type isbn") 38 | TYPE_UNKNOWN -> append("Barcode of unknown type") 39 | } 40 | } 41 | 42 | if (this.isBlank()) { 43 | return RESULT_TITLE + EMPTY_RESULT_MESSAGE 44 | } 45 | 46 | RESULT_TITLE + toString() 47 | } 48 | 49 | private fun onBarcodeOfTypeWifi(wifi: WiFi?): String = with(StringBuilder()) { 50 | append("Barcode of type wifi\n") 51 | wifi?.let { 52 | append("Password: ${it.password}\n") 53 | append("Encryption type: ${it.encryptionType}\n") 54 | append("Ssid: ${it.ssid}\n") 55 | } 56 | toString() 57 | } 58 | 59 | private fun onBarcodeOfTypeCalendarEvent(calendarEvent: CalendarEvent?): String = with(StringBuilder()) { 60 | append("Barcode of type calendar event\n") 61 | calendarEvent?.let { 62 | append("Organizer: ${it.organizer}\n") 63 | append("Summary: ${it.summary}\n") 64 | append("Location: ${it.location}\n") 65 | append("Description: ${it.description}\n") 66 | append("Status: ${it.status}\n") 67 | append("Starts ${it.start} and ends ${it.end}\n") 68 | } 69 | toString() 70 | } 71 | 72 | private fun onBarcodeOfTypeContactInfo(contactInfo: ContactInfo?): String = with(StringBuilder()) { 73 | append("Barcode of type contact info\n") 74 | contactInfo?.let { 75 | append("Name: ${it.name}\n") 76 | append("Title: ${it.title}\n") 77 | append("Organization: ${it.organization}\n") 78 | append("Emails: ${it.emails}\n") 79 | append("Addresses: ${it.addresses}\n") 80 | append("Phones: ${it.phones}\n") 81 | append("Urls: ${it.urls}\n") 82 | } 83 | toString() 84 | } 85 | 86 | private fun onBarcodeOfTypeDriverLicense(driverLicense: DriverLicense?): String = with(StringBuilder()) { 87 | append("Barcode of type driver license\n") 88 | driverLicense?.let { 89 | append("Full name: ${it.firstName} ${it.middleName} ${it.lastName}\n") 90 | append("Gender: ${it.gender}\n") 91 | append("Born: ${it.birthDate}\n") 92 | append("Issued: ${it.issueDate}, expires ${it.expiryDate}\n") 93 | append("Address: ${it.addressStreet}, ${it.addressCity}, ${it.addressState}, ${it.addressZip}\n") 94 | } 95 | toString() 96 | } 97 | 98 | private fun onBarcodeOfTypeEmail(email: Email?): String = with(StringBuilder()) { 99 | append("Barcode of type email\n") 100 | email?.let { 101 | append("Addressed to ${it.address}\n") 102 | append("Subject: ${it.subject}\n") 103 | append("Type: ${it.type}\n") 104 | append("Body: ${it.body}\n") 105 | } 106 | toString() 107 | } 108 | 109 | private fun onBarcodeOfTypeGeoPoint(geoPoint: GeoPoint?): String = with(StringBuilder()) { 110 | append("Barcode of type geoPoint\n") 111 | geoPoint?.let { 112 | append("Coordinates(${it.lat}, ${it.lng})\n") 113 | } 114 | toString() 115 | } 116 | 117 | private fun onBarcodeOfTypePhone(phone: Phone?): String = with(StringBuilder()) { 118 | append("Barcode of type phone\n") 119 | phone?.let { 120 | append("Type: ${it.type}\n") 121 | append("Number: ${it.number}\n") 122 | } 123 | toString() 124 | } 125 | 126 | private fun onBarcodeOfTypeSms(sms: Sms?): String = with(StringBuilder()) { 127 | append("Barcode of type sms\n") 128 | sms?.let { 129 | append("Phone number: ${it.phoneNumber}\n") 130 | append("Message: ${it.message}\n") 131 | } 132 | toString() 133 | } 134 | 135 | private fun onBarcodeOfTypeUrl(url: UrlBookmark?): String = with(StringBuilder()) { 136 | append("Barcode of type url\n") 137 | url?.let { 138 | append("Title: ${it.title}\n") 139 | append("Url: ${it.url}\n") 140 | } 141 | toString() 142 | } 143 | 144 | override fun onDetectionFailure(exception: Exception): String { 145 | return ERROR_MESSAGE + exception.message 146 | } 147 | 148 | companion object { 149 | private const val RESULT_TITLE = "Barcode detection results\n\n" 150 | 151 | private const val EMPTY_RESULT_MESSAGE = "Failed to detect barcodes in the provided image." 152 | 153 | private const val ERROR_MESSAGE = "An error occurred while trying to detect barcodes in the provided image.\n\nCause: " 154 | } 155 | } -------------------------------------------------------------------------------- /app/src/main/java/io/husaynhakeem/mlkit_sample/core/api/FaceDetector.kt: -------------------------------------------------------------------------------- 1 | package io.husaynhakeem.mlkit_sample.core.api 2 | 3 | import com.google.android.gms.tasks.Task 4 | import com.google.firebase.ml.vision.face.FirebaseVisionFace 5 | import com.google.firebase.ml.vision.face.FirebaseVisionFaceDetector 6 | import com.google.firebase.ml.vision.face.FirebaseVisionFaceDetectorOptions 7 | import com.google.firebase.ml.vision.face.FirebaseVisionFaceDetectorOptions.* 8 | import com.google.firebase.ml.vision.face.FirebaseVisionFaceLandmark 9 | import io.husaynhakeem.mlkit_sample.core.visionimage.BitmapVisionImageGenerator 10 | 11 | 12 | class FaceDetector : MLKitApi>() { 13 | 14 | override val processor: FirebaseVisionFaceDetector 15 | get() = firebaseVisionInstance.getVisionFaceDetector( 16 | FirebaseVisionFaceDetectorOptions.Builder() 17 | .setModeType(ACCURATE_MODE) 18 | .setLandmarkType(ALL_LANDMARKS) 19 | .setClassificationType(ALL_CLASSIFICATIONS) 20 | .setTrackingEnabled(true) 21 | .build()) 22 | 23 | override fun detectInImage(image: String, onSuccess: (String) -> Unit, onFailure: (String) -> Unit): Task> { 24 | return processor.detectInImage(BitmapVisionImageGenerator(image).get()) 25 | } 26 | 27 | override fun onDetectionSuccess(result: List) = with(StringBuilder()) { 28 | result.forEach { 29 | append("Head id: ${it.trackingId}\n") 30 | 31 | val bounds = it.boundingBox 32 | val rotY = Math.round(it.headEulerAngleY) 33 | val rotZ = Math.round(it.headEulerAngleZ) 34 | 35 | append("Head is rotated to the right $rotY degrees\n") 36 | append("Head is tilted sideways $rotZ degrees\n") 37 | 38 | val smilingProbability = Math.round(it.smilingProbability * 100) 39 | append("Smiling probability $smilingProbability%\n") 40 | 41 | val rightEyeOpenProbability = Math.round(it.rightEyeOpenProbability * 100) 42 | append("Right eye open probablity $rightEyeOpenProbability%\n") 43 | 44 | val leftEyeOpenProbability = Math.round(it.leftEyeOpenProbability * 100) 45 | append("Left eye open probablity $leftEyeOpenProbability%\n") 46 | 47 | val rightEar = it.getLandmark(FirebaseVisionFaceLandmark.RIGHT_EAR) 48 | val leftEar = it.getLandmark(FirebaseVisionFaceLandmark.LEFT_EAR) 49 | 50 | rightEar?.position?.let { 51 | append("Right ear (${it.x}, ${it.y}, ${it.z})\n") 52 | } 53 | 54 | leftEar?.position?.let { 55 | append("Right ear (${it.x}, ${it.y}, ${it.z})\n") 56 | } 57 | 58 | append("\n") 59 | } 60 | 61 | if (this.isBlank()) { 62 | return RESULT_TITLE + EMPTY_RESULT_MESSAGE 63 | } 64 | 65 | RESULT_TITLE + toString() 66 | } 67 | 68 | override fun onDetectionFailure(exception: Exception): String { 69 | return ERROR_MESSAGE + exception.message 70 | } 71 | 72 | companion object { 73 | private const val RESULT_TITLE = "Face detection results\n\n" 74 | 75 | private const val EMPTY_RESULT_MESSAGE = "Failed to detect faces in the provided image." 76 | 77 | private const val ERROR_MESSAGE = "An error occurred while trying to detect faces in the provided image.\n\nCause: " 78 | } 79 | } -------------------------------------------------------------------------------- /app/src/main/java/io/husaynhakeem/mlkit_sample/core/api/ImageLabeler.kt: -------------------------------------------------------------------------------- 1 | package io.husaynhakeem.mlkit_sample.core.api 2 | 3 | import com.google.android.gms.tasks.Task 4 | import com.google.firebase.ml.vision.label.FirebaseVisionLabel 5 | import com.google.firebase.ml.vision.label.FirebaseVisionLabelDetector 6 | import com.google.firebase.ml.vision.label.FirebaseVisionLabelDetectorOptions 7 | import io.husaynhakeem.mlkit_sample.core.visionimage.BitmapVisionImageGenerator 8 | 9 | 10 | class ImageLabeler : MLKitApi>() { 11 | 12 | override val processor: FirebaseVisionLabelDetector 13 | get() = firebaseVisionInstance.getVisionLabelDetector( 14 | FirebaseVisionLabelDetectorOptions.Builder() 15 | .setConfidenceThreshold(0.5f) 16 | .build()) 17 | 18 | override fun detectInImage(image: String, onSuccess: (String) -> Unit, onFailure: (String) -> Unit): Task> { 19 | return processor.detectInImage(BitmapVisionImageGenerator(image).get()) 20 | } 21 | 22 | override fun onDetectionSuccess(result: List) = with(StringBuilder()) { 23 | result.forEach { 24 | val label = it.label 25 | val confidenceProbability = Math.round(it.confidence * 100) 26 | append("- A $label was detected in the image with a probability of $confidenceProbability%\n") 27 | } 28 | 29 | if (this.isBlank()) { 30 | return RESULT_TITLE + EMPTY_RESULT_MESSAGE 31 | } 32 | 33 | RESULT_TITLE + toString() 34 | } 35 | 36 | override fun onDetectionFailure(exception: Exception): String { 37 | return ERROR_MESSAGE + exception.message 38 | } 39 | 40 | companion object { 41 | private const val RESULT_TITLE = "Image labeling results\n\n" 42 | 43 | private const val EMPTY_RESULT_MESSAGE = "Failed to label objects in the provided image." 44 | 45 | private const val ERROR_MESSAGE = "An error occurred while trying to label objects in the provided image.\n\nCause: " 46 | } 47 | } -------------------------------------------------------------------------------- /app/src/main/java/io/husaynhakeem/mlkit_sample/core/api/LandmarkDetector.kt: -------------------------------------------------------------------------------- 1 | package io.husaynhakeem.mlkit_sample.core.api 2 | 3 | import com.google.android.gms.tasks.Task 4 | import com.google.firebase.ml.vision.cloud.FirebaseVisionCloudDetectorOptions 5 | import com.google.firebase.ml.vision.cloud.FirebaseVisionCloudDetectorOptions.LATEST_MODEL 6 | import com.google.firebase.ml.vision.cloud.landmark.FirebaseVisionCloudLandmark 7 | import com.google.firebase.ml.vision.cloud.landmark.FirebaseVisionCloudLandmarkDetector 8 | import io.husaynhakeem.mlkit_sample.core.visionimage.BitmapVisionImageGenerator 9 | 10 | 11 | class LandmarkDetector : MLKitApi>() { 12 | 13 | override val processor: FirebaseVisionCloudLandmarkDetector 14 | get() = firebaseVisionInstance.getVisionCloudLandmarkDetector( 15 | FirebaseVisionCloudDetectorOptions.Builder() 16 | .setModelType(LATEST_MODEL) 17 | .setMaxResults(15) 18 | .build()) 19 | 20 | override fun detectInImage(image: String, onSuccess: (String) -> Unit, onFailure: (String) -> Unit): Task> { 21 | return processor.detectInImage(BitmapVisionImageGenerator(image).get()) 22 | } 23 | 24 | override fun onDetectionSuccess(result: List) = with(StringBuilder()) { 25 | result.forEach { 26 | append("Landmark id: ${it.entityId}") 27 | 28 | val confidenceProbability = it.confidence * 100 29 | append("Probability of confidence: $confidenceProbability") 30 | 31 | val landmark = it.landmark 32 | append("The landmark is called $landmark") 33 | 34 | it.locations.forEach { 35 | append("Location(${it.latitude}, ${it.longitude})") 36 | } 37 | } 38 | 39 | if (this.isBlank()) { 40 | return RESULT_TITLE + EMPTY_RESULT_MESSAGE 41 | } 42 | 43 | RESULT_TITLE + toString() 44 | } 45 | 46 | override fun onDetectionFailure(exception: Exception): String { 47 | return ERROR_MESSAGE + exception.message 48 | } 49 | 50 | companion object { 51 | private const val RESULT_TITLE = "Landmark detection results\n\n" 52 | 53 | private const val EMPTY_RESULT_MESSAGE = "Failed to detect landmarks in the provided image." 54 | 55 | private const val ERROR_MESSAGE = "An error occurred while trying to detect landmarks in the provided image.\n\nCause: " 56 | } 57 | } -------------------------------------------------------------------------------- /app/src/main/java/io/husaynhakeem/mlkit_sample/core/api/MLKitApi.kt: -------------------------------------------------------------------------------- 1 | package io.husaynhakeem.mlkit_sample.core.api 2 | 3 | import com.google.android.gms.tasks.Task 4 | import com.google.firebase.ml.vision.FirebaseVision 5 | 6 | 7 | abstract class MLKitApi { 8 | 9 | protected val firebaseVisionInstance = FirebaseVision.getInstance() 10 | 11 | protected abstract val processor: P 12 | 13 | protected abstract fun detectInImage(image: String, onSuccess: (String) -> Unit, onFailure: (String) -> Unit): Task 14 | 15 | protected abstract fun onDetectionSuccess(result: T): String 16 | 17 | protected abstract fun onDetectionFailure(exception: Exception): String 18 | 19 | fun process(image: String, onSuccess: (String) -> Unit, onFailure: (String) -> Unit) { 20 | detectInImage(image, onSuccess, onFailure) 21 | .addOnSuccessListener { onSuccess(onDetectionSuccess(it)) } 22 | .addOnFailureListener { onFailure(onDetectionFailure(it)) } 23 | } 24 | } -------------------------------------------------------------------------------- /app/src/main/java/io/husaynhakeem/mlkit_sample/core/api/MLkitApiFactory.kt: -------------------------------------------------------------------------------- 1 | package io.husaynhakeem.mlkit_sample.core.api 2 | 3 | import io.husaynhakeem.mlkit_sample.core.model.MLKitApiType 4 | import io.husaynhakeem.mlkit_sample.core.model.MLKitApiType.* 5 | import java.util.* 6 | 7 | 8 | object MLkitApiFactory { 9 | 10 | private val apis = EnumMap>(MLKitApiType::class.java) 11 | 12 | fun get(type: MLKitApiType): MLKitApi<*, *> = when (type) { 13 | BARCODE_DETECTOR -> apis.getOrPut(BARCODE_DETECTOR, { BarcodeDetector() }) 14 | FACE_DETECTOR -> apis.getOrPut(FACE_DETECTOR, { FaceDetector() }) 15 | IMAGE_LABELER -> apis.getOrPut(IMAGE_LABELER, { ImageLabeler() }) 16 | LANDMARK_DETECTOR -> apis.getOrPut(LANDMARK_DETECTOR, { LandmarkDetector() }) 17 | TEXT_DETECTOR -> apis.getOrPut(TEXT_DETECTOR, { TextDetector() }) 18 | } 19 | } -------------------------------------------------------------------------------- /app/src/main/java/io/husaynhakeem/mlkit_sample/core/api/TextDetector.kt: -------------------------------------------------------------------------------- 1 | package io.husaynhakeem.mlkit_sample.core.api 2 | 3 | import com.google.android.gms.tasks.Task 4 | import com.google.firebase.ml.vision.text.FirebaseVisionText 5 | import com.google.firebase.ml.vision.text.FirebaseVisionTextDetector 6 | import io.husaynhakeem.mlkit_sample.core.visionimage.BitmapVisionImageGenerator 7 | 8 | 9 | class TextDetector : MLKitApi() { 10 | 11 | override val processor: FirebaseVisionTextDetector 12 | get() = firebaseVisionInstance.visionTextDetector 13 | 14 | override fun detectInImage(image: String, onSuccess: (String) -> Unit, onFailure: (String) -> Unit): Task { 15 | return processor.detectInImage(BitmapVisionImageGenerator(image).get()) 16 | } 17 | 18 | override fun onDetectionSuccess(result: FirebaseVisionText): String { 19 | val stringResult = recognizedTextAsBlocks(result) 20 | if (stringResult.isBlank()) { 21 | return RESULT_TITLE + EMPTY_RESULT_MESSAGE 22 | } 23 | return RESULT_TITLE + stringResult 24 | } 25 | 26 | private fun recognizedTextAsSingleLine(text: FirebaseVisionText): String = with(StringBuilder()) { 27 | text.blocks.forEach { 28 | it.lines.forEach { 29 | it.elements.forEach { 30 | append(it.text).append(" ") 31 | } 32 | } 33 | } 34 | toString() 35 | } 36 | 37 | private fun recognizedTextAsBlocks(text: FirebaseVisionText): String = with(StringBuilder()) { 38 | 39 | text.blocks.forEach { 40 | append(it.text).append(" ") 41 | } 42 | 43 | toString() 44 | } 45 | 46 | override fun onDetectionFailure(exception: Exception): String { 47 | return ERROR_MESSAGE + exception.message 48 | } 49 | 50 | companion object { 51 | private const val RESULT_TITLE = "Text detection results\n\n" 52 | 53 | private const val EMPTY_RESULT_MESSAGE = "Failed to detect text in the provided image." 54 | 55 | private const val ERROR_MESSAGE = "An error occurred while trying to detect text in the provided image.\n\nCause: " 56 | } 57 | } -------------------------------------------------------------------------------- /app/src/main/java/io/husaynhakeem/mlkit_sample/core/model/MLKitApiType.kt: -------------------------------------------------------------------------------- 1 | package io.husaynhakeem.mlkit_sample.core.model 2 | 3 | 4 | enum class MLKitApiType { 5 | BARCODE_DETECTOR, 6 | FACE_DETECTOR, 7 | IMAGE_LABELER, 8 | LANDMARK_DETECTOR, 9 | TEXT_DETECTOR 10 | } -------------------------------------------------------------------------------- /app/src/main/java/io/husaynhakeem/mlkit_sample/core/model/OptionModels.kt: -------------------------------------------------------------------------------- 1 | package io.husaynhakeem.mlkit_sample.core.model 2 | 3 | import androidx.annotation.DrawableRes 4 | import androidx.annotation.StringRes 5 | 6 | open class UserOption( 7 | @DrawableRes open val iconResId: Int, 8 | @StringRes open val title: Int, 9 | @StringRes open val body: Int) 10 | 11 | data class MLKitApiOption( 12 | @DrawableRes override val iconResId: Int, 13 | @StringRes override val title: Int, 14 | @StringRes override val body: Int, 15 | val type: MLKitApiType, 16 | val isEnabled: Boolean) : UserOption(iconResId, title, body) 17 | 18 | data class NewImageOption( 19 | @DrawableRes override val iconResId: Int, 20 | @StringRes override val title: Int, 21 | @StringRes override val body: Int) : UserOption(iconResId, title, body) 22 | -------------------------------------------------------------------------------- /app/src/main/java/io/husaynhakeem/mlkit_sample/core/ui/CenteredHorizontalLayoutManager.kt: -------------------------------------------------------------------------------- 1 | package io.husaynhakeem.mlkit_sample.core.ui 2 | 3 | import androidx.appcompat.app.AppCompatActivity 4 | import androidx.recyclerview.widget.LinearLayoutManager 5 | import io.husaynhakeem.mlkit_sample.R 6 | 7 | 8 | class CenteredHorizontalLayoutManager(context: AppCompatActivity) : 9 | LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false) { 10 | 11 | private val windowWidth = context.windowManager.defaultDisplay.width 12 | private val itemViewWidth = context.resources.getDimension(R.dimen.user_option_imageview_size) 13 | 14 | override fun getPaddingLeft(): Int { 15 | return ((windowWidth / 2) - (itemViewWidth / 2)).toInt() 16 | } 17 | 18 | override fun getPaddingRight(): Int { 19 | return paddingLeft 20 | } 21 | } -------------------------------------------------------------------------------- /app/src/main/java/io/husaynhakeem/mlkit_sample/core/visionimage/BitmapVisionImageGenerator.kt: -------------------------------------------------------------------------------- 1 | package io.husaynhakeem.mlkit_sample.core.visionimage 2 | 3 | import android.graphics.BitmapFactory 4 | import com.google.firebase.ml.vision.common.FirebaseVisionImage 5 | 6 | 7 | class BitmapVisionImageGenerator(private val imagePath: String) : FirebaseVisionImageGenerator { 8 | 9 | override fun get(): FirebaseVisionImage = FirebaseVisionImage.fromBitmap(BitmapFactory.decodeFile(imagePath)) 10 | } -------------------------------------------------------------------------------- /app/src/main/java/io/husaynhakeem/mlkit_sample/core/visionimage/FileVisionImageGenerator.kt: -------------------------------------------------------------------------------- 1 | package io.husaynhakeem.mlkit_sample.core.visionimage 2 | 3 | import android.content.Context 4 | import android.net.Uri 5 | import com.google.firebase.ml.vision.common.FirebaseVisionImage 6 | 7 | 8 | class FileVisionImageGenerator( 9 | private val context: Context, 10 | private val imagePath: String) : FirebaseVisionImageGenerator { 11 | 12 | override fun get(): FirebaseVisionImage = 13 | FirebaseVisionImage.fromFilePath(context, Uri.parse(URI_FILE_PREFIX + imagePath)) 14 | 15 | companion object { 16 | private const val URI_FILE_PREFIX = "file://" 17 | } 18 | } -------------------------------------------------------------------------------- /app/src/main/java/io/husaynhakeem/mlkit_sample/core/visionimage/FirebaseVisionImageGenerator.kt: -------------------------------------------------------------------------------- 1 | package io.husaynhakeem.mlkit_sample.core.visionimage 2 | 3 | import com.google.firebase.ml.vision.common.FirebaseVisionImage 4 | 5 | 6 | interface FirebaseVisionImageGenerator { 7 | 8 | fun get() : FirebaseVisionImage 9 | } -------------------------------------------------------------------------------- /app/src/main/java/io/husaynhakeem/mlkit_sample/core/wrapper/SharedPreferencesWrapper.kt: -------------------------------------------------------------------------------- 1 | package io.husaynhakeem.mlkit_sample.core.wrapper 2 | 3 | import android.content.Context.MODE_PRIVATE 4 | import io.husaynhakeem.mlkit_sample.MLKitApplication 5 | import io.husaynhakeem.mlkit_sample.R 6 | 7 | 8 | object SharedPreferencesWrapper { 9 | 10 | private val preferences by lazy { 11 | with(MLKitApplication.instance.applicationContext) { 12 | this.getSharedPreferences(this.getString(R.string.preferences_file), MODE_PRIVATE) 13 | } 14 | } 15 | 16 | fun put(key: String, value: Any) { 17 | when (value) { 18 | is Int -> preferences.edit().putInt(key, value).apply() 19 | is Long -> preferences.edit().putLong(key, value).apply() 20 | is Boolean -> preferences.edit().putBoolean(key, value).apply() 21 | is Float -> preferences.edit().putFloat(key, value).apply() 22 | is String -> preferences.edit().putString(key, value).apply() 23 | } 24 | } 25 | 26 | fun getBoolean(key: String) = preferences.getBoolean(key, true) 27 | } -------------------------------------------------------------------------------- /app/src/main/java/io/husaynhakeem/mlkit_sample/ui/MainActivity.kt: -------------------------------------------------------------------------------- 1 | package io.husaynhakeem.mlkit_sample.ui 2 | 3 | import android.content.Intent 4 | import android.graphics.BitmapFactory 5 | import android.net.Uri 6 | import android.os.Bundle 7 | import android.view.Menu 8 | import android.view.MenuItem 9 | import android.view.View 10 | import androidx.appcompat.app.AppCompatActivity 11 | import androidx.lifecycle.Observer 12 | import androidx.lifecycle.ViewModelProviders 13 | import androidx.recyclerview.widget.LinearSnapHelper 14 | import com.esafirm.imagepicker.features.ImagePicker 15 | import io.husaynhakeem.mlkit_sample.R 16 | import io.husaynhakeem.mlkit_sample.core.model.MLKitApiOption 17 | import io.husaynhakeem.mlkit_sample.core.model.NewImageOption 18 | import io.husaynhakeem.mlkit_sample.core.model.UserOption 19 | import io.husaynhakeem.mlkit_sample.core.ui.CenteredHorizontalLayoutManager 20 | import io.husaynhakeem.mlkit_sample.ui.dialog.ImagePickerDialog 21 | import io.husaynhakeem.mlkit_sample.ui.dialog.MLKitApiAboutDialog 22 | import io.husaynhakeem.mlkit_sample.ui.recycler.UserOptionsAdapter 23 | import kotlinx.android.synthetic.main.activity_main.* 24 | 25 | class MainActivity : AppCompatActivity(), ImagePickerDialog.Listener, MLKitApiAboutDialog.Listener { 26 | 27 | private lateinit var viewModel: MainViewModel 28 | 29 | override fun onCreate(savedInstanceState: Bundle?) { 30 | super.onCreate(savedInstanceState) 31 | setContentView(R.layout.activity_main) 32 | setUpViewModel() 33 | setupBackgroundClickListener() 34 | } 35 | 36 | private fun setUpViewModel() { 37 | viewModel = ViewModelProviders.of(this).get(MainViewModel::class.java) 38 | viewModel.viewState.observe(this, Observer { 39 | if (it == null) { 40 | return@Observer 41 | } 42 | renderUserOptions(it.userOptions) 43 | renderLoading(it.isLoading) 44 | renderSelectedImage(it.imagePath) 45 | renderResult(it.result) 46 | renderError(it.error) 47 | renderMLKitApiAboutDialog(it.displayAboutDialog, it.mlKitApiOption) 48 | }) 49 | } 50 | 51 | //====================================================== 52 | //region UI rendering 53 | //====================================================== 54 | private fun renderUserOptions(options: Array) { 55 | if (userOptionsRecyclerView.adapter == null) { 56 | userOptionsRecyclerView.layoutManager = CenteredHorizontalLayoutManager(this@MainActivity) 57 | LinearSnapHelper().attachToRecyclerView(userOptionsRecyclerView) 58 | userOptionsRecyclerView.adapter = UserOptionsAdapter(options, { onUserOptionClicked(it) }) 59 | } 60 | } 61 | 62 | private fun renderLoading(isLoading: Boolean) { 63 | progressLoader.visibility = if (isLoading) View.VISIBLE else View.GONE 64 | } 65 | 66 | private fun renderSelectedImage(imagePath: String) { 67 | if (imagePath.isBlank()) { 68 | showImagePicker(false) 69 | } else { 70 | selectedImageImageView.setImageBitmap(BitmapFactory.decodeFile(imagePath)) 71 | } 72 | } 73 | 74 | private fun showImagePicker(isCancelable: Boolean) { 75 | if (supportFragmentManager.findFragmentByTag(ImagePickerDialog.TAG) == null) { 76 | val imagePickerDialog = ImagePickerDialog() 77 | imagePickerDialog.isCancelable = isCancelable 78 | imagePickerDialog.show(supportFragmentManager, ImagePickerDialog.TAG) 79 | } 80 | } 81 | 82 | private fun onUserOptionClicked(userOption: UserOption) { 83 | when (userOption) { 84 | is NewImageOption -> showImagePicker(true) 85 | is MLKitApiOption -> viewModel.onMLKitApiOptionSelected(userOption) 86 | } 87 | } 88 | 89 | private fun renderResult(result: String) { 90 | if (result.isNotBlank()) 91 | resultTextView.text = result 92 | } 93 | 94 | private fun renderError(error: String) { 95 | if (error.isNotBlank()) 96 | resultTextView.text = error 97 | } 98 | 99 | private fun renderMLKitApiAboutDialog(displayAboutDialog: Boolean, option: MLKitApiOption) { 100 | if (displayAboutDialog) 101 | showMLKitAboutDialog(option) 102 | } 103 | 104 | private fun showMLKitAboutDialog(option: MLKitApiOption) { 105 | if (supportFragmentManager.findFragmentByTag(MLKitApiAboutDialog.TAG) == null) { 106 | val dialog = MLKitApiAboutDialog() 107 | val bundle = Bundle() 108 | bundle.putInt(MLKitApiAboutDialog.KEY_TITLE, option.title) 109 | bundle.putInt(MLKitApiAboutDialog.KEY_BODY, option.body) 110 | dialog.arguments = bundle 111 | dialog.show(supportFragmentManager, MLKitApiAboutDialog.TAG) 112 | } 113 | } 114 | //endregion 115 | 116 | private fun setupBackgroundClickListener() { 117 | resultTextView.setOnClickListener { 118 | userOptionsRecyclerView.visibility = 119 | if (userOptionsRecyclerView.visibility == View.VISIBLE) View.GONE else View.VISIBLE 120 | } 121 | } 122 | 123 | override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { 124 | super.onActivityResult(requestCode, resultCode, data) 125 | if (ImagePicker.shouldHandle(requestCode, resultCode, data)) { 126 | val image = ImagePicker.getFirstImageOrNull(data) 127 | if (image != null) { 128 | viewModel.onImageSelected(image.path) 129 | } 130 | } 131 | } 132 | 133 | //====================================================== 134 | //region ImagePickerDialog.Listener 135 | //====================================================== 136 | override fun onCameraSelected() { 137 | ImagePicker.cameraOnly().start(this) 138 | } 139 | 140 | override fun onGallerySelected() { 141 | ImagePicker.create(this) 142 | .showCamera(false) 143 | .theme(R.style.CameraPickerTheme) 144 | .start() 145 | } 146 | //endregion 147 | 148 | //====================================================== 149 | //region MLKitApiAboutDialog.Listener 150 | //====================================================== 151 | override fun onDismissed() { 152 | viewModel.onMLKitAboutDialogDismissed() 153 | } 154 | //endregion 155 | 156 | //====================================================== 157 | //region Menu 158 | //====================================================== 159 | override fun onCreateOptionsMenu(menu: Menu?): Boolean { 160 | menuInflater.inflate(R.menu.menu_main, menu) 161 | return true 162 | } 163 | 164 | override fun onOptionsItemSelected(item: MenuItem): Boolean { 165 | return when (item.itemId) { 166 | R.id.menuItemGithub -> { 167 | openGithubProfile() 168 | true 169 | } 170 | else -> false 171 | } 172 | } 173 | 174 | private fun openGithubProfile() { 175 | val intent = Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.github_profile_url))) 176 | startActivity(intent) 177 | } 178 | //endregion 179 | } 180 | -------------------------------------------------------------------------------- /app/src/main/java/io/husaynhakeem/mlkit_sample/ui/MainViewModel.kt: -------------------------------------------------------------------------------- 1 | package io.husaynhakeem.mlkit_sample.ui 2 | 3 | import androidx.lifecycle.MutableLiveData 4 | import androidx.lifecycle.ViewModel 5 | import io.husaynhakeem.mlkit_sample.core.api.MLkitApiFactory 6 | import io.husaynhakeem.mlkit_sample.core.model.MLKitApiOption 7 | import io.husaynhakeem.mlkit_sample.ui.usecase.DisplayMLKitAboutDialogUseCase 8 | 9 | class MainViewModel : ViewModel() { 10 | 11 | val viewState: MutableLiveData by lazy { 12 | MutableLiveData().apply { 13 | value = MainViewState() 14 | } 15 | } 16 | 17 | fun onImageSelected(imagePath: String) { 18 | viewState.value = viewState.value?.copy(imagePath = imagePath) 19 | processImage() 20 | } 21 | 22 | private fun processImage() { 23 | viewState.value?.let { 24 | processImageWithMLKitApiOption(it.imagePath, it.mlKitApiOption) 25 | } 26 | } 27 | 28 | private fun processImageWithMLKitApiOption(image: String, mlKitApiOption: MLKitApiOption) { 29 | if (!mlKitApiOption.isEnabled) { 30 | return 31 | } 32 | viewState.value = viewState.value?.copy(isLoading = true) 33 | MLkitApiFactory.get(mlKitApiOption.type).process( 34 | image, 35 | { viewState.value = viewState.value?.copy(isLoading = false, result = it, error = "") }, 36 | { viewState.value = viewState.value?.copy(isLoading = false, result = "", error = it) }) 37 | } 38 | 39 | fun onMLKitApiOptionSelected(option: MLKitApiOption) { 40 | viewState.value?.mlKitApiOption = option 41 | showAboutDialogForMLKitApi(option) 42 | processImage() 43 | } 44 | 45 | private fun showAboutDialogForMLKitApi(option: MLKitApiOption) { 46 | if (DisplayMLKitAboutDialogUseCase.shouldShowAboutDialogFor(option)) { 47 | viewState.value = viewState.value?.copy(displayAboutDialog = true) 48 | } 49 | } 50 | 51 | fun onMLKitAboutDialogDismissed() { 52 | viewState.value = viewState.value?.copy(displayAboutDialog = false) 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /app/src/main/java/io/husaynhakeem/mlkit_sample/ui/MainViewState.kt: -------------------------------------------------------------------------------- 1 | package io.husaynhakeem.mlkit_sample.ui 2 | 3 | import io.husaynhakeem.mlkit_sample.core.model.MLKitApiOption 4 | import io.husaynhakeem.mlkit_sample.core.model.UserOption 5 | import io.husaynhakeem.mlkit_sample.ui.data.UserOptionsRepository 6 | 7 | 8 | data class MainViewState( 9 | val userOptions: Array = UserOptionsRepository.options, 10 | var isLoading: Boolean = false, 11 | var imagePath: String = "", 12 | var result: String = "", 13 | var error: String = "", 14 | var mlKitApiOption: MLKitApiOption = UserOptionsRepository.firstMLKitApiOption, 15 | var displayAboutDialog: Boolean = false 16 | ) -------------------------------------------------------------------------------- /app/src/main/java/io/husaynhakeem/mlkit_sample/ui/data/UserOptionsRepository.kt: -------------------------------------------------------------------------------- 1 | package io.husaynhakeem.mlkit_sample.ui.data 2 | 3 | import io.husaynhakeem.mlkit_sample.R 4 | import io.husaynhakeem.mlkit_sample.core.model.MLKitApiOption 5 | import io.husaynhakeem.mlkit_sample.core.model.MLKitApiType.* 6 | import io.husaynhakeem.mlkit_sample.core.model.NewImageOption 7 | import io.husaynhakeem.mlkit_sample.core.model.UserOption 8 | 9 | 10 | object UserOptionsRepository { 11 | 12 | val options: Array by lazy { 13 | arrayOf( 14 | NewImageOption( 15 | R.drawable.ic_new_image, 16 | R.string.new_image_title, 17 | R.string.new_image_body), 18 | MLKitApiOption( 19 | R.drawable.ic_barcode_detector, 20 | R.string.barcode_detector_title, 21 | R.string.barcode_detector_body, 22 | BARCODE_DETECTOR, 23 | true), 24 | MLKitApiOption( 25 | R.drawable.ic_face_detector, 26 | R.string.face_detector_title, 27 | R.string.face_detector_body, 28 | FACE_DETECTOR, 29 | true), 30 | MLKitApiOption( 31 | R.drawable.ic_image_labeler, 32 | R.string.image_labeler_title, 33 | R.string.image_labeler_body, 34 | IMAGE_LABELER, 35 | true), 36 | MLKitApiOption( 37 | R.drawable.ic_landmark_detector, 38 | R.string.landmark_detector_title, 39 | R.string.landmark_detector_body, 40 | LANDMARK_DETECTOR, 41 | false), 42 | MLKitApiOption( 43 | R.drawable.ic_text_detector, 44 | R.string.text_detector_title, 45 | R.string.text_detector_body, 46 | TEXT_DETECTOR, 47 | true)) 48 | } 49 | 50 | val firstMLKitApiOption: MLKitApiOption = options[1] as MLKitApiOption 51 | } -------------------------------------------------------------------------------- /app/src/main/java/io/husaynhakeem/mlkit_sample/ui/dialog/ImagePickerDialog.kt: -------------------------------------------------------------------------------- 1 | package io.husaynhakeem.mlkit_sample.ui.dialog 2 | 3 | import android.app.Dialog 4 | import android.content.Context 5 | import android.os.Bundle 6 | import androidx.appcompat.app.AlertDialog 7 | import androidx.fragment.app.DialogFragment 8 | import io.husaynhakeem.mlkit_sample.R 9 | 10 | 11 | class ImagePickerDialog : DialogFragment() { 12 | 13 | private var listener: Listener? = null 14 | 15 | override fun onAttach(context: Context?) { 16 | super.onAttach(context) 17 | if (context is Listener) { 18 | listener = context 19 | } 20 | } 21 | 22 | override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { 23 | return AlertDialog.Builder(context!!) 24 | .setTitle(R.string.image_picker_dialog_title) 25 | .setMessage(R.string.image_picker_dialog_message) 26 | .setPositiveButton(R.string.image_picker_dialog_button_camera, { _, _ -> 27 | listener?.onCameraSelected() 28 | }) 29 | .setNegativeButton(R.string.image_picker_dialog_button_gallery, { _, _ -> 30 | listener?.onGallerySelected() 31 | }) 32 | .create() 33 | } 34 | 35 | companion object { 36 | val TAG = ImagePickerDialog::class.java.simpleName 37 | } 38 | 39 | interface Listener { 40 | fun onCameraSelected() 41 | fun onGallerySelected() 42 | } 43 | } -------------------------------------------------------------------------------- /app/src/main/java/io/husaynhakeem/mlkit_sample/ui/dialog/MLKitApiAboutDialog.kt: -------------------------------------------------------------------------------- 1 | package io.husaynhakeem.mlkit_sample.ui.dialog 2 | 3 | import android.app.Dialog 4 | import android.content.Context 5 | import android.os.Bundle 6 | import androidx.appcompat.app.AlertDialog 7 | import androidx.fragment.app.DialogFragment 8 | import io.husaynhakeem.mlkit_sample.R 9 | 10 | 11 | class MLKitApiAboutDialog : DialogFragment() { 12 | 13 | private var listener: Listener? = null 14 | 15 | override fun onAttach(context: Context?) { 16 | super.onAttach(context) 17 | if (context is Listener) { 18 | listener = context 19 | } 20 | } 21 | 22 | override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { 23 | val title = arguments?.getInt(KEY_TITLE) ?: 0 24 | val body = arguments?.getInt(KEY_BODY) ?: 0 25 | return AlertDialog.Builder(context!!) 26 | .setTitle(title) 27 | .setMessage(body) 28 | .setPositiveButton(R.string.definition_dialog_button_label, { _, _ -> listener?.onDismissed() }) 29 | .create() 30 | } 31 | 32 | companion object { 33 | val TAG = MLKitApiAboutDialog::class.java.simpleName 34 | const val KEY_TITLE = "key_title" 35 | const val KEY_BODY = "key_body" 36 | } 37 | 38 | interface Listener { 39 | fun onDismissed() 40 | } 41 | } -------------------------------------------------------------------------------- /app/src/main/java/io/husaynhakeem/mlkit_sample/ui/recycler/UserOptionViewHolder.kt: -------------------------------------------------------------------------------- 1 | package io.husaynhakeem.mlkit_sample.ui.recycler 2 | 3 | import android.view.LayoutInflater 4 | import android.view.ViewGroup 5 | import androidx.recyclerview.widget.RecyclerView 6 | import io.husaynhakeem.mlkit_sample.R 7 | import io.husaynhakeem.mlkit_sample.core.model.UserOption 8 | import kotlinx.android.synthetic.main.item_user_option.view.* 9 | 10 | class UserOptionViewHolder(parent: ViewGroup, private val onItemClickListener: (UserOption) -> Unit) : 11 | RecyclerView.ViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_user_option, parent, false)) { 12 | 13 | fun bind(option: UserOption) { 14 | itemView.userOptionImageView.setImageResource(option.iconResId) 15 | itemView.setOnClickListener { onItemClickListener.invoke(option) } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /app/src/main/java/io/husaynhakeem/mlkit_sample/ui/recycler/UserOptionsAdapter.kt: -------------------------------------------------------------------------------- 1 | package io.husaynhakeem.mlkit_sample.ui.recycler 2 | 3 | import android.view.ViewGroup 4 | import androidx.recyclerview.widget.RecyclerView 5 | import io.husaynhakeem.mlkit_sample.core.model.UserOption 6 | 7 | class UserOptionsAdapter( 8 | private val options: Array, 9 | private val onItemClickListener: (UserOption) -> Unit) : RecyclerView.Adapter() { 10 | 11 | override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = UserOptionViewHolder(parent, onItemClickListener) 12 | 13 | override fun onBindViewHolder(holder: UserOptionViewHolder, position: Int) { 14 | holder.bind(options[position]) 15 | } 16 | 17 | override fun getItemCount() = options.size 18 | } 19 | -------------------------------------------------------------------------------- /app/src/main/java/io/husaynhakeem/mlkit_sample/ui/usecase/DisplayMLKitAboutDialogUseCase.kt: -------------------------------------------------------------------------------- 1 | package io.husaynhakeem.mlkit_sample.ui.usecase 2 | 3 | import io.husaynhakeem.mlkit_sample.core.model.MLKitApiOption 4 | import io.husaynhakeem.mlkit_sample.core.wrapper.SharedPreferencesWrapper 5 | 6 | 7 | object DisplayMLKitAboutDialogUseCase { 8 | 9 | fun shouldShowAboutDialogFor(option: MLKitApiOption): Boolean { 10 | val isOptionFirstCall = SharedPreferencesWrapper.getBoolean(option.type.name) 11 | if (isOptionFirstCall) { 12 | SharedPreferencesWrapper.put(option.type.name, false) 13 | } 14 | return isOptionFirstCall || !option.isEnabled 15 | } 16 | } -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_barcode_detector.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/husaynhakeem/Android-ML-Kit-Sample/8e8f3766da920ace0f5e9f0e83d95679cb98f935/app/src/main/res/drawable/ic_barcode_detector.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_face_detector.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/husaynhakeem/Android-ML-Kit-Sample/8e8f3766da920ace0f5e9f0e83d95679cb98f935/app/src/main/res/drawable/ic_face_detector.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_github.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/husaynhakeem/Android-ML-Kit-Sample/8e8f3766da920ace0f5e9f0e83d95679cb98f935/app/src/main/res/drawable/ic_github.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_image_labeler.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/husaynhakeem/Android-ML-Kit-Sample/8e8f3766da920ace0f5e9f0e83d95679cb98f935/app/src/main/res/drawable/ic_image_labeler.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_landmark_detector.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/husaynhakeem/Android-ML-Kit-Sample/8e8f3766da920ace0f5e9f0e83d95679cb98f935/app/src/main/res/drawable/ic_landmark_detector.png -------------------------------------------------------------------------------- /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/res/drawable/ic_new_image.xml: -------------------------------------------------------------------------------- 1 | 7 | 10 | 11 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_text_detector.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/husaynhakeem/Android-ML-Kit-Sample/8e8f3766da920ace0f5e9f0e83d95679cb98f935/app/src/main/res/drawable/ic_text_detector.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/white_rounded_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 14 | 15 | 19 | 20 | 24 | 25 | 36 | 37 | 38 | 45 | 46 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /app/src/main/res/layout/item_user_option.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 10 | 11 | 16 | 17 | -------------------------------------------------------------------------------- /app/src/main/res/menu/menu_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 12 | -------------------------------------------------------------------------------- /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/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/husaynhakeem/Android-ML-Kit-Sample/8e8f3766da920ace0f5e9f0e83d95679cb98f935/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/husaynhakeem/Android-ML-Kit-Sample/8e8f3766da920ace0f5e9f0e83d95679cb98f935/app/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/husaynhakeem/Android-ML-Kit-Sample/8e8f3766da920ace0f5e9f0e83d95679cb98f935/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/husaynhakeem/Android-ML-Kit-Sample/8e8f3766da920ace0f5e9f0e83d95679cb98f935/app/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/husaynhakeem/Android-ML-Kit-Sample/8e8f3766da920ace0f5e9f0e83d95679cb98f935/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/husaynhakeem/Android-ML-Kit-Sample/8e8f3766da920ace0f5e9f0e83d95679cb98f935/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/husaynhakeem/Android-ML-Kit-Sample/8e8f3766da920ace0f5e9f0e83d95679cb98f935/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/husaynhakeem/Android-ML-Kit-Sample/8e8f3766da920ace0f5e9f0e83d95679cb98f935/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/husaynhakeem/Android-ML-Kit-Sample/8e8f3766da920ace0f5e9f0e83d95679cb98f935/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/husaynhakeem/Android-ML-Kit-Sample/8e8f3766da920ace0f5e9f0e83d95679cb98f935/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #4688F1 4 | #4688F1 5 | #D81B60 6 | 7 | #90000000 8 | 9 | -------------------------------------------------------------------------------- /app/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 70dp 5 | 16dp 6 | 7 | 12dp 8 | 24dp 9 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | Machine Learning Kit - Sample 3 | mlkit_preferences 4 | 5 | Select an image 6 | Select an image using the 7 | Camera 8 | Gallery 9 | Got it! 10 | 11 | 12 | Choose a new image 13 | 14 | Barcode Detector 15 | With ML Kit\'s barcode scanning API, you can read data encoded using most standard barcode formats. 16 | 17 | Face detector 18 | With ML Kit\'s face detection API, you can detect faces in an image and identify key facial features. 19 | 20 | Image Labeler 21 | With ML Kit\'s image labeling APIs, you can recognize entities in an image without having to provide any additional contextual metadata, using either an on-device API or a cloud-based API. 22 | 23 | Landmark Detector (Disabled) 24 | With ML Kit\'s landmark recognition API, you can recognize well-known landmarks in an image. 25 | 26 | Text Detector 27 | With ML Kit\'s text recognition APIs, you can recognize text in any Latin-based language (and more, with Cloud-based text recognition). 28 | 29 | Github 30 | https://github.com/husaynhakeem 31 | 32 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 8 | 9 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /app/src/test/java/io/husaynhakeem/mlkit_sample/ExampleUnitTest.kt: -------------------------------------------------------------------------------- 1 | package io.husaynhakeem.mlkit_sample 2 | 3 | import org.junit.Test 4 | 5 | import org.junit.Assert.* 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * See [testing documentation](http://d.android.com/tools/testing). 11 | */ 12 | class ExampleUnitTest { 13 | @Test 14 | fun addition_isCorrect() { 15 | assertEquals(4, 2 + 2) 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | ext.kotlin_version = '1.2.41' 5 | repositories { 6 | google() 7 | jcenter() 8 | } 9 | dependencies { 10 | classpath 'com.android.tools.build:gradle:3.2.0-alpha14' 11 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 12 | classpath 'com.google.gms:google-services:3.3.1' 13 | } 14 | } 15 | 16 | allprojects { 17 | repositories { 18 | google() 19 | jcenter() 20 | maven { url "https://jitpack.io" } 21 | } 22 | } 23 | 24 | task clean(type: Delete) { 25 | delete rootProject.buildDir 26 | } 27 | 28 | ext { 29 | supportLibraryVersion = '27.1.1' 30 | constraintLayoutVersion = '1.1.0' 31 | firebaseCoreVersion = '15.0.2' 32 | firebaseMLKitVersion = '15.0.0' 33 | navigationVersion = '1.0.0-alpha01' 34 | materialVersion = '1.0.0-alpha1' 35 | imagePickerVersion = '1.12.0' 36 | } 37 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | # IDE (e.g. Android Studio) users: 3 | # Gradle settings configured through the IDE *will override* 4 | # any settings specified in this file. 5 | # For more details on how to configure your build environment visit 6 | # http://www.gradle.org/docs/current/userguide/build_environment.html 7 | # Specifies the JVM arguments used for the daemon process. 8 | # The setting is particularly useful for tweaking memory settings. 9 | android.enableJetifier=true 10 | android.useAndroidX=true 11 | org.gradle.jvmargs=-Xmx1536m 12 | # When configured, Gradle will run in incubating parallel mode. 13 | # This option should only be used with decoupled projects. More details, visit 14 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 15 | # org.gradle.parallel=true 16 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/husaynhakeem/Android-ML-Kit-Sample/8e8f3766da920ace0f5e9f0e83d95679cb98f935/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn () { 37 | echo "$*" 38 | } 39 | 40 | die () { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Escape application args 158 | save () { 159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 160 | echo " " 161 | } 162 | APP_ARGS=$(save "$@") 163 | 164 | # Collect all arguments for the java command, following the shell quoting and substitution rules 165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 166 | 167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 169 | cd "$(dirname "$0")" 170 | fi 171 | 172 | exec "$JAVACMD" "$@" 173 | -------------------------------------------------------------------------------- /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 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 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 Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app' 2 | --------------------------------------------------------------------------------