Skip to content

Commit ee16d78

Browse files
committed
Camera2Extensions: Initial commit
Change-Id: I571f12252ce4e0b9fbbd69d6f1a96ac03c76eb88
1 parent 852767f commit ee16d78

File tree

29 files changed

+1677
-0
lines changed

29 files changed

+1677
-0
lines changed

Camera2Extensions/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
utils/
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Copyright (C) 2021 The Android Open Source Project
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
# GOOGLE SAMPLE PACKAGING DATA
16+
#
17+
# This file is used by Google as part of our samples packaging process.
18+
# End users may safely ignore this file. It has no relevance to other systems.
19+
---
20+
status: PUBLISHED
21+
technologies: [Android]
22+
categories: [Camera]
23+
languages: [Kotlin]
24+
solutions: [Mobile]
25+
github: android/camera-samples
26+
level: INTERMEDIATE
27+
apiRefs:
28+
- android:android.hardware.camera2
29+
license: apache2

Camera2Extensions/README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
Android Camera2Extensions Sample
3+
===========================
4+
5+
This sample captures [Camera2][1] extensions generated images via the Camera2 API
6+
including displaying a camera preview and using repeating capture requests.
7+
This sample application can only be built and used on Android SDK 31+.
8+
9+
Introduction
10+
------------
11+
This sample displays a live camera preview in TextureView and allows the user to
12+
capture a still image using the Camera Extension [API][2]
13+
14+
[1]: https://developer.android.com/reference/android/hardware/camera2/package-summary.html
15+
[2]: https://developer.android.com/reference/android/hardware/camera2/CameraExtensionCharacteristics
16+
17+
Pre-requisites
18+
--------------
19+
20+
- Android SDK 31+
21+
- Android Studio 3.6+
22+
- Device with CameraX/Camera2 extensions
23+
24+
Getting Started
25+
---------------
26+
27+
This sample uses the Gradle build system. To build this project, use the
28+
"gradlew build" command or use "Import Project" in Android Studio.
29+
30+
Support
31+
-------
32+
33+
- Stack Overflow: http://stackoverflow.com/questions/tagged/android
34+
35+
If you've found an error in this sample, please file an issue:
36+
https://github.com/android/camera-samples
37+
38+
Patches are encouraged, and may be submitted by forking this project and
39+
submitting a pull request through GitHub. Please see CONTRIBUTING.md for more details.

