├── app
├── .gitignore
├── src
│ ├── main
│ │ ├── btn_camera-web.png
│ │ ├── btn_timelapse-web.png
│ │ ├── btn_video_busy-web.png
│ │ ├── btn_video_online-web.png
│ │ ├── res
│ │ │ ├── mipmap-hdpi
│ │ │ │ ├── btn_camera.png
│ │ │ │ ├── btn_timelapse.png
│ │ │ │ ├── ic_launcher.png
│ │ │ │ ├── btn_video_busy.png
│ │ │ │ └── btn_video_online.png
│ │ │ ├── mipmap-mdpi
│ │ │ │ ├── btn_camera.png
│ │ │ │ ├── btn_timelapse.png
│ │ │ │ ├── ic_launcher.png
│ │ │ │ ├── btn_video_busy.png
│ │ │ │ └── btn_video_online.png
│ │ │ ├── mipmap-xhdpi
│ │ │ │ ├── btn_camera.png
│ │ │ │ ├── ic_launcher.png
│ │ │ │ ├── btn_timelapse.png
│ │ │ │ ├── btn_video_busy.png
│ │ │ │ └── btn_video_online.png
│ │ │ ├── mipmap-xxhdpi
│ │ │ │ ├── btn_camera.png
│ │ │ │ ├── ic_launcher.png
│ │ │ │ ├── btn_timelapse.png
│ │ │ │ ├── btn_video_busy.png
│ │ │ │ └── btn_video_online.png
│ │ │ ├── mipmap-xxxhdpi
│ │ │ │ ├── btn_camera.png
│ │ │ │ ├── ic_launcher.png
│ │ │ │ ├── btn_timelapse.png
│ │ │ │ ├── btn_video_busy.png
│ │ │ │ └── btn_video_online.png
│ │ │ ├── values
│ │ │ │ ├── strings.xml
│ │ │ │ ├── colors.xml
│ │ │ │ ├── dimens.xml
│ │ │ │ └── styles.xml
│ │ │ ├── values-w820dp
│ │ │ │ └── dimens.xml
│ │ │ └── layout
│ │ │ │ └── activity_camera2_video_image.xml
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ │ └── mobapptut
│ │ │ └── com
│ │ │ └── camera2videoimage
│ │ │ └── Camera2VideoImageActivity.java
│ ├── test
│ │ └── java
│ │ │ └── mobapptut
│ │ │ └── com
│ │ │ └── camera2videoimage
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── mobapptut
│ │ └── com
│ │ └── camera2videoimage
│ │ └── ApplicationTest.java
├── build.gradle
├── proguard-rules.pro
└── app.iml
├── settings.gradle
├── .idea
├── copyright
│ └── profiles_settings.xml
├── vcs.xml
├── modules.xml
├── runConfigurations.xml
└── compiler.xml
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .gitignore
├── README.md
├── gradle.properties
├── Camera2VideoImage.iml
├── LICENSE.md
├── android_manifest_permission_camera.patch
├── gradlew.bat
└── gradlew
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app'
2 |
--------------------------------------------------------------------------------
/.idea/copyright/profiles_settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/app/src/main/btn_camera-web.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mobapptuts/android_camera2_api_video_app/HEAD/app/src/main/btn_camera-web.png
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mobapptuts/android_camera2_api_video_app/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/app/src/main/btn_timelapse-web.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mobapptuts/android_camera2_api_video_app/HEAD/app/src/main/btn_timelapse-web.png
--------------------------------------------------------------------------------
/app/src/main/btn_video_busy-web.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mobapptuts/android_camera2_api_video_app/HEAD/app/src/main/btn_video_busy-web.png
--------------------------------------------------------------------------------
/app/src/main/btn_video_online-web.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mobapptuts/android_camera2_api_video_app/HEAD/app/src/main/btn_video_online-web.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/btn_camera.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mobapptuts/android_camera2_api_video_app/HEAD/app/src/main/res/mipmap-hdpi/btn_camera.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/btn_camera.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mobapptuts/android_camera2_api_video_app/HEAD/app/src/main/res/mipmap-mdpi/btn_camera.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/btn_timelapse.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mobapptuts/android_camera2_api_video_app/HEAD/app/src/main/res/mipmap-hdpi/btn_timelapse.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mobapptuts/android_camera2_api_video_app/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/btn_timelapse.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mobapptuts/android_camera2_api_video_app/HEAD/app/src/main/res/mipmap-mdpi/btn_timelapse.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mobapptuts/android_camera2_api_video_app/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/btn_camera.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mobapptuts/android_camera2_api_video_app/HEAD/app/src/main/res/mipmap-xhdpi/btn_camera.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mobapptuts/android_camera2_api_video_app/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/btn_camera.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mobapptuts/android_camera2_api_video_app/HEAD/app/src/main/res/mipmap-xxhdpi/btn_camera.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mobapptuts/android_camera2_api_video_app/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/btn_camera.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mobapptuts/android_camera2_api_video_app/HEAD/app/src/main/res/mipmap-xxxhdpi/btn_camera.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/btn_video_busy.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mobapptuts/android_camera2_api_video_app/HEAD/app/src/main/res/mipmap-hdpi/btn_video_busy.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/btn_video_busy.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mobapptuts/android_camera2_api_video_app/HEAD/app/src/main/res/mipmap-mdpi/btn_video_busy.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/btn_timelapse.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mobapptuts/android_camera2_api_video_app/HEAD/app/src/main/res/mipmap-xhdpi/btn_timelapse.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/btn_video_busy.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mobapptuts/android_camera2_api_video_app/HEAD/app/src/main/res/mipmap-xhdpi/btn_video_busy.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/btn_timelapse.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mobapptuts/android_camera2_api_video_app/HEAD/app/src/main/res/mipmap-xxhdpi/btn_timelapse.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mobapptuts/android_camera2_api_video_app/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/btn_video_online.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mobapptuts/android_camera2_api_video_app/HEAD/app/src/main/res/mipmap-hdpi/btn_video_online.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/btn_video_online.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mobapptuts/android_camera2_api_video_app/HEAD/app/src/main/res/mipmap-mdpi/btn_video_online.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/btn_video_online.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mobapptuts/android_camera2_api_video_app/HEAD/app/src/main/res/mipmap-xhdpi/btn_video_online.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/btn_video_busy.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mobapptuts/android_camera2_api_video_app/HEAD/app/src/main/res/mipmap-xxhdpi/btn_video_busy.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/btn_video_online.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mobapptuts/android_camera2_api_video_app/HEAD/app/src/main/res/mipmap-xxhdpi/btn_video_online.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/btn_timelapse.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mobapptuts/android_camera2_api_video_app/HEAD/app/src/main/res/mipmap-xxxhdpi/btn_timelapse.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/btn_video_busy.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mobapptuts/android_camera2_api_video_app/HEAD/app/src/main/res/mipmap-xxxhdpi/btn_video_busy.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/btn_video_online.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mobapptuts/android_camera2_api_video_app/HEAD/app/src/main/res/mipmap-xxxhdpi/btn_video_online.png
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | .gradle
2 | /local.properties
3 | /.idea/workspace.xml
4 | /.idea/libraries
5 | /.idea/encodings.xml
6 | /.idea/gradle.xml
7 | /.idea/misc.xml
8 | camera2_video_image_proto.iml
9 | .DS_Store
10 | /build
11 | /captures
12 | /projectFilesBackup/
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | Camera2 Video Image
3 | Video Record Button
4 | Camera Capture Button
5 |
6 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | distributionBase=GRADLE_USER_HOME
2 | distributionPath=wrapper/dists
3 | zipStoreBase=GRADLE_USER_HOME
4 | zipStorePath=wrapper/dists
5 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
6 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 16dp
4 | 16dp
5 |
6 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values-w820dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 64dp
6 |
7 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/app/src/test/java/mobapptut/com/camera2videoimage/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package mobapptut.com.camera2videoimage;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * To work on unit tests, switch the Test Artifact in the Build Variants view.
9 | */
10 | public class ExampleUnitTest {
11 | @Test
12 | public void addition_isCorrect() throws Exception {
13 | assertEquals(4, 2 + 2);
14 | }
15 | }
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/mobapptut/com/camera2videoimage/ApplicationTest.java:
--------------------------------------------------------------------------------
1 | package mobapptut.com.camera2videoimage;
2 |
3 | import android.app.Application;
4 | import android.test.ApplicationTestCase;
5 |
6 | /**
7 | * Testing Fundamentals
8 | */
9 | public class ApplicationTest extends ApplicationTestCase {
10 | public ApplicationTest() {
11 | super(Application.class);
12 | }
13 | }
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # android_camera2_api_video_app
2 | [](https://www.codementor.io/mobapptuts?utm_source=github&utm_medium=button&utm_term=mobapptuts&utm_campaign=github)
3 |
4 | Android tutorial code that describes how to create an android video application using the android camera2 API.
5 |
6 | Code for the android tutorial series found here https://www.youtube.com/playlist?list=PL9jCwTXYWjDIHNEGtsRdCTk79I9-95TbJ that describes how to create an android video application using the android camera2 API.
7 |
--------------------------------------------------------------------------------
/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 24
5 | buildToolsVersion "24.0.3"
6 |
7 | defaultConfig {
8 | applicationId "mobapptut.com.camera2videoimage"
9 | minSdkVersion 21
10 | targetSdkVersion 24
11 | versionCode 1
12 | versionName "1.0"
13 | }
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 | }
21 |
22 | dependencies {
23 | compile fileTree(dir: 'libs', include: ['*.jar'])
24 | testCompile 'junit:junit:4.12'
25 | compile 'com.android.support:appcompat-v7:24.2.1'
26 | }
27 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /Users/nigelhenshaw/Downloads/adt-bundle-mac-x86_64-20140702/sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m
13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
14 |
15 | # When configured, Gradle will run in incubating parallel mode.
16 | # This option should only be used with decoupled projects. More details, visit
17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
18 | # org.gradle.parallel=true
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
8 |
9 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/Camera2VideoImage.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/LICENSE.md:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2015 Nigel Henshaw
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 |
--------------------------------------------------------------------------------
/android_manifest_permission_camera.patch:
--------------------------------------------------------------------------------
1 | Index: app/src/main/java/mobapptut/com/camera2videoimage/Camera2VideoImageActivity.java
2 | IDEA additional info:
3 | Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
4 | <+>UTF-8
5 | ===================================================================
6 | --- app/src/main/java/mobapptut/com/camera2videoimage/Camera2VideoImageActivity.java (date 1460880215000)
7 | +++ app/src/main/java/mobapptut/com/camera2videoimage/Camera2VideoImageActivity.java (revision )
8 | @@ -262,15 +262,15 @@
9 | CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
10 | try {
11 | if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
12 | - if(ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) ==
13 | + if(ContextCompat.checkSelfPermission(this, android.Manifest.permission.CAMERA) ==
14 | PackageManager.PERMISSION_GRANTED) {
15 | cameraManager.openCamera(mCameraId, mCameraDeviceStateCallback, mBackgroundHandler);
16 | } else {
17 | - if(shouldShowRequestPermissionRationale(Manifest.permission.CAMERA)) {
18 | + if(shouldShowRequestPermissionRationale(android.Manifest.permission.CAMERA)) {
19 | Toast.makeText(this,
20 | "Video app required access to camera", Toast.LENGTH_SHORT).show();
21 | }
22 | - requestPermissions(new String[] {Manifest.permission.CAMERA}, REQUEST_CAMERA_PERMISSION_RESULT);
23 | + requestPermissions(new String[] {android.Manifest.permission.CAMERA}, REQUEST_CAMERA_PERMISSION_RESULT);
24 | }
25 |
26 | } else {
27 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_camera2_video_image.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
14 |
15 |
25 |
26 |
35 |
36 |
45 |
46 |
47 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/app/app.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 | generateDebugSources
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
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 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
120 |
--------------------------------------------------------------------------------
/app/src/main/java/mobapptut/com/camera2videoimage/Camera2VideoImageActivity.java:
--------------------------------------------------------------------------------
1 | package mobapptut.com.camera2videoimage;
2 |
3 | import android.Manifest;
4 | import android.content.Context;
5 | import android.content.Intent;
6 | import android.content.pm.PackageManager;
7 | import android.graphics.ImageFormat;
8 | import android.graphics.SurfaceTexture;
9 | import android.hardware.camera2.CameraAccessException;
10 | import android.hardware.camera2.CameraCaptureSession;
11 | import android.hardware.camera2.CameraCharacteristics;
12 | import android.hardware.camera2.CameraDevice;
13 | import android.hardware.camera2.CameraManager;
14 | import android.hardware.camera2.CaptureRequest;
15 | import android.hardware.camera2.CaptureResult;
16 | import android.hardware.camera2.TotalCaptureResult;
17 | import android.hardware.camera2.params.StreamConfigurationMap;
18 | import android.media.CamcorderProfile;
19 | import android.media.Image;
20 | import android.media.ImageReader;
21 | import android.media.MediaRecorder;
22 | import android.net.Uri;
23 | import android.os.Build;
24 | import android.os.Environment;
25 | import android.os.Handler;
26 | import android.os.HandlerThread;
27 | import android.os.SystemClock;
28 | import android.support.v4.content.ContextCompat;
29 | import android.support.v7.app.AppCompatActivity;
30 | import android.os.Bundle;
31 | import android.util.Log;
32 | import android.util.Size;
33 | import android.util.SparseIntArray;
34 | import android.view.Surface;
35 | import android.view.TextureView;
36 | import android.view.View;
37 | import android.widget.Chronometer;
38 | import android.widget.ImageButton;
39 | import android.widget.Toast;
40 |
41 | import java.io.File;
42 | import java.io.FileOutputStream;
43 | import java.io.IOException;
44 | import java.nio.ByteBuffer;
45 | import java.text.SimpleDateFormat;
46 | import java.util.ArrayList;
47 | import java.util.Arrays;
48 | import java.util.Collections;
49 | import java.util.Comparator;
50 | import java.util.Date;
51 | import java.util.List;
52 |
53 | public class Camera2VideoImageActivity extends AppCompatActivity {
54 |
55 | private static final String TAG = "Camera2VideoImageActivi";
56 |
57 | private static final int REQUEST_CAMERA_PERMISSION_RESULT = 0;
58 | private static final int REQUEST_WRITE_EXTERNAL_STORAGE_PERMISSION_RESULT = 1;
59 | private static final int STATE_PREVIEW = 0;
60 | private static final int STATE_WAIT_LOCK = 1;
61 | private int mCaptureState = STATE_PREVIEW;
62 | private TextureView mTextureView;
63 | private TextureView.SurfaceTextureListener mSurfaceTextureListener = new TextureView.SurfaceTextureListener() {
64 | @Override
65 | public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
66 | setupCamera(width, height);
67 | connectCamera();
68 | }
69 |
70 | @Override
71 | public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
72 |
73 | }
74 |
75 | @Override
76 | public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
77 | return false;
78 | }
79 |
80 | @Override
81 | public void onSurfaceTextureUpdated(SurfaceTexture surface) {
82 |
83 | }
84 | };
85 | private CameraDevice mCameraDevice;
86 | private CameraDevice.StateCallback mCameraDeviceStateCallback = new CameraDevice.StateCallback() {
87 | @Override
88 | public void onOpened(CameraDevice camera) {
89 | mCameraDevice = camera;
90 | mMediaRecorder = new MediaRecorder();
91 | if(mIsRecording) {
92 | try {
93 | createVideoFileName();
94 | } catch (IOException e) {
95 | e.printStackTrace();
96 | }
97 | startRecord();
98 | mMediaRecorder.start();
99 | runOnUiThread(new Runnable() {
100 | @Override
101 | public void run() {
102 | mChronometer.setBase(SystemClock.elapsedRealtime());
103 | mChronometer.setVisibility(View.VISIBLE);
104 | mChronometer.start();
105 | }
106 | });
107 | } else {
108 | startPreview();
109 | }
110 | // Toast.makeText(getApplicationContext(),
111 | // "Camera connection made!", Toast.LENGTH_SHORT).show();
112 | }
113 |
114 | @Override
115 | public void onDisconnected(CameraDevice camera) {
116 | camera.close();
117 | mCameraDevice = null;
118 | }
119 |
120 | @Override
121 | public void onError(CameraDevice camera, int error) {
122 | camera.close();
123 | mCameraDevice = null;
124 | }
125 | };
126 | private HandlerThread mBackgroundHandlerThread;
127 | private Handler mBackgroundHandler;
128 | private String mCameraId;
129 | private Size mPreviewSize;
130 | private Size mVideoSize;
131 | private Size mImageSize;
132 | private ImageReader mImageReader;
133 | private final ImageReader.OnImageAvailableListener mOnImageAvailableListener = new
134 | ImageReader.OnImageAvailableListener() {
135 | @Override
136 | public void onImageAvailable(ImageReader reader) {
137 | mBackgroundHandler.post(new ImageSaver(reader.acquireLatestImage()));
138 | }
139 | };
140 | private class ImageSaver implements Runnable {
141 |
142 | private final Image mImage;
143 |
144 | public ImageSaver(Image image) {
145 | mImage = image;
146 | }
147 |
148 | @Override
149 | public void run() {
150 | ByteBuffer byteBuffer = mImage.getPlanes()[0].getBuffer();
151 | byte[] bytes = new byte[byteBuffer.remaining()];
152 | byteBuffer.get(bytes);
153 |
154 | FileOutputStream fileOutputStream = null;
155 | try {
156 | fileOutputStream = new FileOutputStream(mImageFileName);
157 | fileOutputStream.write(bytes);
158 | } catch (IOException e) {
159 | e.printStackTrace();
160 | } finally {
161 | mImage.close();
162 |
163 | Intent mediaStoreUpdateIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
164 | mediaStoreUpdateIntent.setData(Uri.fromFile(new File(mImageFileName)));
165 | sendBroadcast(mediaStoreUpdateIntent);
166 |
167 | if(fileOutputStream != null) {
168 | try {
169 | fileOutputStream.close();
170 | } catch (IOException e) {
171 | e.printStackTrace();
172 | }
173 | }
174 | }
175 |
176 | }
177 | }
178 | private MediaRecorder mMediaRecorder;
179 | private Chronometer mChronometer;
180 | private int mTotalRotation;
181 | private CameraCaptureSession mPreviewCaptureSession;
182 | private CameraCaptureSession.CaptureCallback mPreviewCaptureCallback = new
183 | CameraCaptureSession.CaptureCallback() {
184 |
185 | private void process(CaptureResult captureResult) {
186 | switch (mCaptureState) {
187 | case STATE_PREVIEW:
188 | // Do nothing
189 | break;
190 | case STATE_WAIT_LOCK:
191 | mCaptureState = STATE_PREVIEW;
192 | Integer afState = captureResult.get(CaptureResult.CONTROL_AF_STATE);
193 | if(afState == CaptureResult.CONTROL_AF_STATE_FOCUSED_LOCKED ||
194 | afState == CaptureResult.CONTROL_AF_STATE_NOT_FOCUSED_LOCKED) {
195 | Toast.makeText(getApplicationContext(), "AF Locked!", Toast.LENGTH_SHORT).show();
196 | startStillCaptureRequest();
197 | }
198 | break;
199 | }
200 | }
201 |
202 | @Override
203 | public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request, TotalCaptureResult result) {
204 | super.onCaptureCompleted(session, request, result);
205 |
206 | process(result);
207 | }
208 | };
209 | private CameraCaptureSession mRecordCaptureSession;
210 | private CameraCaptureSession.CaptureCallback mRecordCaptureCallback = new
211 | CameraCaptureSession.CaptureCallback() {
212 |
213 | private void process(CaptureResult captureResult) {
214 | switch (mCaptureState) {
215 | case STATE_PREVIEW:
216 | // Do nothing
217 | break;
218 | case STATE_WAIT_LOCK:
219 | mCaptureState = STATE_PREVIEW;
220 | Integer afState = captureResult.get(CaptureResult.CONTROL_AF_STATE);
221 | if(afState == CaptureResult.CONTROL_AF_STATE_FOCUSED_LOCKED ||
222 | afState == CaptureResult.CONTROL_AF_STATE_NOT_FOCUSED_LOCKED) {
223 | Toast.makeText(getApplicationContext(), "AF Locked!", Toast.LENGTH_SHORT).show();
224 | startStillCaptureRequest();
225 | }
226 | break;
227 | }
228 | }
229 |
230 | @Override
231 | public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request, TotalCaptureResult result) {
232 | super.onCaptureCompleted(session, request, result);
233 |
234 | process(result);
235 | }
236 | };
237 | private CaptureRequest.Builder mCaptureRequestBuilder;
238 |
239 | private ImageButton mRecordImageButton;
240 | private ImageButton mStillImageButton;
241 | private boolean mIsRecording = false;
242 | private boolean mIsTimelapse = false;
243 |
244 | private File mVideoFolder;
245 | private String mVideoFileName;
246 | private File mImageFolder;
247 | private String mImageFileName;
248 |
249 | private static SparseIntArray ORIENTATIONS = new SparseIntArray();
250 | static {
251 | ORIENTATIONS.append(Surface.ROTATION_0, 0);
252 | ORIENTATIONS.append(Surface.ROTATION_90, 90);
253 | ORIENTATIONS.append(Surface.ROTATION_180, 180);
254 | ORIENTATIONS.append(Surface.ROTATION_270, 270);
255 | }
256 |
257 | private static class CompareSizeByArea implements Comparator {
258 |
259 | @Override
260 | public int compare(Size lhs, Size rhs) {
261 | return Long.signum( (long)(lhs.getWidth() * lhs.getHeight()) -
262 | (long)(rhs.getWidth() * rhs.getHeight()));
263 | }
264 | }
265 |
266 | @Override
267 | protected void onCreate(Bundle savedInstanceState) {
268 | super.onCreate(savedInstanceState);
269 | setContentView(R.layout.activity_camera2_video_image);
270 |
271 | createVideoFolder();
272 | createImageFolder();
273 |
274 | mChronometer = (Chronometer) findViewById(R.id.chronometer);
275 | mTextureView = (TextureView) findViewById(R.id.textureView);
276 | mStillImageButton = (ImageButton) findViewById(R.id.cameraImageButton2);
277 | mStillImageButton.setOnClickListener(new View.OnClickListener() {
278 | @Override
279 | public void onClick(View v) {
280 | if(!(mIsTimelapse || mIsRecording)) {
281 | checkWriteStoragePermission();
282 | }
283 | lockFocus();
284 | }
285 | });
286 | mRecordImageButton = (ImageButton) findViewById(R.id.videoOnlineImageButton);
287 | mRecordImageButton.setOnClickListener(new View.OnClickListener() {
288 | @Override
289 | public void onClick(View v) {
290 | if (mIsRecording || mIsTimelapse) {
291 | mChronometer.stop();
292 | mChronometer.setVisibility(View.INVISIBLE);
293 | mIsRecording = false;
294 | mIsTimelapse = false;
295 | mRecordImageButton.setImageResource(R.mipmap.btn_video_online);
296 |
297 | // Starting the preview prior to stopping recording which should hopefully
298 | // resolve issues being seen in Samsung devices.
299 | startPreview();
300 | mMediaRecorder.stop();
301 | mMediaRecorder.reset();
302 |
303 | Intent mediaStoreUpdateIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
304 | mediaStoreUpdateIntent.setData(Uri.fromFile(new File(mVideoFileName)));
305 | sendBroadcast(mediaStoreUpdateIntent);
306 |
307 | } else {
308 | mIsRecording = true;
309 | mRecordImageButton.setImageResource(R.mipmap.btn_video_busy);
310 | checkWriteStoragePermission();
311 | }
312 | }
313 | });
314 | mRecordImageButton.setOnLongClickListener(new View.OnLongClickListener() {
315 | @Override
316 | public boolean onLongClick(View v) {
317 | mIsTimelapse =true;
318 | mRecordImageButton.setImageResource(R.mipmap.btn_timelapse);
319 | checkWriteStoragePermission();
320 | return true;
321 | }
322 | });
323 | }
324 |
325 | @Override
326 | protected void onResume() {
327 | super.onResume();
328 |
329 | startBackgroundThread();
330 |
331 | if(mTextureView.isAvailable()) {
332 | setupCamera(mTextureView.getWidth(), mTextureView.getHeight());
333 | connectCamera();
334 | } else {
335 | mTextureView.setSurfaceTextureListener(mSurfaceTextureListener);
336 | }
337 | }
338 |
339 | @Override
340 | public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
341 | super.onRequestPermissionsResult(requestCode, permissions, grantResults);
342 | if(requestCode == REQUEST_CAMERA_PERMISSION_RESULT) {
343 | if(grantResults[0] != PackageManager.PERMISSION_GRANTED) {
344 | Toast.makeText(getApplicationContext(),
345 | "Application will not run without camera services", Toast.LENGTH_SHORT).show();
346 | }
347 | if(grantResults[1] != PackageManager.PERMISSION_GRANTED) {
348 | Toast.makeText(getApplicationContext(),
349 | "Application will not have audio on record", Toast.LENGTH_SHORT).show();
350 | }
351 | }
352 | if(requestCode == REQUEST_WRITE_EXTERNAL_STORAGE_PERMISSION_RESULT) {
353 | if(grantResults[0] == PackageManager.PERMISSION_GRANTED) {
354 | if(mIsRecording || mIsTimelapse) {
355 | mIsRecording = true;
356 | mRecordImageButton.setImageResource(R.mipmap.btn_video_busy);
357 | }
358 | Toast.makeText(this,
359 | "Permission successfully granted!", Toast.LENGTH_SHORT).show();
360 | } else {
361 | Toast.makeText(this,
362 | "App needs to save video to run", Toast.LENGTH_SHORT).show();
363 | }
364 | }
365 | }
366 |
367 | @Override
368 | protected void onPause() {
369 | closeCamera();
370 |
371 | stopBackgroundThread();
372 |
373 | super.onPause();
374 | }
375 |
376 | @Override
377 | public void onWindowFocusChanged(boolean hasFocas) {
378 | super.onWindowFocusChanged(hasFocas);
379 | View decorView = getWindow().getDecorView();
380 | if(hasFocas) {
381 | decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
382 | | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
383 | | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
384 | | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
385 | | View.SYSTEM_UI_FLAG_FULLSCREEN
386 | | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
387 | }
388 | }
389 |
390 | private void setupCamera(int width, int height) {
391 | CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
392 | try {
393 | for(String cameraId : cameraManager.getCameraIdList()){
394 | CameraCharacteristics cameraCharacteristics = cameraManager.getCameraCharacteristics(cameraId);
395 | if(cameraCharacteristics.get(CameraCharacteristics.LENS_FACING) ==
396 | CameraCharacteristics.LENS_FACING_FRONT){
397 | continue;
398 | }
399 | StreamConfigurationMap map = cameraCharacteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
400 | int deviceOrientation = getWindowManager().getDefaultDisplay().getRotation();
401 | mTotalRotation = sensorToDeviceRotation(cameraCharacteristics, deviceOrientation);
402 | boolean swapRotation = mTotalRotation == 90 || mTotalRotation == 270;
403 | int rotatedWidth = width;
404 | int rotatedHeight = height;
405 | if(swapRotation) {
406 | rotatedWidth = height;
407 | rotatedHeight = width;
408 | }
409 | mPreviewSize = chooseOptimalSize(map.getOutputSizes(SurfaceTexture.class), rotatedWidth, rotatedHeight);
410 | mVideoSize = chooseOptimalSize(map.getOutputSizes(MediaRecorder.class), rotatedWidth, rotatedHeight);
411 | mImageSize = chooseOptimalSize(map.getOutputSizes(ImageFormat.JPEG), rotatedWidth, rotatedHeight);
412 | mImageReader = ImageReader.newInstance(mImageSize.getWidth(), mImageSize.getHeight(), ImageFormat.JPEG, 1);
413 | mImageReader.setOnImageAvailableListener(mOnImageAvailableListener, mBackgroundHandler);
414 | mCameraId = cameraId;
415 | return;
416 | }
417 | } catch (CameraAccessException e) {
418 | e.printStackTrace();
419 | }
420 | }
421 |
422 | private void connectCamera() {
423 | CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
424 | try {
425 | if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
426 | if(ContextCompat.checkSelfPermission(this, android.Manifest.permission.CAMERA) ==
427 | PackageManager.PERMISSION_GRANTED) {
428 | cameraManager.openCamera(mCameraId, mCameraDeviceStateCallback, mBackgroundHandler);
429 | } else {
430 | if(shouldShowRequestPermissionRationale(android.Manifest.permission.CAMERA)) {
431 | Toast.makeText(this,
432 | "Video app required access to camera", Toast.LENGTH_SHORT).show();
433 | }
434 | requestPermissions(new String[] {android.Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO
435 | }, REQUEST_CAMERA_PERMISSION_RESULT);
436 | }
437 |
438 | } else {
439 | cameraManager.openCamera(mCameraId, mCameraDeviceStateCallback, mBackgroundHandler);
440 | }
441 | } catch (CameraAccessException e) {
442 | e.printStackTrace();
443 | }
444 | }
445 |
446 | private void startRecord() {
447 |
448 | try {
449 | if(mIsRecording) {
450 | setupMediaRecorder();
451 | } else if(mIsTimelapse) {
452 | setupTimelapse();
453 | }
454 | SurfaceTexture surfaceTexture = mTextureView.getSurfaceTexture();
455 | surfaceTexture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight());
456 | Surface previewSurface = new Surface(surfaceTexture);
457 | Surface recordSurface = mMediaRecorder.getSurface();
458 | mCaptureRequestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_RECORD);
459 | mCaptureRequestBuilder.addTarget(previewSurface);
460 | mCaptureRequestBuilder.addTarget(recordSurface);
461 |
462 | mCameraDevice.createCaptureSession(Arrays.asList(previewSurface, recordSurface, mImageReader.getSurface()),
463 | new CameraCaptureSession.StateCallback() {
464 | @Override
465 | public void onConfigured(CameraCaptureSession session) {
466 | mRecordCaptureSession = session;
467 | try {
468 | mRecordCaptureSession.setRepeatingRequest(
469 | mCaptureRequestBuilder.build(), null, null
470 | );
471 | } catch (CameraAccessException e) {
472 | e.printStackTrace();
473 | }
474 | }
475 |
476 | @Override
477 | public void onConfigureFailed(CameraCaptureSession session) {
478 | Log.d(TAG, "onConfigureFailed: startRecord");
479 | }
480 | }, null);
481 |
482 | } catch (Exception e) {
483 | e.printStackTrace();
484 | }
485 | }
486 | private void startPreview() {
487 | SurfaceTexture surfaceTexture = mTextureView.getSurfaceTexture();
488 | surfaceTexture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight());
489 | Surface previewSurface = new Surface(surfaceTexture);
490 |
491 | try {
492 | mCaptureRequestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
493 | mCaptureRequestBuilder.addTarget(previewSurface);
494 |
495 | mCameraDevice.createCaptureSession(Arrays.asList(previewSurface, mImageReader.getSurface()),
496 | new CameraCaptureSession.StateCallback() {
497 | @Override
498 | public void onConfigured(CameraCaptureSession session) {
499 | Log.d(TAG, "onConfigured: startPreview");
500 | mPreviewCaptureSession = session;
501 | try {
502 | mPreviewCaptureSession.setRepeatingRequest(mCaptureRequestBuilder.build(),
503 | null, mBackgroundHandler);
504 | } catch (CameraAccessException e) {
505 | e.printStackTrace();
506 | }
507 | }
508 |
509 | @Override
510 | public void onConfigureFailed(CameraCaptureSession session) {
511 | Log.d(TAG, "onConfigureFailed: startPreview");
512 |
513 | }
514 | }, null);
515 | } catch (CameraAccessException e) {
516 | e.printStackTrace();
517 | }
518 | }
519 |
520 | private void startStillCaptureRequest() {
521 | try {
522 | if(mIsRecording) {
523 | mCaptureRequestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_VIDEO_SNAPSHOT);
524 | } else {
525 | mCaptureRequestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
526 | }
527 | mCaptureRequestBuilder.addTarget(mImageReader.getSurface());
528 | mCaptureRequestBuilder.set(CaptureRequest.JPEG_ORIENTATION, mTotalRotation);
529 |
530 | CameraCaptureSession.CaptureCallback stillCaptureCallback = new
531 | CameraCaptureSession.CaptureCallback() {
532 | @Override
533 | public void onCaptureStarted(CameraCaptureSession session, CaptureRequest request, long timestamp, long frameNumber) {
534 | super.onCaptureStarted(session, request, timestamp, frameNumber);
535 |
536 | try {
537 | createImageFileName();
538 | } catch (IOException e) {
539 | e.printStackTrace();
540 | }
541 | }
542 | };
543 |
544 | if(mIsRecording) {
545 | mRecordCaptureSession.capture(mCaptureRequestBuilder.build(), stillCaptureCallback, null);
546 | } else {
547 | mPreviewCaptureSession.capture(mCaptureRequestBuilder.build(), stillCaptureCallback, null);
548 | }
549 | } catch (CameraAccessException e) {
550 | e.printStackTrace();
551 | }
552 | }
553 |
554 | private void closeCamera() {
555 | if(mCameraDevice != null) {
556 | mCameraDevice.close();
557 | mCameraDevice = null;
558 | }
559 | if(mMediaRecorder != null) {
560 | mMediaRecorder.release();
561 | mMediaRecorder = null;
562 | }
563 | }
564 |
565 | private void startBackgroundThread() {
566 | mBackgroundHandlerThread = new HandlerThread("Camera2VideoImage");
567 | mBackgroundHandlerThread.start();
568 | mBackgroundHandler = new Handler(mBackgroundHandlerThread.getLooper());
569 | }
570 |
571 | private void stopBackgroundThread() {
572 | mBackgroundHandlerThread.quitSafely();
573 | try {
574 | mBackgroundHandlerThread.join();
575 | mBackgroundHandlerThread = null;
576 | mBackgroundHandler = null;
577 | } catch (InterruptedException e) {
578 | e.printStackTrace();
579 | }
580 | }
581 |
582 | private static int sensorToDeviceRotation(CameraCharacteristics cameraCharacteristics, int deviceOrientation) {
583 | int sensorOrienatation = cameraCharacteristics.get(CameraCharacteristics.SENSOR_ORIENTATION);
584 | deviceOrientation = ORIENTATIONS.get(deviceOrientation);
585 | return (sensorOrienatation + deviceOrientation + 360) % 360;
586 | }
587 |
588 | private static Size chooseOptimalSize(Size[] choices, int width, int height) {
589 | List bigEnough = new ArrayList();
590 | for(Size option : choices) {
591 | if(option.getHeight() == option.getWidth() * height / width &&
592 | option.getWidth() >= width && option.getHeight() >= height) {
593 | bigEnough.add(option);
594 | }
595 | }
596 | if(bigEnough.size() > 0) {
597 | return Collections.min(bigEnough, new CompareSizeByArea());
598 | } else {
599 | return choices[0];
600 | }
601 | }
602 |
603 | private void createVideoFolder() {
604 | File movieFile = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES);
605 | mVideoFolder = new File(movieFile, "camera2VideoImage");
606 | if(!mVideoFolder.exists()) {
607 | mVideoFolder.mkdirs();
608 | }
609 | }
610 |
611 | private File createVideoFileName() throws IOException {
612 | String timestamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
613 | String prepend = "VIDEO_" + timestamp + "_";
614 | File videoFile = File.createTempFile(prepend, ".mp4", mVideoFolder);
615 | mVideoFileName = videoFile.getAbsolutePath();
616 | return videoFile;
617 | }
618 |
619 | private void createImageFolder() {
620 | File imageFile = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
621 | mImageFolder = new File(imageFile, "camera2VideoImage");
622 | if(!mImageFolder.exists()) {
623 | mImageFolder.mkdirs();
624 | }
625 | }
626 |
627 | private File createImageFileName() throws IOException {
628 | String timestamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
629 | String prepend = "IMAGE_" + timestamp + "_";
630 | File imageFile = File.createTempFile(prepend, ".jpg", mImageFolder);
631 | mImageFileName = imageFile.getAbsolutePath();
632 | return imageFile;
633 | }
634 |
635 | private void checkWriteStoragePermission() {
636 | if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
637 | if(ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
638 | == PackageManager.PERMISSION_GRANTED) {
639 | try {
640 | createVideoFileName();
641 | } catch (IOException e) {
642 | e.printStackTrace();
643 | }
644 | if(mIsTimelapse || mIsRecording) {
645 | startRecord();
646 | mMediaRecorder.start();
647 | mChronometer.setBase(SystemClock.elapsedRealtime());
648 | mChronometer.setVisibility(View.VISIBLE);
649 | mChronometer.start();
650 | }
651 | } else {
652 | if(shouldShowRequestPermissionRationale(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
653 | Toast.makeText(this, "app needs to be able to save videos", Toast.LENGTH_SHORT).show();
654 | }
655 | requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_WRITE_EXTERNAL_STORAGE_PERMISSION_RESULT);
656 | }
657 | } else {
658 | try {
659 | createVideoFileName();
660 | } catch (IOException e) {
661 | e.printStackTrace();
662 | }
663 | if(mIsRecording || mIsTimelapse) {
664 | startRecord();
665 | mMediaRecorder.start();
666 | mChronometer.setBase(SystemClock.elapsedRealtime());
667 | mChronometer.setVisibility(View.VISIBLE);
668 | mChronometer.start();
669 | }
670 | }
671 | }
672 |
673 | private void setupMediaRecorder() throws IOException {
674 | mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
675 | mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
676 | mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
677 | mMediaRecorder.setOutputFile(mVideoFileName);
678 | mMediaRecorder.setVideoEncodingBitRate(1000000);
679 | mMediaRecorder.setVideoFrameRate(30);
680 | mMediaRecorder.setVideoSize(mVideoSize.getWidth(), mVideoSize.getHeight());
681 | mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
682 | mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
683 | mMediaRecorder.setOrientationHint(mTotalRotation);
684 | mMediaRecorder.prepare();
685 | }
686 |
687 | private void setupTimelapse() throws IOException {
688 | mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
689 | mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_TIME_LAPSE_HIGH));
690 | mMediaRecorder.setOutputFile(mVideoFileName);
691 | mMediaRecorder.setCaptureRate(2);
692 | mMediaRecorder.setOrientationHint(mTotalRotation);
693 | mMediaRecorder.prepare();
694 | }
695 |
696 | private void lockFocus() {
697 | mCaptureState = STATE_WAIT_LOCK;
698 | mCaptureRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CaptureRequest.CONTROL_AF_TRIGGER_START);
699 | try {
700 | if(mIsRecording) {
701 | mRecordCaptureSession.capture(mCaptureRequestBuilder.build(), mRecordCaptureCallback, mBackgroundHandler);
702 | } else {
703 | mPreviewCaptureSession.capture(mCaptureRequestBuilder.build(), mPreviewCaptureCallback, mBackgroundHandler);
704 | }
705 | } catch (CameraAccessException e) {
706 | e.printStackTrace();
707 | }
708 | }
709 | }
710 |
--------------------------------------------------------------------------------