├── settings.gradle
├── .DS_Store
├── app
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── values
│ │ │ │ ├── strings.xml
│ │ │ │ ├── colors.xml
│ │ │ │ └── styles.xml
│ │ │ ├── mipmap-hdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ ├── ic_launcher_round.png
│ │ │ │ └── ic_launcher_foreground.png
│ │ │ ├── mipmap-mdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ ├── ic_launcher_round.png
│ │ │ │ └── ic_launcher_foreground.png
│ │ │ ├── mipmap-xhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ ├── ic_launcher_round.png
│ │ │ │ └── ic_launcher_foreground.png
│ │ │ ├── mipmap-xxhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ ├── ic_launcher_round.png
│ │ │ │ └── ic_launcher_foreground.png
│ │ │ ├── mipmap-xxxhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ ├── ic_launcher_round.png
│ │ │ │ └── ic_launcher_foreground.png
│ │ │ ├── mipmap-anydpi-v26
│ │ │ │ ├── ic_launcher.xml
│ │ │ │ └── ic_launcher_round.xml
│ │ │ ├── layout
│ │ │ │ └── activity_camera.xml
│ │ │ └── drawable
│ │ │ │ └── ic_launcher_background.xml
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ │ └── com
│ │ │ └── bluewindtalker
│ │ │ └── camera
│ │ │ └── demo
│ │ │ ├── LightSensorUtil.java
│ │ │ └── CameraActivity.java
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── bluewindtalker
│ │ │ └── camera
│ │ │ └── demo
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── bluewindtalker
│ │ └── camera
│ │ └── demo
│ │ └── ExampleInstrumentedTest.java
├── proguard-rules.pro
├── .gitignore
└── build.gradle
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── README.md
├── gradle.properties
├── .gitignore
├── gradlew.bat
├── gradlew
└── LICENSE
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app'
2 |
--------------------------------------------------------------------------------
/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bluewindtalker/camerademo/HEAD/.DS_Store
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | CameraDemo
3 |
4 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bluewindtalker/camerademo/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bluewindtalker/camerademo/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bluewindtalker/camerademo/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bluewindtalker/camerademo/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bluewindtalker/camerademo/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bluewindtalker/camerademo/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bluewindtalker/camerademo/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/bluewindtalker/camerademo/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/bluewindtalker/camerademo/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/bluewindtalker/camerademo/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/bluewindtalker/camerademo/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_foreground.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bluewindtalker/camerademo/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher_foreground.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_foreground.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bluewindtalker/camerademo/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_foreground.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_foreground.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bluewindtalker/camerademo/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher_foreground.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_foreground.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bluewindtalker/camerademo/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher_foreground.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_foreground.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bluewindtalker/camerademo/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_foreground.png
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Sat Apr 14 11:05:00 CST 2018
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-4.1-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 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # camerademo
2 | 相机的流程demo
3 | demo讲解可看我的技术文章
4 | https://blog.csdn.net/bluewindtalker/article/details/54563910
5 | 欢迎关注我的微博
6 | bluewindtalker
7 |
8 | 本作品采用知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议进行许可。
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/test/java/com/bluewindtalker/camera/demo/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.bluewindtalker.camera.demo;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() throws Exception {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/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 | org.gradle.jvmargs=-Xmx1536m
13 |
14 | # When configured, Gradle will run in incubating parallel mode.
15 | # This option should only be used with decoupled projects. More details, visit
16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
17 | # org.gradle.parallel=true
18 |
--------------------------------------------------------------------------------
/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/bluewindtalker/camera/demo/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.bluewindtalker.camera.demo;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumented test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() throws Exception {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.bluewindtalker.camera.camerademo", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | # Built application files
2 | *.apk
3 | *.ap_
4 |
5 | # Files for the ART/Dalvik VM
6 | *.dex
7 |
8 | # Java class files
9 | *.class
10 |
11 | # Generated files
12 | bin/
13 | gen/
14 | out/
15 |
16 | # Gradle files
17 | .gradle/
18 | build/
19 |
20 | # Local configuration file (sdk path, etc)
21 | local.properties
22 |
23 | # Proguard folder generated by Eclipse
24 | proguard/
25 |
26 | # Log Files
27 | *.log
28 |
29 | # Android Studio Navigation editor temp files
30 | .navigation/
31 |
32 | # Android Studio captures folder
33 | captures/
34 |
35 | # Intellij
36 | *.iml
37 | .idea/workspace.xml
38 | .idea/tasks.xml
39 | .idea/gradle.xml
40 | .idea/dictionaries
41 | .idea/libraries
42 |
43 | # Keystore files
44 | *.jks
45 |
46 | # External native build folder generated in Android Studio 2.2 and later
47 | .externalNativeBuild
48 |
49 | # Google Services (e.g. APIs or Firebase)
50 | google-services.json
51 |
52 | # Freeline
53 | freeline.py
54 | freeline/
55 | freeline_project_description.json
56 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Built application files
2 | *.apk
3 | *.ap_
4 |
5 | # Files for the ART/Dalvik VM
6 | *.dex
7 |
8 | # Java class files
9 | *.class
10 |
11 | # Generated files
12 | bin/
13 | gen/
14 | out/
15 |
16 | # Gradle files
17 | .gradle/
18 | build/
19 |
20 | # Local configuration file (sdk path, etc)
21 | local.properties
22 |
23 | # Proguard folder generated by Eclipse
24 | proguard/
25 |
26 | # Log Files
27 | *.log
28 |
29 | # Android Studio Navigation editor temp files
30 | .navigation/
31 |
32 | # Android Studio captures folder
33 | captures/
34 |
35 | # Intellij
36 | *.iml
37 | .idea/workspace.xml
38 | .idea/tasks.xml
39 | .idea/gradle.xml
40 | .idea/dictionaries
41 | .idea/libraries
42 |
43 | # Keystore files
44 | *.jks
45 |
46 | # External native build folder generated in Android Studio 2.2 and later
47 | .externalNativeBuild
48 |
49 | # Google Services (e.g. APIs or Firebase)
50 | google-services.json
51 |
52 | # Freeline
53 | freeline.py
54 | freeline/
55 | freeline_project_description.json
56 | */.idea/
57 | .idea/
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 26
5 | buildToolsVersion "27.0.0"
6 | defaultConfig {
7 | applicationId "com.bluewindtalker.camera.camerademo"
8 | minSdkVersion 23
9 | targetSdkVersion 26
10 | versionCode 1
11 | versionName "1.0"
12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
13 | }
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 | }
21 |
22 | dependencies {
23 | implementation fileTree(dir: 'libs', include: ['*.jar'])
24 | implementation 'com.android.support:appcompat-v7:26.1.0'
25 | implementation 'com.android.support.constraint:constraint-layout:1.1.0'
26 | testImplementation 'junit:junit:4.12'
27 | androidTestImplementation('com.android.support.test.espresso:espresso-core:3.0.1', {
28 | exclude group: 'com.android.support', module: 'support-annotations'
29 | })
30 | }
31 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
8 |
15 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/app/src/main/java/com/bluewindtalker/camera/demo/LightSensorUtil.java:
--------------------------------------------------------------------------------
1 | package com.bluewindtalker.camera.demo;
2 |
3 | import android.content.Context;
4 | import android.hardware.Sensor;
5 | import android.hardware.SensorEvent;
6 | import android.hardware.SensorEventListener;
7 | import android.hardware.SensorManager;
8 |
9 | /**
10 | * @author bluewindtalker
11 | * @description 光线传感器工具
12 | * @date 2018/4/15-下午12:08
13 | */
14 | public final class LightSensorUtil {
15 |
16 | private LightSensorUtil() {
17 | }
18 |
19 | public static SensorManager getSenosrManager(Context context){
20 | return (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
21 |
22 | }
23 |
24 | /**
25 | * 注册光线传感器监听器
26 | * @param sensorManager
27 | * @param listener
28 | */
29 | public static void registerLightSensor(SensorManager sensorManager,SensorEventListener listener) {
30 | if(sensorManager == null || listener == null){
31 | return;
32 | }
33 | Sensor lightSensor = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT); // 获取光线传感器
34 | if (lightSensor != null) { // 光线传感器存在时
35 | sensorManager.registerListener(listener, lightSensor, SensorManager.SENSOR_DELAY_NORMAL); // 注册事件监听
36 | }
37 | }
38 | /**
39 | * 反注册光线传感器监听器
40 | * @param sensorManager
41 | * @param listener
42 | */
43 | public static void unregisterLightSensor(SensorManager sensorManager,SensorEventListener listener) {
44 | if(sensorManager == null || listener == null){
45 | return;
46 | }
47 | sensorManager.unregisterListener(listener);
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_camera.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
13 |
14 |
25 |
26 |
32 |
33 |
38 |
39 |
48 |
49 |
50 |
62 |
63 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
12 |
17 |
22 |
27 |
32 |
37 |
42 |
47 |
52 |
57 |
62 |
67 |
72 |
77 |
82 |
87 |
92 |
97 |
102 |
107 |
112 |
113 |
114 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Mozilla Public License Version 2.0
2 | ==================================
3 |
4 | 1. Definitions
5 | --------------
6 |
7 | 1.1. "Contributor"
8 | means each individual or legal entity that creates, contributes to
9 | the creation of, or owns Covered Software.
10 |
11 | 1.2. "Contributor Version"
12 | means the combination of the Contributions of others (if any) used
13 | by a Contributor and that particular Contributor's Contribution.
14 |
15 | 1.3. "Contribution"
16 | means Covered Software of a particular Contributor.
17 |
18 | 1.4. "Covered Software"
19 | means Source Code Form to which the initial Contributor has attached
20 | the notice in Exhibit A, the Executable Form of such Source Code
21 | Form, and Modifications of such Source Code Form, in each case
22 | including portions thereof.
23 |
24 | 1.5. "Incompatible With Secondary Licenses"
25 | means
26 |
27 | (a) that the initial Contributor has attached the notice described
28 | in Exhibit B to the Covered Software; or
29 |
30 | (b) that the Covered Software was made available under the terms of
31 | version 1.1 or earlier of the License, but not also under the
32 | terms of a Secondary License.
33 |
34 | 1.6. "Executable Form"
35 | means any form of the work other than Source Code Form.
36 |
37 | 1.7. "Larger Work"
38 | means a work that combines Covered Software with other material, in
39 | a separate file or files, that is not Covered Software.
40 |
41 | 1.8. "License"
42 | means this document.
43 |
44 | 1.9. "Licensable"
45 | means having the right to grant, to the maximum extent possible,
46 | whether at the time of the initial grant or subsequently, any and
47 | all of the rights conveyed by this License.
48 |
49 | 1.10. "Modifications"
50 | means any of the following:
51 |
52 | (a) any file in Source Code Form that results from an addition to,
53 | deletion from, or modification of the contents of Covered
54 | Software; or
55 |
56 | (b) any new file in Source Code Form that contains any Covered
57 | Software.
58 |
59 | 1.11. "Patent Claims" of a Contributor
60 | means any patent claim(s), including without limitation, method,
61 | process, and apparatus claims, in any patent Licensable by such
62 | Contributor that would be infringed, but for the grant of the
63 | License, by the making, using, selling, offering for sale, having
64 | made, import, or transfer of either its Contributions or its
65 | Contributor Version.
66 |
67 | 1.12. "Secondary License"
68 | means either the GNU General Public License, Version 2.0, the GNU
69 | Lesser General Public License, Version 2.1, the GNU Affero General
70 | Public License, Version 3.0, or any later versions of those
71 | licenses.
72 |
73 | 1.13. "Source Code Form"
74 | means the form of the work preferred for making modifications.
75 |
76 | 1.14. "You" (or "Your")
77 | means an individual or a legal entity exercising rights under this
78 | License. For legal entities, "You" includes any entity that
79 | controls, is controlled by, or is under common control with You. For
80 | purposes of this definition, "control" means (a) the power, direct
81 | or indirect, to cause the direction or management of such entity,
82 | whether by contract or otherwise, or (b) ownership of more than
83 | fifty percent (50%) of the outstanding shares or beneficial
84 | ownership of such entity.
85 |
86 | 2. License Grants and Conditions
87 | --------------------------------
88 |
89 | 2.1. Grants
90 |
91 | Each Contributor hereby grants You a world-wide, royalty-free,
92 | non-exclusive license:
93 |
94 | (a) under intellectual property rights (other than patent or trademark)
95 | Licensable by such Contributor to use, reproduce, make available,
96 | modify, display, perform, distribute, and otherwise exploit its
97 | Contributions, either on an unmodified basis, with Modifications, or
98 | as part of a Larger Work; and
99 |
100 | (b) under Patent Claims of such Contributor to make, use, sell, offer
101 | for sale, have made, import, and otherwise transfer either its
102 | Contributions or its Contributor Version.
103 |
104 | 2.2. Effective Date
105 |
106 | The licenses granted in Section 2.1 with respect to any Contribution
107 | become effective for each Contribution on the date the Contributor first
108 | distributes such Contribution.
109 |
110 | 2.3. Limitations on Grant Scope
111 |
112 | The licenses granted in this Section 2 are the only rights granted under
113 | this License. No additional rights or licenses will be implied from the
114 | distribution or licensing of Covered Software under this License.
115 | Notwithstanding Section 2.1(b) above, no patent license is granted by a
116 | Contributor:
117 |
118 | (a) for any code that a Contributor has removed from Covered Software;
119 | or
120 |
121 | (b) for infringements caused by: (i) Your and any other third party's
122 | modifications of Covered Software, or (ii) the combination of its
123 | Contributions with other software (except as part of its Contributor
124 | Version); or
125 |
126 | (c) under Patent Claims infringed by Covered Software in the absence of
127 | its Contributions.
128 |
129 | This License does not grant any rights in the trademarks, service marks,
130 | or logos of any Contributor (except as may be necessary to comply with
131 | the notice requirements in Section 3.4).
132 |
133 | 2.4. Subsequent Licenses
134 |
135 | No Contributor makes additional grants as a result of Your choice to
136 | distribute the Covered Software under a subsequent version of this
137 | License (see Section 10.2) or under the terms of a Secondary License (if
138 | permitted under the terms of Section 3.3).
139 |
140 | 2.5. Representation
141 |
142 | Each Contributor represents that the Contributor believes its
143 | Contributions are its original creation(s) or it has sufficient rights
144 | to grant the rights to its Contributions conveyed by this License.
145 |
146 | 2.6. Fair Use
147 |
148 | This License is not intended to limit any rights You have under
149 | applicable copyright doctrines of fair use, fair dealing, or other
150 | equivalents.
151 |
152 | 2.7. Conditions
153 |
154 | Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted
155 | in Section 2.1.
156 |
157 | 3. Responsibilities
158 | -------------------
159 |
160 | 3.1. Distribution of Source Form
161 |
162 | All distribution of Covered Software in Source Code Form, including any
163 | Modifications that You create or to which You contribute, must be under
164 | the terms of this License. You must inform recipients that the Source
165 | Code Form of the Covered Software is governed by the terms of this
166 | License, and how they can obtain a copy of this License. You may not
167 | attempt to alter or restrict the recipients' rights in the Source Code
168 | Form.
169 |
170 | 3.2. Distribution of Executable Form
171 |
172 | If You distribute Covered Software in Executable Form then:
173 |
174 | (a) such Covered Software must also be made available in Source Code
175 | Form, as described in Section 3.1, and You must inform recipients of
176 | the Executable Form how they can obtain a copy of such Source Code
177 | Form by reasonable means in a timely manner, at a charge no more
178 | than the cost of distribution to the recipient; and
179 |
180 | (b) You may distribute such Executable Form under the terms of this
181 | License, or sublicense it under different terms, provided that the
182 | license for the Executable Form does not attempt to limit or alter
183 | the recipients' rights in the Source Code Form under this License.
184 |
185 | 3.3. Distribution of a Larger Work
186 |
187 | You may create and distribute a Larger Work under terms of Your choice,
188 | provided that You also comply with the requirements of this License for
189 | the Covered Software. If the Larger Work is a combination of Covered
190 | Software with a work governed by one or more Secondary Licenses, and the
191 | Covered Software is not Incompatible With Secondary Licenses, this
192 | License permits You to additionally distribute such Covered Software
193 | under the terms of such Secondary License(s), so that the recipient of
194 | the Larger Work may, at their option, further distribute the Covered
195 | Software under the terms of either this License or such Secondary
196 | License(s).
197 |
198 | 3.4. Notices
199 |
200 | You may not remove or alter the substance of any license notices
201 | (including copyright notices, patent notices, disclaimers of warranty,
202 | or limitations of liability) contained within the Source Code Form of
203 | the Covered Software, except that You may alter any license notices to
204 | the extent required to remedy known factual inaccuracies.
205 |
206 | 3.5. Application of Additional Terms
207 |
208 | You may choose to offer, and to charge a fee for, warranty, support,
209 | indemnity or liability obligations to one or more recipients of Covered
210 | Software. However, You may do so only on Your own behalf, and not on
211 | behalf of any Contributor. You must make it absolutely clear that any
212 | such warranty, support, indemnity, or liability obligation is offered by
213 | You alone, and You hereby agree to indemnify every Contributor for any
214 | liability incurred by such Contributor as a result of warranty, support,
215 | indemnity or liability terms You offer. You may include additional
216 | disclaimers of warranty and limitations of liability specific to any
217 | jurisdiction.
218 |
219 | 4. Inability to Comply Due to Statute or Regulation
220 | ---------------------------------------------------
221 |
222 | If it is impossible for You to comply with any of the terms of this
223 | License with respect to some or all of the Covered Software due to
224 | statute, judicial order, or regulation then You must: (a) comply with
225 | the terms of this License to the maximum extent possible; and (b)
226 | describe the limitations and the code they affect. Such description must
227 | be placed in a text file included with all distributions of the Covered
228 | Software under this License. Except to the extent prohibited by statute
229 | or regulation, such description must be sufficiently detailed for a
230 | recipient of ordinary skill to be able to understand it.
231 |
232 | 5. Termination
233 | --------------
234 |
235 | 5.1. The rights granted under this License will terminate automatically
236 | if You fail to comply with any of its terms. However, if You become
237 | compliant, then the rights granted under this License from a particular
238 | Contributor are reinstated (a) provisionally, unless and until such
239 | Contributor explicitly and finally terminates Your grants, and (b) on an
240 | ongoing basis, if such Contributor fails to notify You of the
241 | non-compliance by some reasonable means prior to 60 days after You have
242 | come back into compliance. Moreover, Your grants from a particular
243 | Contributor are reinstated on an ongoing basis if such Contributor
244 | notifies You of the non-compliance by some reasonable means, this is the
245 | first time You have received notice of non-compliance with this License
246 | from such Contributor, and You become compliant prior to 30 days after
247 | Your receipt of the notice.
248 |
249 | 5.2. If You initiate litigation against any entity by asserting a patent
250 | infringement claim (excluding declaratory judgment actions,
251 | counter-claims, and cross-claims) alleging that a Contributor Version
252 | directly or indirectly infringes any patent, then the rights granted to
253 | You by any and all Contributors for the Covered Software under Section
254 | 2.1 of this License shall terminate.
255 |
256 | 5.3. In the event of termination under Sections 5.1 or 5.2 above, all
257 | end user license agreements (excluding distributors and resellers) which
258 | have been validly granted by You or Your distributors under this License
259 | prior to termination shall survive termination.
260 |
261 | ************************************************************************
262 | * *
263 | * 6. Disclaimer of Warranty *
264 | * ------------------------- *
265 | * *
266 | * Covered Software is provided under this License on an "as is" *
267 | * basis, without warranty of any kind, either expressed, implied, or *
268 | * statutory, including, without limitation, warranties that the *
269 | * Covered Software is free of defects, merchantable, fit for a *
270 | * particular purpose or non-infringing. The entire risk as to the *
271 | * quality and performance of the Covered Software is with You. *
272 | * Should any Covered Software prove defective in any respect, You *
273 | * (not any Contributor) assume the cost of any necessary servicing, *
274 | * repair, or correction. This disclaimer of warranty constitutes an *
275 | * essential part of this License. No use of any Covered Software is *
276 | * authorized under this License except under this disclaimer. *
277 | * *
278 | ************************************************************************
279 |
280 | ************************************************************************
281 | * *
282 | * 7. Limitation of Liability *
283 | * -------------------------- *
284 | * *
285 | * Under no circumstances and under no legal theory, whether tort *
286 | * (including negligence), contract, or otherwise, shall any *
287 | * Contributor, or anyone who distributes Covered Software as *
288 | * permitted above, be liable to You for any direct, indirect, *
289 | * special, incidental, or consequential damages of any character *
290 | * including, without limitation, damages for lost profits, loss of *
291 | * goodwill, work stoppage, computer failure or malfunction, or any *
292 | * and all other commercial damages or losses, even if such party *
293 | * shall have been informed of the possibility of such damages. This *
294 | * limitation of liability shall not apply to liability for death or *
295 | * personal injury resulting from such party's negligence to the *
296 | * extent applicable law prohibits such limitation. Some *
297 | * jurisdictions do not allow the exclusion or limitation of *
298 | * incidental or consequential damages, so this exclusion and *
299 | * limitation may not apply to You. *
300 | * *
301 | ************************************************************************
302 |
303 | 8. Litigation
304 | -------------
305 |
306 | Any litigation relating to this License may be brought only in the
307 | courts of a jurisdiction where the defendant maintains its principal
308 | place of business and such litigation shall be governed by laws of that
309 | jurisdiction, without reference to its conflict-of-law provisions.
310 | Nothing in this Section shall prevent a party's ability to bring
311 | cross-claims or counter-claims.
312 |
313 | 9. Miscellaneous
314 | ----------------
315 |
316 | This License represents the complete agreement concerning the subject
317 | matter hereof. If any provision of this License is held to be
318 | unenforceable, such provision shall be reformed only to the extent
319 | necessary to make it enforceable. Any law or regulation which provides
320 | that the language of a contract shall be construed against the drafter
321 | shall not be used to construe this License against a Contributor.
322 |
323 | 10. Versions of the License
324 | ---------------------------
325 |
326 | 10.1. New Versions
327 |
328 | Mozilla Foundation is the license steward. Except as provided in Section
329 | 10.3, no one other than the license steward has the right to modify or
330 | publish new versions of this License. Each version will be given a
331 | distinguishing version number.
332 |
333 | 10.2. Effect of New Versions
334 |
335 | You may distribute the Covered Software under the terms of the version
336 | of the License under which You originally received the Covered Software,
337 | or under the terms of any subsequent version published by the license
338 | steward.
339 |
340 | 10.3. Modified Versions
341 |
342 | If you create software not governed by this License, and you want to
343 | create a new license for such software, you may create and use a
344 | modified version of this License if you rename the license and remove
345 | any references to the name of the license steward (except to note that
346 | such modified license differs from this License).
347 |
348 | 10.4. Distributing Source Code Form that is Incompatible With Secondary
349 | Licenses
350 |
351 | If You choose to distribute Source Code Form that is Incompatible With
352 | Secondary Licenses under the terms of this version of the License, the
353 | notice described in Exhibit B of this License must be attached.
354 |
355 | Exhibit A - Source Code Form License Notice
356 | -------------------------------------------
357 |
358 | This Source Code Form is subject to the terms of the Mozilla Public
359 | License, v. 2.0. If a copy of the MPL was not distributed with this
360 | file, You can obtain one at http://mozilla.org/MPL/2.0/.
361 |
362 | If it is not possible or desirable to put the notice in a particular
363 | file, then You may include the notice in a location (such as a LICENSE
364 | file in a relevant directory) where a recipient would be likely to look
365 | for such a notice.
366 |
367 | You may add additional accurate notices of copyright ownership.
368 |
369 | Exhibit B - "Incompatible With Secondary Licenses" Notice
370 | ---------------------------------------------------------
371 |
372 | This Source Code Form is "Incompatible With Secondary Licenses", as
373 | defined by the Mozilla Public License, v. 2.0.
374 |
--------------------------------------------------------------------------------
/app/src/main/java/com/bluewindtalker/camera/demo/CameraActivity.java:
--------------------------------------------------------------------------------
1 | package com.bluewindtalker.camera.demo;
2 |
3 | import android.Manifest;
4 | import android.app.Activity;
5 | import android.content.pm.PackageManager;
6 | import android.content.res.Resources;
7 | import android.graphics.Bitmap;
8 | import android.graphics.BitmapFactory;
9 | import android.graphics.Matrix;
10 | import android.hardware.Camera;
11 | import android.hardware.Sensor;
12 | import android.hardware.SensorEvent;
13 | import android.hardware.SensorEventListener;
14 | import android.hardware.SensorManager;
15 | import android.os.Build;
16 | import android.support.annotation.NonNull;
17 | import android.support.v4.app.ActivityCompat;
18 | import android.support.v4.content.ContextCompat;
19 | import android.support.v7.app.AppCompatActivity;
20 | import android.os.Bundle;
21 | import android.util.DisplayMetrics;
22 | import android.util.Log;
23 | import android.view.Surface;
24 | import android.view.SurfaceHolder;
25 | import android.view.SurfaceView;
26 | import android.view.View;
27 | import android.widget.Button;
28 | import android.widget.FrameLayout;
29 | import android.widget.ImageView;
30 | import android.widget.TextView;
31 | import android.widget.Toast;
32 |
33 | import java.io.IOException;
34 | import java.util.List;
35 |
36 | /**
37 | * @author bluewindtalker
38 | * @description 相机demo
39 | * @date 2018/4/14-下午12:08
40 | */
41 |
42 | public class CameraActivity extends AppCompatActivity implements View.OnClickListener {
43 | private final String TAG = this.getClass().getSimpleName();
44 | /**
45 | * 预览控件
46 | */
47 | private SurfaceView displaySfv;
48 |
49 | /**
50 | * 拍照按钮
51 | */
52 | private Button takePicBtn;
53 |
54 | /**
55 | * 照片控件容器
56 | */
57 | private FrameLayout picFl;
58 |
59 | /**
60 | * 照片容器
61 | */
62 | private ImageView picIV;
63 |
64 | /**
65 | * 提示开灯的tv
66 | */
67 | private TextView lightTV;
68 |
69 | private Camera camera;
70 |
71 | private boolean isRequestPermission = false;
72 |
73 | /**
74 | * 获得传感器管理者
75 | */
76 | private SensorManager sensorManager;
77 |
78 | /**
79 | * 当前打开摄像头类型,当前写的是后置摄像头
80 | */
81 | private int currentCameraType = Camera.CameraInfo.CAMERA_FACING_BACK;
82 |
83 | //需要申请的权限
84 | private String[] permissions = {Manifest.permission.CAMERA};
85 | private static final int CAMERA_PERMISSION_CODE = 1;
86 |
87 | @Override
88 | protected void onCreate(Bundle savedInstanceState) {
89 | super.onCreate(savedInstanceState);
90 | setContentView(R.layout.activity_camera);
91 |
92 | displaySfv = findViewById(R.id.sf_display_demo_camera);
93 | takePicBtn = findViewById(R.id.btn_take_picture_demo_camera);
94 | picFl = findViewById(R.id.fl_picture_demo_camera);
95 | picIV = findViewById(R.id.iv_picture_demo_camera);
96 | lightTV = findViewById(R.id.tv_light_demo_camera);
97 | takePicBtn.setOnClickListener(this);
98 | picFl.setOnClickListener(this);
99 |
100 | sensorManager = LightSensorUtil.getSenosrManager(this);
101 | }
102 |
103 | private SensorEventListener lightSensorListener = new SensorEventListener() {
104 | @Override
105 | public void onSensorChanged(SensorEvent event) {
106 | if (event.sensor.getType() == Sensor.TYPE_LIGHT) {
107 | //光线强度
108 | float lux = event.values[0];
109 | Log.e(TAG, "光线传感器得到的光线强度-->" + lux);
110 | }
111 | }
112 |
113 | @Override
114 | public void onAccuracyChanged(Sensor sensor, int accuracy) {
115 | }
116 | };
117 |
118 | @Override
119 | protected void onResume() {
120 | super.onResume();
121 | Log.e(TAG, "onResume");
122 | LightSensorUtil.registerLightSensor(sensorManager, lightSensorListener);
123 |
124 | if (!isRequestPermission) {
125 | checkAndInitCamera();
126 | }
127 | }
128 |
129 | @Override
130 | protected void onPause() {
131 | super.onPause();
132 | LightSensorUtil.unregisterLightSensor(sensorManager, lightSensorListener);
133 |
134 | Log.e(TAG, "onPause");
135 | releaseCamera();
136 |
137 | }
138 |
139 | private void releaseCamera() {
140 | if (camera != null) {
141 | camera.stopPreview();
142 | camera.release();
143 | camera = null;
144 | }
145 | }
146 |
147 | @Override
148 | public void onClick(View v) {
149 | if (v.getId() == R.id.btn_take_picture_demo_camera) {
150 | takePicture();
151 | takePicBtn.setVisibility(View.GONE);
152 | } else if (v.getId() == R.id.fl_picture_demo_camera) {
153 | takePicBtn.setVisibility(View.VISIBLE);
154 | picFl.setVisibility(View.GONE);
155 | picIV.setImageBitmap(null);
156 | checkAndInitCamera();
157 | }
158 | }
159 |
160 | private void checkAndInitCamera() {
161 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
162 | // 检查该权限是否已经获取
163 | int i = ContextCompat.checkSelfPermission(this, permissions[0]);
164 | // 权限是否已经 授权 GRANTED---授权 DINIED---拒绝
165 | if (i != PackageManager.PERMISSION_GRANTED) {
166 | // 如果没有授予该权限,就去提示用户请求
167 | isRequestPermission = true;
168 | ActivityCompat.requestPermissions(this, permissions, CAMERA_PERMISSION_CODE);
169 | } else {
170 | initCamera();
171 | }
172 | } else {
173 | initCamera();
174 | }
175 | }
176 |
177 | @Override
178 | public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
179 | super.onRequestPermissionsResult(requestCode, permissions, grantResults);
180 | Log.e(TAG, "onRequestPermissionsResult");
181 | if (requestCode == CAMERA_PERMISSION_CODE) {
182 | boolean isGranted = false;
183 | if (grantResults != null && grantResults.length > 0) {
184 | isGranted = true;
185 | for (int i = 0; i < grantResults.length; i++) {
186 | if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
187 | isGranted = false;
188 | break;
189 | }
190 | }
191 | }
192 | if (isGranted) {
193 | initCamera();
194 | } else {
195 | Toast.makeText(this, "请去设置授权", Toast.LENGTH_SHORT).show();
196 | }
197 | }
198 | }
199 |
200 | /**
201 | * 初始化照片
202 | */
203 | private void initCamera() {
204 | if (camera != null) {
205 | camera.startPreview();
206 | setPreviewLight();
207 | }
208 | Log.e(TAG, "initCamera");
209 | //1. Obtain an instance of Camera from open(int).
210 | //这里可以根据前后摄像头设置
211 | camera = openCamera(currentCameraType);
212 | if (camera == null) {
213 | return;
214 | }
215 | //2. Get existing (default) settings with getParameters().
216 | //获得存在的默认配置属性
217 | Camera.Parameters parameters = camera.getParameters();
218 |
219 | //3. If necessary, modify the returned Camera.Parameters object and call setParameters(Camera.Parameters).
220 | //可以根据需要修改属性,这些属性包括是否自动持续对焦、拍摄的gps信息、图片视频格式及大小、预览的fps、
221 | // 白平衡和自动曝光补偿、自动对焦区域、闪光灯状态等。
222 | //具体可以参阅https://developer.android.com/reference/android/hardware/Camera.Parameters.html
223 | if (parameters.getSupportedFocusModes().contains(Camera.Parameters
224 | .FOCUS_MODE_CONTINUOUS_PICTURE)) {
225 | //自动持续对焦
226 | parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
227 | }
228 | //在设置图片和预览的大小时要注意当前摄像头支持的大小,不同手机支持的大小不同,如果你的SurfaceView不是全屏,有可能被拉伸。
229 | // parameters.getSupportedPreviewSizes(),parameters.getSupportedPictureSizes()
230 | List picSizes = parameters.getSupportedPictureSizes();
231 | Resources resources = this.getResources();
232 | DisplayMetrics dm = resources.getDisplayMetrics();
233 | float density = dm.density;
234 | int width = dm.widthPixels;
235 | int height = dm.heightPixels;
236 | Camera.Size picSize = getPictureSize(picSizes, width, height);
237 | parameters.setPictureSize(picSize.width, picSize.height);
238 | camera.setParameters(parameters);
239 | //4. Call setDisplayOrientation(int) to ensure correct orientation of preview.
240 | //你可能会遇到画面方向和手机的方向不一致的问题,竖向手机的时候,但是画面是横的,这是由于摄像头默认捕获的画面横向的
241 | // 通过调用setDisplayOrientation来设置PreviewDisplay的方向,可以解决这个问题。
242 | setCameraDisplayOrientation(this, currentCameraType, camera);
243 |
244 | //5. Important: Pass a fully initialized SurfaceHolder to setPreviewDisplay(SurfaceHolder).
245 | // Without a surface, the camera will be unable to start the preview.
246 | //camera必须绑定一个surfaceview才可以正常显示。
247 | try {
248 | camera.setPreviewDisplay(displaySfv.getHolder());
249 | } catch (IOException e) {
250 | e.printStackTrace();
251 | }
252 | //6. Important: Call startPreview() to start updating the preview surface.
253 | // Preview must be started before you can take a picture.
254 | //在调用拍照之前必须调用startPreview()方法,但是在此时有可能surface还未创建成功。
255 | // 所以加上SurfaceHolder.Callback(),在回调再次初始化下。
256 | camera.startPreview();
257 | setPreviewLight();
258 | //7. When you want, call
259 | // takePicture(Camera.ShutterCallback, Camera.PictureCallback, Camera.PictureCallback, Camera.PictureCallback)
260 | // to capture a photo. Wait for the callbacks to provide the actual image data.
261 | //当如果想要拍照的时候,调用takePicture方法,这个下面我们会讲到。
262 |
263 | //8. After taking a picture, preview display will have stopped. To take more photos, call startPreview() again first.
264 | //在拍照结束后相机预览将会关闭,如果要再次拍照需要再次调用startPreview()
265 |
266 | //9. Call stopPreview() to stop updating the preview surface.
267 | //通过调用stopPreview方法可以结束预览
268 | //10. Important: Call release() to release the camera for use by other applications.
269 | // Applications should release the camera immediately in onPause()(and re-open() it in onResume()).
270 | //建议在onResume调用open的方法,在onPause的时候执行release方法
271 |
272 | SurfaceHolder holder = displaySfv.getHolder();
273 | if (holder != null) {
274 | if(mCallBack != null){
275 | holder.removeCallback(mCallBack);
276 | }
277 | mCallBack = new SurfaceHolder.Callback() {
278 | @Override
279 | public void surfaceCreated(SurfaceHolder holder) {
280 | Log.e(TAG, "surfaceCreated" + holder + this);
281 | checkAndInitCamera();
282 |
283 | }
284 |
285 | @Override
286 | public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
287 | Log.e(TAG, "surfaceChanged" + holder + this);
288 | }
289 |
290 | @Override
291 | public void surfaceDestroyed(SurfaceHolder holder) {
292 | Log.e(TAG, "surfaceDestroyed" + holder + this);
293 | }
294 | };
295 | holder.addCallback(mCallBack);
296 | }
297 | }
298 | private SurfaceHolder.Callback mCallBack;
299 |
300 | /**
301 | * 获得最合是的宽高size
302 | */
303 | private Camera.Size getPictureSize(List picSizes, int width, int height) {
304 | Camera.Size betterSize = null;
305 | int diff = Integer.MAX_VALUE;
306 | if (picSizes != null && picSizes.size() > 0) {
307 | for (Camera.Size size : picSizes) {
308 | int newDiff = Math.abs(size.width - width) + Math.abs(size.height - height);
309 | if (newDiff == 0) {
310 | return size;
311 | }
312 | if (newDiff < diff) {
313 | betterSize = size;
314 | diff = newDiff;
315 | }
316 | }
317 | }
318 | return betterSize;
319 | }
320 |
321 | private Camera openCamera(int type) {
322 | int cameraTypeIndex = -1;
323 | int cameraCount = Camera.getNumberOfCameras();
324 | Camera.CameraInfo info = new Camera.CameraInfo();
325 | for (int cameraIndex = 0; cameraIndex < cameraCount; cameraIndex++) {
326 | Camera.getCameraInfo(cameraIndex, info);
327 | if (info.facing == type) {
328 | cameraTypeIndex = cameraIndex;
329 | break;
330 | }
331 | }
332 | if (cameraTypeIndex != -1) {
333 | return Camera.open(cameraTypeIndex);
334 | }
335 | return null;
336 | }
337 |
338 | /**
339 | * 拍摄照片
340 | */
341 | private void takePicture() {
342 | picIV.setImageBitmap(null);
343 | if (camera == null) {
344 | return;
345 | }
346 | //如果不加第一个回调,手机会没有拍照音效,第二个回调是返回raw格式图片,
347 | // 了解过相机的人可能知道这是原图的意思,这个我们不处理,我们处理第三个回调,jpg格式的数据
348 | // 拍摄照片
349 | camera.takePicture(new Camera.ShutterCallback() {
350 | @Override
351 | public void onShutter() {
352 | }
353 | }, null, new Camera.PictureCallback() {
354 | @Override
355 | public void onPictureTaken(byte[] data, Camera camera) {
356 | // 将拍照数据data数组转化为Bitmap,这里应该放到线程执行了,这里为了简单处理直接放UI线程了
357 | Bitmap imageBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
358 | //一般手机需要旋转90度来适应方向,如果setCameraDisplayOrientation得到的结果不是90度,一般还需要再次旋转180
359 | picIV.setImageBitmap(rotate(imageBitmap, 90));
360 | picFl.setVisibility(View.VISIBLE);
361 | }
362 | });
363 | }
364 |
365 | public Bitmap rotate(Bitmap bitmap, int degree) {
366 | Matrix matrix = new Matrix();
367 | matrix.postRotate(degree);
368 | return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, false);
369 | }
370 |
371 | //设置相机的方向
372 | public int setCameraDisplayOrientation(Activity activity, int cameraId, android.hardware.Camera camera) {
373 | android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
374 | android.hardware.Camera.getCameraInfo(cameraId, info);
375 | int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
376 | int degrees = 0;
377 | switch (rotation) {
378 | case Surface.ROTATION_0:
379 | degrees = 0;
380 | break;
381 | case Surface.ROTATION_90:
382 | degrees = 90;
383 | break;
384 | case Surface.ROTATION_180:
385 | degrees = 180;
386 | break;
387 | case Surface.ROTATION_270:
388 | degrees = 270;
389 | break;
390 | default:
391 | degrees = 0;
392 | break;
393 | }
394 | int result;
395 | if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
396 | result = (info.orientation + degrees) % 360;
397 | result = (360 - result) % 360; // compensate the mirror
398 | } else {
399 | // back-facing
400 | result = (info.orientation - degrees + 360) % 360;
401 | }
402 | camera.setDisplayOrientation(result);
403 | return degrees;
404 | }
405 |
406 |
407 | //上次记录的时间戳
408 | long lastRecordTime = System.currentTimeMillis();
409 |
410 | //上次记录的索引
411 | int darkIndex = 0;
412 | //一个历史记录的数组,255是代表亮度最大值
413 | long[] darkList = new long[]{255, 255, 255, 255};
414 | //扫描间隔
415 | int waitScanTime = 300;
416 |
417 | //亮度低的阀值
418 | int darkValue = 60;
419 | private void setPreviewLight() {
420 | //不需要的时候直接清空
421 | // if(noNeed){
422 | // camera.setPreviewCallback(null);
423 | // return;
424 | // }
425 | camera.setPreviewCallback(new Camera.PreviewCallback() {
426 | @Override
427 | public void onPreviewFrame(byte[] data, Camera camera) {
428 | long currentTime = System.currentTimeMillis();
429 | if (currentTime - lastRecordTime < waitScanTime) {
430 | return;
431 | }
432 | lastRecordTime = currentTime;
433 |
434 | int width = camera.getParameters().getPreviewSize().width;
435 | int height = camera.getParameters().getPreviewSize().height;
436 | //像素点的总亮度
437 | long pixelLightCount = 0L;
438 | //像素点的总数
439 | long pixeCount = width * height;
440 | //采集步长,因为没有必要每个像素点都采集,可以跨一段采集一个,减少计算负担,必须大于等于1。
441 | int step = 10;
442 | //data.length - allCount * 1.5f的目的是判断图像格式是不是YUV420格式,只有是这种格式才相等
443 | //因为int整形与float浮点直接比较会出问题,所以这么比
444 | if (Math.abs(data.length - pixeCount * 1.5f) < 0.00001f) {
445 | for (int i = 0; i < pixeCount; i += step) {
446 | //如果直接加是不行的,因为data[i]记录的是色值并不是数值,byte的范围是+127到—128,
447 | // 而亮度FFFFFF是11111111是-127,所以这里需要先转为无符号unsigned long参考Byte.toUnsignedLong()
448 | pixelLightCount += ((long) data[i]) & 0xffL;
449 | }
450 | //平均亮度
451 | long cameraLight = pixelLightCount / (pixeCount / step);
452 | //更新历史记录
453 | int lightSize = darkList.length;
454 | darkList[darkIndex = darkIndex % lightSize] = cameraLight;
455 | darkIndex++;
456 | boolean isDarkEnv = true;
457 | //判断在时间范围waitScanTime * lightSize内是不是亮度过暗
458 | for (int i = 0; i < lightSize; i++) {
459 | if (darkList[i] > darkValue) {
460 | isDarkEnv = false;
461 | }
462 | }
463 | Log.e(TAG, "摄像头环境亮度为 : " + cameraLight);
464 | if (!isFinishing()) {
465 | //亮度过暗就提醒
466 | if (isDarkEnv) {
467 | lightTV.setVisibility(View.VISIBLE);
468 | } else {
469 | lightTV.setVisibility(View.GONE);
470 | }
471 | }
472 | }
473 | }
474 | });
475 | }
476 | }
477 |
--------------------------------------------------------------------------------