├── .gitignore ├── LICENSE ├── README.md ├── build.gradle ├── demo ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── desmond │ │ └── demo │ │ └── ApplicationTest.java │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── desmond │ │ └── demo │ │ └── MainActivity.java │ └── res │ ├── layout │ └── activity_main.xml │ ├── menu │ └── menu_main.xml │ ├── mipmap-hdpi │ └── ic_launcher.png │ ├── mipmap-mdpi │ └── ic_launcher.png │ ├── mipmap-xhdpi │ └── ic_launcher.png │ ├── mipmap-xxhdpi │ └── ic_launcher.png │ ├── values-w820dp │ └── dimens.xml │ └── values │ ├── dimens.xml │ ├── strings.xml │ └── styles.xml ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── squarecamera ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src ├── androidTest └── java │ └── com │ └── desmond │ └── squarecamera │ └── ApplicationTest.java └── main ├── AndroidManifest.xml ├── java └── com │ └── desmond │ └── squarecamera │ ├── CameraActivity.java │ ├── CameraFragment.java │ ├── CameraSettingPreferences.java │ ├── EditSavePhotoFragment.java │ ├── ImageParameters.java │ ├── ImageUtility.java │ ├── ResizeAnimation.java │ ├── RuntimePermissionActivity.java │ ├── SquareCameraPreview.java │ └── SquareImageView.java └── res ├── color ├── squarecamera__red_selectable.xml └── squarecamera__white_selectable.xml ├── drawable-xxhdpi ├── squarecamera__auto.png ├── squarecamera__camera.png ├── squarecamera__camera_snap_selected.png ├── squarecamera__camera_snap_unselected.png ├── squarecamera__cancel_selected.png ├── squarecamera__cancel_unselected.png ├── squarecamera__save_selected.png ├── squarecamera__save_unselected.png └── squarecamera__toggle_flash.png ├── drawable ├── squarecamera__cancel_drawable.xml ├── squarecamera__capture_photo_button.xml └── squarecamera__save_photo_drawable.xml ├── layout-land ├── squarecamera__fragment_camera.xml └── squarecamera__fragment_edit_save_photo.xml ├── layout ├── squarecamera__activity_camera.xml ├── squarecamera__fragment_camera.xml └── squarecamera__fragment_edit_save_photo.xml ├── menu ├── squarecamera__menu_blank.xml └── squarecamera__menu_main.xml ├── values-v19 └── styles.xml ├── values-v21 └── styles.xml ├── values-w820dp └── dimens.xml └── values ├── colors.xml ├── dimens.xml ├── strings.xml └── styles.xml /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | 3 | # generated files 4 | bin/ 5 | gen/ 6 | 7 | # Ignore gradle files 8 | .gradle 9 | build/ 10 | 11 | # Local configuration file (sdk path, etc) 12 | local.properties 13 | 14 | # Proguard folder generated by Eclipse 15 | proguard/ 16 | 17 | # Eclipse Metadata 18 | .metadata/ 19 | 20 | # Mac OS X clutter 21 | *.DS_Store 22 | 23 | # Windows clutter 24 | Thumbs.db 25 | 26 | # Intellij IDEA (see https://intellij-support.jetbrains.com/entries/23393067) 27 | /.idea 28 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Ng Yang Yi,Desmond 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SquareCamera 2 | [![Android Arsenal](https://img.shields.io/badge/Android%20Arsenal-SquareCamera-brightgreen.svg?style=flat)](http://android-arsenal.com/details/1/1745) 3 | ## Description 4 | Android module that takes a square photo using the native Android Camera APIs. The new Camera2 APIs from the L release is not used because support has to go back to SDK version 14 for my own requirement. 5 | 6 | ## Features 7 | - Tap to focus 8 | - Two fingers zooming 9 | - Front & Back camera 10 | - Flash mode (Saved when the user exits) 11 | - Supports both portrait & landscape 12 | - Runtime permission is supported for saving/viewing photos 13 | 14 | ## SDK Support 15 | Support from SDK version 14 onwards 16 | 17 | ## Download 18 | jCenter: 19 | ```groovy 20 | repositories { 21 | jcenter() 22 | } 23 | 24 | dependencies { 25 | compile 'com.github.boxme:squarecamera:1.1.0' 26 | } 27 | ``` 28 | 29 | ## Example 30 | ```java 31 | private static final int REQUEST_CAMERA = 0; 32 | 33 | // Check for camera permission in MashMallow 34 | public void requestForCameraPermission(View view) { 35 | final String permission = Manifest.permission.CAMERA; 36 | if (ContextCompat.checkSelfPermission(MainActivity.this, permission) 37 | != PackageManager.PERMISSION_GRANTED) { 38 | if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, permission)) { 39 | // Show permission rationale 40 | } else { 41 | // Handle the result in Activity#onRequestPermissionResult(int, String[], int[]) 42 | ActivityCompat.requestPermissions(YourActivity.this, new String[]{permission}, REQUEST_CAMERA_PERMISSION); 43 | } 44 | } else { 45 | // Start CameraActivity 46 | } 47 | } 48 | 49 | // Start CameraActivity 50 | Intent startCustomCameraIntent = new Intent(this, CameraActivity.class); 51 | startActivityForResult(startCustomCameraIntent, REQUEST_CAMERA); 52 | 53 | // Receive Uri of saved square photo 54 | @Override 55 | protected void onActivityResult(int requestCode, int resultCode, Intent data) { 56 | if (resultCode != RESULT_OK) return; 57 | 58 | if (requestCode == REQUEST_CAMERA) { 59 | Uri photoUri = data.getData(); 60 | } 61 | super.onActivityResult(requestCode, resultCode, data); 62 | } 63 | ``` 64 | 65 | ## Video Demo 66 | Link: https://youtu.be/cSGFiP-gZYU 67 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | repositories { 5 | jcenter() 6 | } 7 | dependencies { 8 | classpath 'com.android.tools.build:gradle:1.3.0' 9 | classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.2' 10 | classpath 'com.github.dcendents:android-maven-plugin:1.2' 11 | // NOTE: Do not place your application dependencies here; they belong 12 | // in the individual module build.gradle files 13 | } 14 | } 15 | 16 | allprojects { 17 | repositories { 18 | jcenter() 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /demo/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /demo/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 23 5 | buildToolsVersion "23.0.0" 6 | 7 | defaultConfig { 8 | applicationId "com.desmond.demo" 9 | minSdkVersion 14 10 | targetSdkVersion 23 11 | versionCode 2 12 | versionName "1.1" 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 | compile 'com.android.support:appcompat-v7:23.0.0' 25 | // compile 'com.github.boxme:squarecamera:1.0.3' 26 | compile project(':squarecamera') 27 | } 28 | -------------------------------------------------------------------------------- /demo/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/desmond/android-sdks/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 | -------------------------------------------------------------------------------- /demo/src/androidTest/java/com/desmond/demo/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.desmond.demo; 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 | } -------------------------------------------------------------------------------- /demo/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 20 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /demo/src/main/java/com/desmond/demo/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.desmond.demo; 2 | 3 | import android.Manifest; 4 | import android.content.DialogInterface; 5 | import android.content.Intent; 6 | import android.content.pm.PackageManager; 7 | import android.graphics.Bitmap; 8 | import android.graphics.Point; 9 | import android.net.Uri; 10 | import android.os.Bundle; 11 | import android.support.annotation.NonNull; 12 | import android.support.v4.app.ActivityCompat; 13 | import android.support.v4.content.ContextCompat; 14 | import android.support.v7.app.AlertDialog; 15 | import android.support.v7.app.AppCompatActivity; 16 | import android.view.Display; 17 | import android.view.View; 18 | import android.widget.ImageView; 19 | 20 | import com.desmond.squarecamera.CameraActivity; 21 | import com.desmond.squarecamera.ImageUtility; 22 | 23 | 24 | public class MainActivity extends AppCompatActivity { 25 | 26 | private static final int REQUEST_CAMERA = 0; 27 | private static final int REQUEST_CAMERA_PERMISSION = 1; 28 | private Point mSize; 29 | 30 | @Override 31 | protected void onCreate(Bundle savedInstanceState) { 32 | super.onCreate(savedInstanceState); 33 | setContentView(R.layout.activity_main); 34 | 35 | Display display = getWindowManager().getDefaultDisplay(); 36 | mSize = new Point(); 37 | display.getSize(mSize); 38 | } 39 | 40 | @Override 41 | protected void onActivityResult(int requestCode, int resultCode, Intent data) { 42 | if (resultCode != RESULT_OK) return; 43 | 44 | if (requestCode == REQUEST_CAMERA) { 45 | Uri photoUri = data.getData(); 46 | // Get the bitmap in according to the width of the device 47 | Bitmap bitmap = ImageUtility.decodeSampledBitmapFromPath(photoUri.getPath(), mSize.x, mSize.x); 48 | ((ImageView) findViewById(R.id.image)).setImageBitmap(bitmap); 49 | } 50 | super.onActivityResult(requestCode, resultCode, data); 51 | } 52 | 53 | public void requestForCameraPermission(View view) { 54 | final String permission = Manifest.permission.CAMERA; 55 | if (ContextCompat.checkSelfPermission(MainActivity.this, permission) 56 | != PackageManager.PERMISSION_GRANTED) { 57 | if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, permission)) { 58 | showPermissionRationaleDialog("Test", permission); 59 | } else { 60 | requestForPermission(permission); 61 | } 62 | } else { 63 | launch(); 64 | } 65 | } 66 | 67 | private void showPermissionRationaleDialog(final String message, final String permission) { 68 | new AlertDialog.Builder(MainActivity.this) 69 | .setMessage(message) 70 | .setPositiveButton("Ok", new DialogInterface.OnClickListener() { 71 | @Override 72 | public void onClick(DialogInterface dialog, int which) { 73 | MainActivity.this.requestForPermission(permission); 74 | } 75 | }) 76 | .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 77 | @Override 78 | public void onClick(DialogInterface dialog, int which) { 79 | 80 | } 81 | }) 82 | .create() 83 | .show(); 84 | } 85 | 86 | private void requestForPermission(final String permission) { 87 | ActivityCompat.requestPermissions(MainActivity.this, new String[]{permission}, REQUEST_CAMERA_PERMISSION); 88 | } 89 | 90 | private void launch() { 91 | Intent startCustomCameraIntent = new Intent(this, CameraActivity.class); 92 | startActivityForResult(startCustomCameraIntent, REQUEST_CAMERA); 93 | } 94 | 95 | @Override 96 | public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 97 | switch (requestCode) { 98 | case REQUEST_CAMERA_PERMISSION: 99 | final int numOfRequest = grantResults.length; 100 | final boolean isGranted = numOfRequest == 1 101 | && PackageManager.PERMISSION_GRANTED == grantResults[numOfRequest - 1]; 102 | if (isGranted) { 103 | launch(); 104 | } 105 | break; 106 | 107 | default: 108 | super.onRequestPermissionsResult(requestCode, permissions, grantResults); 109 | } 110 | } 111 | } 112 | -------------------------------------------------------------------------------- /demo/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 6 | 7 | 12 | 13 |