├── .gitignore ├── .idea ├── .name ├── codeStyles │ ├── Project.xml │ └── codeStyleConfig.xml ├── gradle.xml ├── misc.xml └── runConfigurations.xml ├── LICENSE ├── README.md ├── apk └── app-debug.apk ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── app │ │ └── cameraxview │ │ └── ExampleInstrumentedTest.kt │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── app │ │ │ └── cameraxview │ │ │ └── MainActivity.kt │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ └── ic_launcher_background.xml │ │ ├── layout │ │ └── activity_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 │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── app │ └── cameraxview │ └── ExampleUnitTest.kt ├── art └── device_art.jpg ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/caches 5 | /.idea/libraries 6 | /.idea/modules.xml 7 | /.idea/workspace.xml 8 | /.idea/navEditor.xml 9 | /.idea/assetWizardSettings.xml 10 | .DS_Store 11 | /build 12 | /captures 13 | .externalNativeBuild 14 | .cxx 15 | -------------------------------------------------------------------------------- /.idea/.name: -------------------------------------------------------------------------------- 1 | CameraXComplete -------------------------------------------------------------------------------- /.idea/codeStyles/Project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | 7 | 8 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | xmlns:android 17 | 18 | ^$ 19 | 20 | 21 | 22 |
23 |
24 | 25 | 26 | 27 | xmlns:.* 28 | 29 | ^$ 30 | 31 | 32 | BY_NAME 33 | 34 |
35 |
36 | 37 | 38 | 39 | .*:id 40 | 41 | http://schemas.android.com/apk/res/android 42 | 43 | 44 | 45 |
46 |
47 | 48 | 49 | 50 | .*:name 51 | 52 | http://schemas.android.com/apk/res/android 53 | 54 | 55 | 56 |
57 |
58 | 59 | 60 | 61 | name 62 | 63 | ^$ 64 | 65 | 66 | 67 |
68 |
69 | 70 | 71 | 72 | style 73 | 74 | ^$ 75 | 76 | 77 | 78 |
79 |
80 | 81 | 82 | 83 | .* 84 | 85 | ^$ 86 | 87 | 88 | BY_NAME 89 | 90 |
91 |
92 | 93 | 94 | 95 | .* 96 | 97 | http://schemas.android.com/apk/res/android 98 | 99 | 100 | ANDROID_ATTRIBUTE_ORDER 101 | 102 |
103 |
104 | 105 | 106 | 107 | .* 108 | 109 | .* 110 | 111 | 112 | BY_NAME 113 | 114 |
115 |
116 |
117 |
118 | 119 | 121 |
122 |
-------------------------------------------------------------------------------- /.idea/codeStyles/codeStyleConfig.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 19 | 20 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 9 | 10 | 11 | 12 | 13 | 1.8 14 | 15 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Baljeet 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 | # CameraXView 2 | Demo for CameraX View Library. 3 | 4 | ![App Ui](https://github.com/iambaljeet/CameraXView/blob/master/art/device_art.jpg) 5 | 6 | ## Get Started 7 | 8 | 1. Clone Repository using 'https://github.com/iambaljeet/CameraXView.git' 9 | 2. Goto File >> Import Project >> CameraXView 10 | -------------------------------------------------------------------------------- /apk/app-debug.apk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iambaljeet/CameraXView/fea43f16280a21f39536ab557112b360bb48f392/apk/app-debug.apk -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /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 29 7 | buildToolsVersion "29.0.3" 8 | 9 | defaultConfig { 10 | applicationId "com.app.cameraxview" 11 | minSdkVersion 21 12 | targetSdkVersion 29 13 | versionCode 1 14 | versionName "1.0" 15 | 16 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 17 | } 18 | 19 | buildTypes { 20 | release { 21 | minifyEnabled false 22 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 23 | } 24 | } 25 | compileOptions { 26 | sourceCompatibility JavaVersion.VERSION_1_8 27 | targetCompatibility JavaVersion.VERSION_1_8 28 | } 29 | kotlinOptions { 30 | jvmTarget = JavaVersion.VERSION_1_8.toString() 31 | } 32 | } 33 | 34 | dependencies { 35 | implementation fileTree(dir: 'libs', include: ['*.jar']) 36 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" 37 | implementation 'androidx.appcompat:appcompat:1.1.0' 38 | implementation 'androidx.core:core-ktx:1.2.0' 39 | implementation 'androidx.constraintlayout:constraintlayout:1.1.3' 40 | testImplementation 'junit:junit:4.13' 41 | androidTestImplementation 'androidx.test.ext:junit:1.1.1' 42 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' 43 | 44 | // CameraX core library 45 | def camerax_version = "1.0.0-beta01" 46 | implementation "androidx.camera:camera-core:$camerax_version" 47 | 48 | // CameraX Camera2 extensions 49 | implementation "androidx.camera:camera-camera2:$camerax_version" 50 | 51 | // CameraX Lifecycle library 52 | implementation "androidx.camera:camera-lifecycle:$camerax_version" 53 | 54 | // CameraX View class 55 | implementation "androidx.camera:camera-view:1.0.0-alpha08" 56 | } 57 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/app/cameraxview/ExampleInstrumentedTest.kt: -------------------------------------------------------------------------------- 1 | package com.app.cameraxview 2 | 3 | import androidx.test.platform.app.InstrumentationRegistry 4 | import androidx.test.ext.junit.runners.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.getInstrumentation().targetContext 22 | assertEquals("com.app.cameraxview", appContext.packageName) 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /app/src/main/java/com/app/cameraxview/MainActivity.kt: -------------------------------------------------------------------------------- 1 | package com.app.cameraxview 2 | 3 | import android.Manifest 4 | import android.content.pm.PackageManager 5 | import android.os.Bundle 6 | import android.os.Environment 7 | import android.util.Log 8 | import android.widget.Toast 9 | import androidx.appcompat.app.AlertDialog 10 | import androidx.appcompat.app.AppCompatActivity 11 | import androidx.camera.core.ImageCapture 12 | import androidx.camera.core.ImageCaptureException 13 | import androidx.camera.core.VideoCapture 14 | import androidx.core.app.ActivityCompat 15 | import androidx.core.content.ContextCompat 16 | import kotlinx.android.synthetic.main.activity_main.* 17 | import java.io.File 18 | 19 | class MainActivity : AppCompatActivity() { 20 | val TAG = MainActivity::class.java.simpleName 21 | var isRecording = false 22 | 23 | var CAMERA_PERMISSION = Manifest.permission.CAMERA 24 | var RECORD_AUDIO_PERMISSION = Manifest.permission.RECORD_AUDIO 25 | 26 | var RC_PERMISSION = 101 27 | 28 | override fun onCreate(savedInstanceState: Bundle?) { 29 | super.onCreate(savedInstanceState) 30 | setContentView(R.layout.activity_main) 31 | 32 | val recordFiles = ContextCompat.getExternalFilesDirs(this, Environment.DIRECTORY_MOVIES) 33 | val storageDirectory = recordFiles[0] 34 | val videoRecordingFilePath = "${storageDirectory.absoluteFile}/${System.currentTimeMillis()}_video.mp4" 35 | val imageCaptureFilePath = "${storageDirectory.absoluteFile}/${System.currentTimeMillis()}_image.jpg" 36 | 37 | if (checkPermissions()) startCameraSession() else requestPermissions() 38 | 39 | video_record.setOnClickListener { 40 | if (isRecording) { 41 | isRecording = false 42 | video_record.text = "Record Video" 43 | Toast.makeText(this, "Recording Stopped", Toast.LENGTH_SHORT).show() 44 | camera_view.stopRecording() 45 | } else { 46 | isRecording = true 47 | video_record.text = "Stop Recording" 48 | Toast.makeText(this, "Recording Started", Toast.LENGTH_SHORT).show() 49 | recordVideo(videoRecordingFilePath) 50 | } 51 | } 52 | 53 | capture_image.setOnClickListener { 54 | captureImage(imageCaptureFilePath) 55 | } 56 | } 57 | 58 | private fun requestPermissions() { 59 | ActivityCompat.requestPermissions(this, arrayOf(CAMERA_PERMISSION, RECORD_AUDIO_PERMISSION), RC_PERMISSION) 60 | } 61 | 62 | private fun checkPermissions(): Boolean { 63 | return ((ActivityCompat.checkSelfPermission(this, CAMERA_PERMISSION)) == PackageManager.PERMISSION_GRANTED 64 | && (ActivityCompat.checkSelfPermission(this, CAMERA_PERMISSION)) == PackageManager.PERMISSION_GRANTED) 65 | } 66 | 67 | override fun onRequestPermissionsResult(requestCode: Int, permissions: Array, grantResults: IntArray) { 68 | super.onRequestPermissionsResult(requestCode, permissions, grantResults) 69 | when(requestCode) { 70 | RC_PERMISSION -> { 71 | var allPermissionsGranted = false 72 | for (result in grantResults) { 73 | if (result != PackageManager.PERMISSION_GRANTED) { 74 | allPermissionsGranted = false 75 | break 76 | } else { 77 | allPermissionsGranted = true 78 | } 79 | } 80 | if (allPermissionsGranted) startCameraSession() else permissionsNotGranted() 81 | } 82 | } 83 | } 84 | 85 | private fun startCameraSession() { 86 | camera_view.bindToLifecycle(this) 87 | } 88 | 89 | private fun permissionsNotGranted() { 90 | AlertDialog.Builder(this).setTitle("Permissions required") 91 | .setMessage("These permissions are required to use this app. Please allow Camera and Audio permissions first") 92 | .setCancelable(false) 93 | .setPositiveButton("Grant") { dialog, which -> requestPermissions() } 94 | .show() 95 | } 96 | 97 | private fun recordVideo(videoRecordingFilePath: String) { 98 | camera_view.startRecording(File(videoRecordingFilePath), ContextCompat.getMainExecutor(this), object: VideoCapture.OnVideoSavedCallback { 99 | override fun onVideoSaved(file: File) { 100 | Toast.makeText(this@MainActivity, "Recording Saved", Toast.LENGTH_SHORT).show() 101 | Log.d(TAG, "onVideoSaved $videoRecordingFilePath") 102 | } 103 | 104 | override fun onError(videoCaptureError: Int, message: String, cause: Throwable?) { 105 | Toast.makeText(this@MainActivity, "Recording Failed", Toast.LENGTH_SHORT).show() 106 | Log.e(TAG, "onError $videoCaptureError $message") 107 | } 108 | }) 109 | } 110 | 111 | private fun captureImage(imageCaptureFilePath: String) { 112 | camera_view.takePicture(File(imageCaptureFilePath), ContextCompat.getMainExecutor(this), object: ImageCapture.OnImageSavedCallback { 113 | override fun onImageSaved(outputFileResults: ImageCapture.OutputFileResults) { 114 | Toast.makeText(this@MainActivity, "Image Captured", Toast.LENGTH_SHORT).show() 115 | Log.d(TAG, "onImageSaved $imageCaptureFilePath") 116 | } 117 | 118 | override fun onError(exception: ImageCaptureException) { 119 | Toast.makeText(this@MainActivity, "Image Capture Failed", Toast.LENGTH_SHORT).show() 120 | Log.e(TAG, "onError $exception") 121 | } 122 | }) 123 | } 124 | } 125 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | 15 | 18 | 21 | 22 | 23 | 24 | 30 | -------------------------------------------------------------------------------- /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/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 22 | 23 |