├── settings.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── app ├── src │ ├── main │ │ ├── res │ │ │ ├── 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 │ │ │ ├── drawable │ │ │ │ ├── shape_zoom_level_background.xml │ │ │ │ ├── ic_flash_on.xml │ │ │ │ ├── ic_flash_off.xml │ │ │ │ ├── ic_flash_auto.xml │ │ │ │ ├── ic_torch_off.xml │ │ │ │ ├── ic_switch_camera.xml │ │ │ │ ├── ic_camera_action.xml │ │ │ │ ├── ic_torch_on.xml │ │ │ │ └── ic_launcher_background.xml │ │ │ ├── mipmap-anydpi-v26 │ │ │ │ ├── ic_launcher.xml │ │ │ │ └── ic_launcher_round.xml │ │ │ ├── values │ │ │ │ ├── colors.xml │ │ │ │ ├── dimens.xml │ │ │ │ ├── styles.xml │ │ │ │ └── strings.xml │ │ │ ├── layout │ │ │ │ ├── activity_luminosity_analyzer.xml │ │ │ │ ├── activity_menu.xml │ │ │ │ ├── activity_image_capture.xml │ │ │ │ ├── activity_video_capture.xml │ │ │ │ ├── activity_camera_view.xml │ │ │ │ └── activity_advance_image_capture.xml │ │ │ └── drawable-v24 │ │ │ │ └── ic_launcher_foreground.xml │ │ ├── java │ │ │ └── com │ │ │ │ └── akexorcist │ │ │ │ └── example │ │ │ │ └── camerax │ │ │ │ ├── helper │ │ │ │ ├── ShortenSeekBarChangeListener.kt │ │ │ │ ├── ShortenMultiplePermissionListener.kt │ │ │ │ ├── ViewExtension.kt │ │ │ │ └── ShortenPermissionListener.kt │ │ │ │ ├── LuminosityAnalyzer.kt │ │ │ │ └── ui │ │ │ │ ├── MenuActivity.kt │ │ │ │ ├── LuminosityAnalyzerActivity.kt │ │ │ │ ├── ImageCaptureActivity.kt │ │ │ │ ├── VideoCaptureActivity.kt │ │ │ │ ├── CameraViewActivity.kt │ │ │ │ └── AdvanceImageCaptureActivity.kt │ │ └── AndroidManifest.xml │ ├── test │ │ └── java │ │ │ └── com │ │ │ └── akexorcist │ │ │ └── example │ │ │ └── camerax │ │ │ └── ExampleUnitTest.kt │ └── androidTest │ │ └── java │ │ └── com │ │ └── akexorcist │ │ └── example │ │ └── camerax │ │ └── ExampleInstrumentedTest.kt ├── proguard-rules.pro └── build.gradle ├── README.md ├── gradle.properties ├── .gitignore ├── gradlew.bat ├── gradlew └── LICENSE /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app' 2 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/akexorcist/CameraX-Sample/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/akexorcist/CameraX-Sample/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/akexorcist/CameraX-Sample/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/akexorcist/CameraX-Sample/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/akexorcist/CameraX-Sample/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/akexorcist/CameraX-Sample/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/akexorcist/CameraX-Sample/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/akexorcist/CameraX-Sample/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/akexorcist/CameraX-Sample/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/akexorcist/CameraX-Sample/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/akexorcist/CameraX-Sample/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/shape_zoom_level_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Tue Dec 01 02:19:39 ICT 2020 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-6.5-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/ic_flash_on.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_flash_off.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_flash_auto.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_torch_off.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /app/src/test/java/com/akexorcist/example/camerax/ExampleUnitTest.kt: -------------------------------------------------------------------------------- 1 | package com.akexorcist.example.camerax 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 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #008577 4 | #00574B 5 | #D81B60 6 | #FFFFFF 7 | #000000 8 | #111111 9 | #77000000 10 | #77FFFFFF 11 | 12 | -------------------------------------------------------------------------------- /app/src/main/java/com/akexorcist/example/camerax/helper/ShortenSeekBarChangeListener.kt: -------------------------------------------------------------------------------- 1 | package com.akexorcist.example.camerax.helper 2 | 3 | import android.widget.SeekBar 4 | 5 | open class ShortenSeekBarChangeListener : SeekBar.OnSeekBarChangeListener { 6 | override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) { 7 | } 8 | 9 | override fun onStartTrackingTouch(seekBar: SeekBar?) { 10 | } 11 | 12 | override fun onStopTrackingTouch(seekBar: SeekBar?) { 13 | } 14 | } -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_switch_camera.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 8dp 4 | 8dp 5 | 80dp 6 | 18sp 7 | 4dp 8 | 150dp 9 | 240dp 10 | 320dp 11 | 40dp 12 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_camera_action.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_torch_on.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Android-CameraX 2 | Sample code for CameraX in Android Jetpack Library 3 | 4 | ### What's in this project? 5 | 6 | • Built-in CameraView implementation 7 | 8 | • Image Analyzer implementation 9 | 10 | • Image Capture implementation 11 | 12 | • Advance Image Capture implementation (image capture, camera switching, flash, torch mode, zoom level, tap to focus) 13 | 14 | • Video Capture implementation (Incomplete) 15 | 16 | ### CameraX dependencies list 17 | 18 | ``` 19 | implementation "androidx.camera:camera-camera2:1.0.0-beta01" 20 | implementation "androidx.camera:camera-lifecycle:1.0.0-beta01" 21 | implementation "androidx.camera:camera-view:1.0.0-alpha08" 22 | ``` 23 | 24 | 25 | -------------------------------------------------------------------------------- /app/src/main/java/com/akexorcist/example/camerax/helper/ShortenMultiplePermissionListener.kt: -------------------------------------------------------------------------------- 1 | package com.akexorcist.example.camerax.helper 2 | 3 | import com.karumi.dexter.MultiplePermissionsReport 4 | import com.karumi.dexter.PermissionToken 5 | import com.karumi.dexter.listener.PermissionRequest 6 | import com.karumi.dexter.listener.multi.MultiplePermissionsListener 7 | 8 | open class ShortenMultiplePermissionListener : MultiplePermissionsListener { 9 | override fun onPermissionsChecked(report: MultiplePermissionsReport) { 10 | } 11 | 12 | override fun onPermissionRationaleShouldBeShown(permissions: List?, token: PermissionToken?) { 13 | token?.continuePermissionRequest() 14 | } 15 | } -------------------------------------------------------------------------------- /app/src/main/java/com/akexorcist/example/camerax/helper/ViewExtension.kt: -------------------------------------------------------------------------------- 1 | package com.akexorcist.example.camerax.helper 2 | 3 | import android.view.View 4 | import androidx.constraintlayout.widget.ConstraintLayout 5 | 6 | // Add extra spacing for translucent status bar and navigation bar 7 | fun View.applyWindowInserts() { 8 | setOnApplyWindowInsetsListener { view, insets -> 9 | val topSize = insets.systemWindowInsetTop 10 | val bottomSize = insets.systemWindowInsetBottom 11 | val params = view.layoutParams as ConstraintLayout.LayoutParams 12 | params.setMargins(params.leftMargin, params.topMargin + topSize, params.rightMargin, params.bottomMargin + bottomSize) 13 | view.layoutParams = params 14 | insets 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/akexorcist/example/camerax/ExampleInstrumentedTest.kt: -------------------------------------------------------------------------------- 1 | package com.akexorcist.example.camerax 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("com.akexorcist.example.camerax", appContext.packageName) 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /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/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 9 | 10 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /app/src/main/java/com/akexorcist/example/camerax/LuminosityAnalyzer.kt: -------------------------------------------------------------------------------- 1 | package com.akexorcist.example.camerax 2 | 3 | import androidx.camera.core.ImageAnalysis 4 | import androidx.camera.core.ImageProxy 5 | import java.nio.ByteBuffer 6 | 7 | class LuminosityAnalyzer(private val onLuminosityUpdated: ((Double) -> Unit)?) : ImageAnalysis.Analyzer { 8 | override fun analyze(image: ImageProxy) { 9 | val buffer = image.planes[0].buffer 10 | val data = buffer.toByteArray() 11 | val pixels = data.map { it.toInt() and 0xFF } 12 | val luma = pixels.average() 13 | onLuminosityUpdated?.invoke(luma) 14 | image.close() 15 | } 16 | 17 | private fun ByteBuffer.toByteArray(): ByteArray { 18 | rewind() 19 | val data = ByteArray(remaining()) 20 | get(data) 21 | return data 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /app/src/main/java/com/akexorcist/example/camerax/helper/ShortenPermissionListener.kt: -------------------------------------------------------------------------------- 1 | package com.akexorcist.example.camerax.helper 2 | 3 | import com.karumi.dexter.PermissionToken 4 | import com.karumi.dexter.listener.PermissionDeniedResponse 5 | import com.karumi.dexter.listener.PermissionGrantedResponse 6 | import com.karumi.dexter.listener.PermissionRequest 7 | import com.karumi.dexter.listener.single.PermissionListener 8 | 9 | open class ShortenPermissionListener : PermissionListener { 10 | override fun onPermissionGranted(response: PermissionGrantedResponse?) { 11 | } 12 | 13 | override fun onPermissionRationaleShouldBeShown( 14 | permission: PermissionRequest?, 15 | token: PermissionToken? 16 | ) { 17 | token?.continuePermissionRequest() 18 | } 19 | 20 | override fun onPermissionDenied(response: PermissionDeniedResponse?) { 21 | } 22 | } -------------------------------------------------------------------------------- /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 | org.gradle.jvmargs=-Xmx1536m 10 | # When configured, Gradle will run in incubating parallel mode. 11 | # This option should only be used with decoupled projects. More details, visit 12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 13 | # org.gradle.parallel=true 14 | # AndroidX package structure to make it clearer which packages are bundled with the 15 | # Android operating system, and which are packaged with your app's APK 16 | # https://developer.android.com/topic/libraries/support-library/androidx-rn 17 | android.useAndroidX=true 18 | # Automatically convert third-party libraries to use AndroidX 19 | android.enableJetifier=true 20 | # Kotlin code style for this project: "official" or "obsolete": 21 | kotlin.code.style=official 22 | -------------------------------------------------------------------------------- /app/src/main/java/com/akexorcist/example/camerax/ui/MenuActivity.kt: -------------------------------------------------------------------------------- 1 | package com.akexorcist.example.camerax.ui 2 | 3 | import android.content.Intent 4 | import android.os.Bundle 5 | import androidx.appcompat.app.AppCompatActivity 6 | import com.akexorcist.example.camerax.databinding.ActivityMenuBinding 7 | 8 | class MenuActivity : AppCompatActivity() { 9 | private val binding: ActivityMenuBinding by lazy { 10 | ActivityMenuBinding.inflate(layoutInflater) 11 | } 12 | 13 | override fun onCreate(savedInstanceState: Bundle?) { 14 | super.onCreate(savedInstanceState) 15 | setContentView(binding.root) 16 | 17 | binding.buttonBuiltInCameraView.setOnClickListener { 18 | startActivity(Intent(this, CameraViewActivity::class.java)) 19 | } 20 | 21 | binding.buttonImageAnalyzer.setOnClickListener { 22 | startActivity(Intent(this, LuminosityAnalyzerActivity::class.java)) 23 | } 24 | 25 | binding.buttonImageCapture.setOnClickListener { 26 | startActivity(Intent(this, ImageCaptureActivity::class.java)) 27 | } 28 | 29 | binding.buttonAdvanceImageCapture.setOnClickListener { 30 | startActivity(Intent(this, AdvanceImageCaptureActivity::class.java)) 31 | } 32 | } 33 | } -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | CameraX 3 | Built-in Camera View 4 | Image Analyzer 5 | Image Capture 6 | Advance Image Capture 7 | Captured 8 | Image capture error with %1$s (%2$d) 9 | Record 10 | Video record error with %1$s (%2$d) 11 | Capture 12 | Record 13 | Stop 14 | Switch 15 | On 16 | Off 17 | Auto 18 | On 19 | Off 20 | Zoom 21 | Luma : %1$.2f 22 | Zoom %1$.1fx 23 | Please grant all camera-related permission 24 | 25 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_luminosity_analyzer.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 17 | 18 | 29 | 30 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_menu.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 |