Camera2Extensions/app/build.gradle

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* Copyright 2021 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
apply plugin: 'com.android.application'
18+
apply plugin: 'kotlin-android'
19+
apply plugin: 'kotlin-kapt'
20+
apply plugin: 'kotlin-android-extensions'
21+
apply plugin: "androidx.navigation.safeargs"
22+
23+
android {
24+
compileSdkVersion 'android-S'
25+
defaultConfig {
26+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
27+
applicationId "com.android.example.camera2.extensions"
28+
minSdkVersion 31
29+
targetSdkVersion 31
30+
versionCode 1
31+
versionName "1.0.0"
32+
}
33+
34+
compileOptions {
35+
sourceCompatibility java_version
36+
targetCompatibility rootProject.ext.java_version
37+
}
38+
39+
kotlinOptions {
40+
jvmTarget = "$rootProject.ext.java_version"
41+
}
42+
43+
buildTypes {
44+
release {
45+
minifyEnabled false
46+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
47+
}
48+
}
49+
50+
// Set the source of tests to same for both Unit and Instrumented tests
51+
sourceSets {
52+
String sharedTestDir = 'src/test/java'
53+
test {
54+
java.srcDir sharedTestDir
55+
}
56+
androidTest {
57+
java.srcDir sharedTestDir
58+
}
59+
}
60+
buildToolsVersion '30.0.3'
61+
62+
viewBinding {
63+
enabled true
64+
}
65+
}
66+
67+
dependencies {
68+
implementation project(':utils')
69+
70+
implementation "androidx.activity:activity-ktx:1.2.0"
71+
implementation "androidx.fragment:fragment:1.3.0"
72+
73+
// Kotlin lang
74+
implementation 'androidx.core:core-ktx:1.3.0'
75+
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
76+
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.4'
77+
78+
// App compat and UI things
79+
implementation 'androidx.appcompat:appcompat:1.1.0'
80+
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.2.0'
81+
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
82+
83+
// Navigation library
84+
def nav_version = "2.2.2"
85+
implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
86+
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
87+
88+
// Unit testing
89+
testImplementation 'androidx.test.ext:junit:1.1.1'
90+
testImplementation 'androidx.test:rules:1.2.0'
91+
testImplementation 'androidx.test:runner:1.2.0'
92+
testImplementation 'androidx.test.espresso:espresso-core:3.2.0'
93+
testImplementation "org.robolectric:robolectric:4.3.1"
94+
95+
// Instrumented testing
96+
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
97+
androidTestImplementation 'androidx.test:rules:1.2.0'
98+
androidTestImplementation 'androidx.test:runner:1.2.0'
99+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
100+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Copyright 2021 The Android Open Source Project
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ https://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
-->
17+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
18+
xmlns:tools="http://schemas.android.com/tools"
19+
package="com.example.android.camera2.extensions">
20+
21+
<!-- Permission declarations -->
22+
<uses-permission android:name="android.permission.CAMERA" />
23+
24+
<uses-feature android:name="android.hardware.camera.any" />
25+
26+
<application
27+
android:allowBackup="true"
28+
android:label="@string/app_name"
29+
android:icon="@drawable/ic_launcher"
30+
android:exported="true"
31+
tools:ignore="AllowBackup,GoogleAppIndexingWarning">
32+
33+
<activity
34+
android:name="com.example.android.camera2.extensions.CameraActivity"
35+
android:exported="true"
36+
android:clearTaskOnLaunch="true"
37+
android:theme="@style/AppTheme">
38+
39+
<!-- Main app intent filter -->
40+
<intent-filter>
41+
<action android:name="android.intent.action.MAIN" />
42+
<category android:name="android.intent.category.LAUNCHER" />
43+
</intent-filter>
44+
45+
</activity>
46+
</application>
47+
48+
</manifest>
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright (C) 2021 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
18+
package com.example.android.camera2.extensions
19+
20+
import android.Manifest
21+
import android.content.pm.PackageManager
22+
import android.os.Bundle
23+
import android.view.View
24+
import android.widget.FrameLayout
25+
import android.widget.Toast
26+
import androidx.appcompat.app.AppCompatActivity
27+
28+
import androidx.activity.result.contract.ActivityResultContracts
29+
import androidx.core.content.ContextCompat
30+
31+
/*
32+
* This is the launching point for the camera extension app where the camera fragment container
33+
* is delayed to allow the UI to settle. All of the camera extension code can be found in
34+
* "CameraFragment".
35+
*/
36+
class CameraActivity : AppCompatActivity() {
37+
38+
private lateinit var container: FrameLayout
39+
40+
private val requestPermissionLauncher =
41+
registerForActivityResult(
42+
ActivityResultContracts.RequestPermission()
43+
) { isGranted: Boolean ->
44+
if (isGranted) {
45+
// Before setting full screen flags, we must wait a bit to let UI settle; otherwise,
46+
// we may be trying to set app to immersive mode before it's ready and the flags do
47+
// not stick
48+
container.postDelayed({container.systemUiVisibility = FLAGS_FULLSCREEN},
49+
IMMERSIVE_FLAG_TIMEOUT)
50+
} else {
51+
Toast.makeText(this, R.string.permission_required,
52+
Toast.LENGTH_SHORT).show()
53+
finish()
54+
}
55+
}
56+
57+
override fun onCreate(savedInstanceState: Bundle?) {
58+
super.onCreate(savedInstanceState)
59+
setContentView(R.layout.activity_camera)
60+
container = findViewById(R.id.fragment_container)
61+
}
62+
63+
override fun onResume() {
64+
super.onResume()
65+
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) ==
66+
PackageManager.PERMISSION_GRANTED) {
67+
container.postDelayed({container.systemUiVisibility = FLAGS_FULLSCREEN},
68+
IMMERSIVE_FLAG_TIMEOUT)
69+
} else {
70+
requestPermissionLauncher.launch(Manifest.permission.CAMERA)
71+
}
72+
}
73+
74+
companion object {
75+
/** Combination of all flags required to put activity into immersive mode */
76+
const val FLAGS_FULLSCREEN =
77+
View.SYSTEM_UI_FLAG_LOW_PROFILE or
78+
View.SYSTEM_UI_FLAG_FULLSCREEN or
79+
View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
80+
View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
81+
82+
/** Milliseconds used for UI animations */
83+
private const val IMMERSIVE_FLAG_TIMEOUT = 500L
84+
}
85+
}

0 commit comments

Comments
 (0)