├── app ├── .gitignore ├── src │ └── main │ │ ├── res │ │ ├── mipmap-xhdpi │ │ │ ├── ic_launcher.png │ │ │ ├── camera_button.png │ │ │ ├── image_default.png │ │ │ └── camera_button_pressed.png │ │ ├── values │ │ │ ├── colors.xml │ │ │ ├── strings.xml │ │ │ └── styles.xml │ │ ├── xml │ │ │ └── file_paths.xml │ │ ├── drawable │ │ │ └── selector.xml │ │ └── layout │ │ │ ├── dialog.xml │ │ │ ├── activity_main.xml │ │ │ ├── activity_camera.xml │ │ │ └── activity_camera2.xml │ │ ├── java │ │ └── com │ │ │ └── xdandroid │ │ │ └── hellocamera2 │ │ │ ├── app │ │ │ ├── App.java │ │ │ ├── BaseCameraActivity.java │ │ │ └── BaseActivity.java │ │ │ ├── view │ │ │ └── CameraPreview.java │ │ │ ├── util │ │ │ ├── Camera2Utils.java │ │ │ ├── CommonUtils.java │ │ │ ├── FrescoUtils.java │ │ │ ├── CameraUtils.java │ │ │ └── BitmapUtils.java │ │ │ ├── CameraActivity.java │ │ │ ├── MainActivity.java │ │ │ └── Camera2Activity.java │ │ └── AndroidManifest.xml ├── proguard-rules.pro └── build.gradle ├── settings.gradle ├── gradle.properties ├── screenshot_camera.png ├── screenshot_main.png ├── screenshot_preview.png ├── .idea ├── copyright │ └── profiles_settings.xml ├── encodings.xml ├── inspectionProfiles │ ├── profiles_settings.xml │ └── Project_Default.xml ├── modules.xml ├── runConfigurations.xml ├── gradle.xml ├── compiler.xml └── misc.xml ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── .gitignore ├── LICENSE ├── gradlew.bat ├── README_zh.md ├── gradlew └── README.md /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app' 2 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | org.gradle.jvmargs=-Dfile.encoding=UTF-8 2 | -------------------------------------------------------------------------------- /screenshot_camera.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xingda920813/HelloCamera2/HEAD/screenshot_camera.png -------------------------------------------------------------------------------- /screenshot_main.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xingda920813/HelloCamera2/HEAD/screenshot_main.png -------------------------------------------------------------------------------- /screenshot_preview.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xingda920813/HelloCamera2/HEAD/screenshot_preview.png -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xingda920813/HelloCamera2/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xingda920813/HelloCamera2/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/camera_button.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xingda920813/HelloCamera2/HEAD/app/src/main/res/mipmap-xhdpi/camera_button.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/image_default.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xingda920813/HelloCamera2/HEAD/app/src/main/res/mipmap-xhdpi/image_default.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/camera_button_pressed.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xingda920813/HelloCamera2/HEAD/app/src/main/res/mipmap-xhdpi/camera_button_pressed.png -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #FFC107 4 | #FFA000 5 | #3D98FF 6 | 7 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Mon Dec 28 10:00:20 PST 2015 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/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | HelloCamera2 3 | HelloCamera2 4 | 拍照 5 | 拍照 6 | 7 | -------------------------------------------------------------------------------- /.idea/inspectionProfiles/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/xml/file_paths.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/selector.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/layout/dialog.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 14 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | 18 | -------------------------------------------------------------------------------- /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 C:\Users\XingDa\AppData\Local\Android\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 | -------------------------------------------------------------------------------- /app/src/main/java/com/xdandroid/hellocamera2/app/App.java: -------------------------------------------------------------------------------- 1 | package com.xdandroid.hellocamera2.app; 2 | 3 | import android.app.*; 4 | import android.os.*; 5 | 6 | import com.facebook.drawee.backends.pipeline.*; 7 | import com.facebook.imagepipeline.core.*; 8 | 9 | public class App extends Application { 10 | 11 | /** 12 | * 启动照相Intent的RequestCode.自定义相机. 13 | */ 14 | public static final int TAKE_PHOTO_CUSTOM = 100; 15 | /** 16 | * 启动照相Intent的RequestCode.系统相机. 17 | */ 18 | public static final int TAKE_PHOTO_SYSTEM = 200; 19 | /** 20 | * 主线程Handler. 21 | */ 22 | public static Handler mHandler; 23 | public static App sApp; 24 | 25 | @Override 26 | public void onCreate() { 27 | super.onCreate(); 28 | sApp = this; 29 | mHandler = new Handler(); 30 | Fresco.initialize(this, ImagePipelineConfig 31 | .newBuilder(this) 32 | .setDownsampleEnabled(true) 33 | .build()); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Da Xing 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 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 26 5 | buildToolsVersion "26.0.2" 6 | defaultConfig { 7 | applicationId "com.xdandroid.hellocamera2" 8 | minSdkVersion 14 9 | targetSdkVersion 26 10 | versionCode 1 11 | versionName "1.0.0" 12 | } 13 | buildTypes { 14 | release { 15 | minifyEnabled false 16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 17 | } 18 | } 19 | lintOptions { 20 | disable 'MissingTranslation' 21 | } 22 | compileOptions { 23 | targetCompatibility JavaVersion.VERSION_1_8 24 | sourceCompatibility JavaVersion.VERSION_1_8 25 | } 26 | } 27 | 28 | repositories { 29 | google() 30 | jcenter() 31 | maven { url "https://jitpack.io" } 32 | } 33 | 34 | dependencies { 35 | api 'com.android.support:appcompat-v7:+' 36 | api 'com.android.support:design:+' 37 | api 'com.zhy:autolayout:+' 38 | api 'com.facebook.fresco:fresco:+' 39 | api 'com.jakewharton:butterknife:+' 40 | annotationProcessor 'com.jakewharton:butterknife-compiler:+' 41 | api 'com.jakewharton.rxbinding2:rxbinding:2.+' 42 | api 'gun0912.ted:tedpermission:2.+' 43 | api 'gun0912.ted:tedpermission-rx2:2.+' 44 | } 45 | -------------------------------------------------------------------------------- /app/src/main/java/com/xdandroid/hellocamera2/app/BaseCameraActivity.java: -------------------------------------------------------------------------------- 1 | package com.xdandroid.hellocamera2.app; 2 | 3 | import android.os.*; 4 | import android.support.annotation.*; 5 | import android.view.*; 6 | 7 | import com.zhy.autolayout.*; 8 | 9 | import butterknife.*; 10 | 11 | public abstract class BaseCameraActivity extends AutoLayoutActivity { 12 | 13 | Unbinder mUnbinder; 14 | 15 | @Override 16 | protected void onCreate(@Nullable Bundle savedInstanceState) { 17 | super.onCreate(savedInstanceState); 18 | setContentView(getContentViewResId()); 19 | 20 | mUnbinder = ButterKnife.bind(this); 21 | preInitData(); 22 | } 23 | 24 | protected abstract int getContentViewResId(); 25 | 26 | protected abstract void preInitData(); 27 | 28 | @Override 29 | public boolean onCreateOptionsMenu(Menu menu) { 30 | return true; 31 | } 32 | 33 | @Override 34 | public boolean onOptionsItemSelected(MenuItem item) { 35 | int id = item.getItemId(); 36 | if (id == android.R.id.home) { 37 | finish(); 38 | } 39 | return true; 40 | } 41 | 42 | @Override 43 | protected void onDestroy() { 44 | super.onDestroy(); 45 | if (mUnbinder != null) { 46 | mUnbinder.unbind(); 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 13 | 14 | 17 | 18 | 22 | 23 | 31 | 32 | -------------------------------------------------------------------------------- /app/src/main/java/com/xdandroid/hellocamera2/app/BaseActivity.java: -------------------------------------------------------------------------------- 1 | package com.xdandroid.hellocamera2.app; 2 | 3 | import android.os.*; 4 | import android.support.annotation.*; 5 | import android.support.v7.widget.*; 6 | import android.view.*; 7 | 8 | import com.xdandroid.hellocamera2.R; 9 | import com.zhy.autolayout.*; 10 | 11 | import butterknife.*; 12 | 13 | public abstract class BaseActivity extends AutoLayoutActivity { 14 | 15 | Unbinder mUnbinder; 16 | 17 | @Override 18 | protected void onCreate(@Nullable Bundle savedInstanceState) { 19 | super.onCreate(savedInstanceState); 20 | setContentView(getContentViewResId()); 21 | Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 22 | setSupportActionBar(toolbar); 23 | 24 | mUnbinder = ButterKnife.bind(this); 25 | preInitData(); 26 | } 27 | 28 | protected abstract int getContentViewResId(); 29 | 30 | protected abstract void preInitData(); 31 | 32 | @Override 33 | public boolean onCreateOptionsMenu(Menu menu) { 34 | return true; 35 | } 36 | 37 | @Override 38 | public boolean onOptionsItemSelected(MenuItem item) { 39 | int id = item.getItemId(); 40 | if (id == android.R.id.home) { 41 | finish(); 42 | } 43 | return true; 44 | } 45 | 46 | @Override 47 | protected void onDestroy() { 48 | super.onDestroy(); 49 | if (mUnbinder != null) { 50 | mUnbinder.unbind(); 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /.idea/inspectionProfiles/Project_Default.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 27 | -------------------------------------------------------------------------------- /app/src/main/java/com/xdandroid/hellocamera2/view/CameraPreview.java: -------------------------------------------------------------------------------- 1 | package com.xdandroid.hellocamera2.view; 2 | 3 | import android.annotation.*; 4 | import android.content.*; 5 | import android.hardware.*; 6 | import android.view.*; 7 | 8 | @SuppressWarnings("deprecation") 9 | @SuppressLint("ViewConstructor") 10 | public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback { 11 | 12 | /** 13 | * 需要让Activity/Fragment处理Throwable时,提供的回调监听类. 14 | */ 15 | public interface ThrowableListener { 16 | void onThrowable(Throwable throwable, boolean showToast); 17 | } 18 | 19 | SurfaceHolder mHolder; 20 | Camera mCamera; 21 | ThrowableListener mThrowableListener; 22 | 23 | public CameraPreview(Context context, Camera camera, ThrowableListener l) { 24 | super(context); 25 | mCamera = camera; 26 | mHolder = getHolder(); 27 | mHolder.addCallback(this); 28 | mThrowableListener = l; 29 | } 30 | 31 | @Override 32 | public void surfaceCreated(SurfaceHolder holder) { 33 | try { 34 | mCamera.setPreviewDisplay(holder); 35 | mCamera.startPreview(); 36 | } catch (Exception e) { 37 | e.printStackTrace(); 38 | if (mThrowableListener != null) mThrowableListener.onThrowable(e, true); 39 | } 40 | } 41 | 42 | /** 43 | * 由Activity/Fragment处理Camera的释放. 44 | * 45 | * @param holder SurfaceHolder 46 | */ 47 | @Override 48 | public void surfaceDestroyed(SurfaceHolder holder) { 49 | try {mHolder.removeCallback(this);} catch (Exception ignored) {} 50 | } 51 | 52 | @Override 53 | public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { 54 | if (mHolder.getSurface() == null) return; 55 | try {mCamera.stopPreview();} catch (Exception ignored) {} 56 | try { 57 | mCamera.setPreviewDisplay(mHolder); 58 | mCamera.startPreview(); 59 | } catch (Exception e) { 60 | e.printStackTrace(); 61 | if (mThrowableListener != null) mThrowableListener.onThrowable(e, false); 62 | } 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /app/src/main/java/com/xdandroid/hellocamera2/util/Camera2Utils.java: -------------------------------------------------------------------------------- 1 | package com.xdandroid.hellocamera2.util; 2 | 3 | import android.os.*; 4 | import android.support.annotation.*; 5 | import android.util.Size; 6 | 7 | import java.util.*; 8 | 9 | @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) 10 | public class Camera2Utils { 11 | 12 | /** 13 | * 比较两个int值的大小. 14 | * 15 | * @param lhs 左侧值 16 | * @param rhs 右侧值 17 | * @return lhs < rhs, -1; lhs = rhs, 0; lhs > rhs, 1; 18 | */ 19 | public static int compare(int lhs, int rhs) { 20 | return lhs < rhs ? -1 : (lhs == rhs ? 0 : 1); 21 | } 22 | 23 | /** 24 | * 判断是否是16:9的Size, 允许误差5%. 25 | * 26 | * @param size Size 27 | * @return 是否是16:9的Size 28 | */ 29 | public static boolean isWide(Size size) { 30 | double ratio = ((double) size.getWidth()) / ((double) size.getHeight()); 31 | return ratio > 1.68 && ratio < 1.87; 32 | } 33 | 34 | /** 35 | * 从sizeArray中找到满足16:9比例,且不超过maxPicturePixels指定的像素数的最大Size. 36 | * 若找不到,则选择满足16:9比例的最大Size(像素数可能超过maxPicturePixels),若仍找不到,返回最大Size。 37 | * 38 | * @param sizeArray StreamConfigurationMap.getOutputSizes(ImageFormat.JPEG)得到的sizeArray 39 | * @param maxPicturePixels 最大可接受的照片像素数 40 | * @return 找到满足16:9比例,且不超过maxPicturePixels指定的像素数的最大Size 41 | */ 42 | public static Size findBestSize(Size[] sizeArray, long maxPicturePixels) { 43 | //满足16:9,但超过maxAcceptedPixels的过大Size 44 | List tooLargeSizes = new ArrayList<>(); 45 | List immutableSizeList = Arrays.asList(sizeArray); 46 | //Arrays.asList返回的List是不可变的,需重新包装为java.util.ArrayList. 47 | List sizeList = new ArrayList<>(immutableSizeList); 48 | //按面积由大到小排序 49 | Collections.sort(sizeList, (lhs, rhs) -> -compare(lhs.getWidth() * lhs.getHeight(), rhs.getWidth() * rhs.getHeight())); 50 | for (Size size : sizeList) { 51 | //非16:9的尺寸无视 52 | if (!isWide(size)) continue; 53 | boolean notTooLarge = ((long) size.getWidth()) * ((long) size.getHeight()) <= maxPicturePixels; 54 | if (!notTooLarge) { 55 | tooLargeSizes.add(size); 56 | continue; 57 | } 58 | return size; 59 | } 60 | if (tooLargeSizes.size() > 0) { 61 | return tooLargeSizes.get(0); 62 | } else { 63 | return sizeList.get(0); 64 | } 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /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/java/com/xdandroid/hellocamera2/util/CommonUtils.java: -------------------------------------------------------------------------------- 1 | package com.xdandroid.hellocamera2.util; 2 | 3 | import android.content.*; 4 | import android.content.res.*; 5 | import android.os.*; 6 | import android.util.*; 7 | import android.view.*; 8 | 9 | import com.xdandroid.hellocamera2.app.*; 10 | 11 | import java.io.*; 12 | 13 | public class CommonUtils { 14 | 15 | /** 16 | * dp转px 17 | * 18 | * @param dpValue dp 19 | * @return px 20 | */ 21 | public static int dp2px(float dpValue) { 22 | final float scale = App.sApp.getResources().getDisplayMetrics().density; 23 | return (int) (dpValue * scale + 0.5f); 24 | } 25 | 26 | public static int px2dp(float pxValue) { 27 | final float scale = App.sApp.getResources().getDisplayMetrics().density; 28 | return (int) (pxValue / scale + 0.5f); 29 | } 30 | 31 | /** 32 | * 判断设备是否具有虚拟导航栏 33 | * @return 设备是否具有虚拟导航栏 34 | */ 35 | public static boolean hasNavigationBar() { 36 | if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) return true; 37 | WindowManager wm = (WindowManager) App.sApp.getSystemService(Context.WINDOW_SERVICE); 38 | Display d = wm.getDefaultDisplay(); 39 | DisplayMetrics realDisplayMetrics = new DisplayMetrics(); 40 | d.getRealMetrics(realDisplayMetrics); 41 | int realHeight = realDisplayMetrics.heightPixels; 42 | int realWidth = realDisplayMetrics.widthPixels; 43 | DisplayMetrics displayMetrics = new DisplayMetrics(); 44 | d.getMetrics(displayMetrics); 45 | int displayHeight = displayMetrics.heightPixels; 46 | int displayWidth = displayMetrics.widthPixels; 47 | return (realWidth - displayWidth) > 0 || (realHeight - displayHeight) > 0; 48 | } 49 | 50 | /** 51 | * 得到以px为单位的虚拟导航栏高度,若设备没有虚拟导航栏,返回0. 52 | * @return 虚拟导航栏高度(px),若设备没有虚拟导航栏,返回0. 53 | */ 54 | public static int getNavigationBarHeightInPx() { 55 | if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) return dp2px(48); 56 | int navBarHeightInPx = 0; 57 | Resources rs = App.sApp.getResources(); 58 | int id = rs.getIdentifier("navigation_bar_height", "dimen", "android"); 59 | if (id > 0 && hasNavigationBar()) navBarHeightInPx = rs.getDimensionPixelSize(id); 60 | return navBarHeightInPx; 61 | } 62 | 63 | /** 64 | * 创建File对象,对应于data/data/${packageName}/cache/fileName. 65 | * 66 | * @param fileName 文件名 67 | * @return File 68 | */ 69 | @SuppressWarnings("ResultOfMethodCallIgnored") 70 | public static File createImageFile(String fileName) { 71 | File dir = new File(App.sApp.getCacheDir(), "images"); 72 | if (!dir.exists()) dir.mkdirs(); 73 | return new File(dir, fileName); 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 16 | 17 | 23 | 24 | 25 | 26 | 33 | 34 | 42 | 43 | 50 | 51 |