Commit 36400a0c authored by Daniel Kornhauser's avatar Daniel Kornhauser

Adding Camera Module

parent 5aff563a
...@@ -6,10 +6,11 @@ ...@@ -6,10 +6,11 @@
<option name="distributionType" value="LOCAL" /> <option name="distributionType" value="LOCAL" />
<option name="externalProjectPath" value="$PROJECT_DIR$" /> <option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="gradleHome" value="$APPLICATION_HOME_DIR$/gradle/gradle-2.4" /> <option name="gradleHome" value="$APPLICATION_HOME_DIR$/gradle/gradle-2.4" />
<option name="gradleJvm" value="1.7" /> <option name="gradleJvm" value="1.8" />
<option name="modules"> <option name="modules">
<set> <set>
<option value="$PROJECT_DIR$" /> <option value="$PROJECT_DIR$" />
<option value="$PROJECT_DIR$/CameraModule" />
<option value="$PROJECT_DIR$/app" /> <option value="$PROJECT_DIR$/app" />
<option value="$PROJECT_DIR$/openCVLibrary2411" /> <option value="$PROJECT_DIR$/openCVLibrary2411" />
</set> </set>
......
...@@ -37,7 +37,7 @@ ...@@ -37,7 +37,7 @@
<ConfirmationsSetting value="0" id="Add" /> <ConfirmationsSetting value="0" id="Add" />
<ConfirmationsSetting value="0" id="Remove" /> <ConfirmationsSetting value="0" id="Remove" />
</component> </component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" assert-keyword="true" jdk-15="true" project-jdk-name="1.7" project-jdk-type="JavaSDK"> <component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" /> <output url="file://$PROJECT_DIR$/build/classes" />
</component> </component>
<component name="ProjectType"> <component name="ProjectType">
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
<project version="4"> <project version="4">
<component name="ProjectModuleManager"> <component name="ProjectModuleManager">
<modules> <modules>
<module fileurl="file://$PROJECT_DIR$/CameraModule/CameraModule.iml" filepath="$PROJECT_DIR$/CameraModule/CameraModule.iml" />
<module fileurl="file://$PROJECT_DIR$/EarRecognition.iml" filepath="$PROJECT_DIR$/EarRecognition.iml" /> <module fileurl="file://$PROJECT_DIR$/EarRecognition.iml" filepath="$PROJECT_DIR$/EarRecognition.iml" />
<module fileurl="file://$PROJECT_DIR$/app/app.iml" filepath="$PROJECT_DIR$/app/app.iml" /> <module fileurl="file://$PROJECT_DIR$/app/app.iml" filepath="$PROJECT_DIR$/app/app.iml" />
<module fileurl="file://$PROJECT_DIR$/openCVLibrary2411/openCVLibrary2411.iml" filepath="$PROJECT_DIR$/openCVLibrary2411/openCVLibrary2411.iml" /> <module fileurl="file://$PROJECT_DIR$/openCVLibrary2411/openCVLibrary2411.iml" filepath="$PROJECT_DIR$/openCVLibrary2411/openCVLibrary2411.iml" />
......
# svn
*.svn*
# built application files
*.apk
*.ap_
# files for the dex VM
*.dex
# Java class files
*.class
# generated GUI files
*/R.java
# generated folder
bin
gen
# local
local.properties
proguard.cfg
# log files
log*.txt
# archives
*.gz
*.tar
*.zip
# eclipse
*.metadata
*.settings
#idea
*.idea
*.iml
out/
#gradle
/build
.gradle/
\ No newline at end of file
apply plugin: 'com.android.library'
repositories {
mavenCentral()
}
android {
compileSdkVersion 19
buildToolsVersion '19.1.0'
defaultConfig {
minSdkVersion 14
targetSdkVersion 19
versionCode 1
versionName "1.0"
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
buildTypes {
release {
//runProguard false
//proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
dependencies {
compile 'com.edmodo:cropper:1.0.1'
compile 'com.squareup.picasso:picasso:2.2.0'
compile 'com.jakewharton.timber:timber:2.2.2'
compile fileTree(dir: 'libs', include: ['*.jar'])
}
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in D:/androidsdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the ProGuard
# include property in project.properties.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# Add any project specific keep options here:
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
\ No newline at end of file
<!--
~ The MIT License (MIT)
~
~ Copyright (c) 2014 Zillow
~
~ Permission is hereby granted, free of charge, to any person obtaining a copy
~ of this software and associated documentation files (the "Software"), to deal
~ in the Software without restriction, including without limitation the rights
~ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
~ copies of the Software, and to permit persons to whom the Software is furnished
~ to do so, subject to the following conditions:
~
~ The above copyright notice and this permission notice shall be included in all
~ copies or substantial portions of the Software.
~
~ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
~ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
~ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
~ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
~ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
~ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.yalantis.cameramodule">
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-feature android:name="android.hardware.camera" android:required="true"/>
<uses-feature android:name="android.hardware.camera.autofocus"/>
<application>
<activity
android:name="com.yalantis.cameramodule.activity.CameraActivity"
android:theme="@style/Theme.TranslucentNavigationBar"
android:configChanges="orientation|screenSize|keyboardHidden"
android:screenOrientation="portrait"/>
<activity
android:name="com.yalantis.cameramodule.activity.PhotoCropActivity"
android:label="@string/lbl_photo_crop"
android:screenOrientation="portrait"/>
<activity
android:name="com.yalantis.cameramodule.activity.PhotoPreviewActivity"
android:label="@string/lbl_photo_preview"
android:screenOrientation="portrait"/>
</application>
</manifest>
/*
* The MIT License (MIT)
*
* Copyright (c) 2014 Zillow
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.squareup.picasso;
public class PicassoTools {
public static void clearCache(Picasso p) {
p.cache.clear();
}
}
/*
* The MIT License (MIT)
*
* Copyright (c) 2014 Zillow
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.yalantis.cameramodule;
public final class CameraConst {
public static final boolean DEBUG = true;
public static final int COMPRESS_QUALITY = 90;
}
/*
* The MIT License (MIT)
*
* Copyright (c) 2014 Zillow
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.yalantis.cameramodule;
import android.content.Context;
import com.yalantis.cameramodule.interfaces.Initializer;
import com.yalantis.cameramodule.manager.ImageManager;
import com.yalantis.cameramodule.manager.LoggerManager;
import com.yalantis.cameramodule.manager.SharedPrefManager;
public enum ManagerInitializer implements Initializer {
i;
@Override
public void init(Context context) {
SharedPrefManager.i.init(context);
LoggerManager.i.init(context);
ImageManager.i.init(context);
}
@Override
public void clear() {
SharedPrefManager.i.clear();
LoggerManager.i.clear();
ImageManager.i.clear();
}
}
/*
* The MIT License (MIT)
*
* Copyright (c) 2014 Zillow
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.yalantis.cameramodule.activity;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
public abstract class BaseActivity extends Activity {
protected Handler handler;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
handler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
return BaseActivity.this.handleMessage(msg);
}
});
}
protected void hideKeyboard() {
InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
View view = this.getCurrentFocus();
if (view != null) {
if (inputManager != null) {
inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
}
protected boolean handleMessage(Message msg) {
return false;
}
protected void showActionBar() {
if (getActionBar() != null) {
getActionBar().show();
}
}
protected void hideActionBar() {
if (getActionBar() != null) {
getActionBar().hide();
}
}
public void showBack() {
getActionBar().setDisplayShowHomeEnabled(false);
getActionBar().setDisplayHomeAsUpEnabled(true);
}
public void hideBack() {
getActionBar().setDisplayShowHomeEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(false);
}
@Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case android.R.id.home:
super.onBackPressed();
return true;
}
return super.onOptionsItemSelected(menuItem);
}
}
/*
* The MIT License (MIT)
*
* Copyright (c) 2014 Zillow
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.yalantis.cameramodule.activity;
import android.app.Fragment;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Target;
import com.yalantis.cameramodule.R;
import com.yalantis.cameramodule.manager.ImageManager;
public abstract class BasePhotoActivity extends BaseActivity {
protected String path;
protected String name;
protected Bitmap bitmap;
protected View progressBar;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
showActionBar();
showBack();
setContentView(R.layout.activity_photo);
if (getIntent().hasExtra(EXTRAS.PATH)) {
path = getIntent().getStringExtra(EXTRAS.PATH);
} else {
throw new RuntimeException("There is no path to image in extras");
}
if (getIntent().hasExtra(EXTRAS.NAME)) {
name = getIntent().getStringExtra(EXTRAS.NAME);
} else {
throw new RuntimeException("There is no image name in extras");
}
progressBar = findViewById(R.id.progress);
}
@Override
protected void onResume() {
super.onResume();
if (bitmap == null || bitmap.isRecycled()) {
loadPhoto();
}
}
protected abstract void showPhoto(Bitmap bitmap);
protected void rotatePhoto(float angle) {
synchronized (bitmap) {
bitmap = ImageManager.i.rotatePhoto(path, angle);
showPhoto(bitmap);
}
setResult(EXTRAS.RESULT_EDITED, setIntentData());
}
protected void deletePhoto() {
setResult(EXTRAS.RESULT_DELETED, setIntentData());
finish();
}
protected void loadPhoto() {
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
ImageManager.i.loadPhoto(path, metrics.widthPixels, metrics.heightPixels, loadingTarget);
}
protected void setFragment(Fragment fragment) {
getFragmentManager()
.beginTransaction()
.replace(R.id.fragment_content, fragment)
.commit();
}
private Target loadingTarget = new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
progressBar.setVisibility(View.GONE);
BasePhotoActivity.this.bitmap = bitmap;
showPhoto(bitmap);
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
progressBar.setVisibility(View.GONE);
bitmap = null;
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
progressBar.setVisibility(View.VISIBLE);
}
};
protected Intent setIntentData() {
return setIntentData(null);
}
protected Intent setIntentData(Intent intent) {
if (intent == null) {
intent = new Intent();
}
intent.putExtra(EXTRAS.PATH, path);
intent.putExtra(EXTRAS.NAME, name);
return intent;
}
public static final class EXTRAS {
public static final String PATH = "path";
public static final String NAME = "name";
public static final String FROM_CAMERA = "from_camera";
public static final int REQUEST_PHOTO_EDIT = 7338;
public static final int RESULT_EDITED = 338;
public static final int RESULT_DELETED = 3583;
}
@Override
public void onBackPressed() {
super.onBackPressed();
}
}
/*
* The MIT License (MIT)
*
* Copyright (c) 2014 Zillow
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.yalantis.cameramodule.activity;
import android.content.Intent;
import android.media.ExifInterface;
import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.widget.Toast;
import com.yalantis.cameramodule.CameraConst;
import com.yalantis.cameramodule.R;
import com.yalantis.cameramodule.fragment.CameraFragment;
import com.yalantis.cameramodule.interfaces.*;
import com.yalantis.cameramodule.manager.SharedPrefManager;
import com.yalantis.cameramodule.util.PhotoUtil;
import com.yalantis.cameramodule.util.SavingPhotoTask;
import timber.log.Timber;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class CameraActivity extends BaseActivity implements PhotoTakenCallback, PhotoSavedListener, RawPhotoTakenCallback,
CameraParamsChangedListener {
public static final String PATH = "path";
public static final String USE_FRONT_CAMERA = "use_front_camera";
public static final String OPEN_PHOTO_PREVIEW = "open_photo_preview";
public static final String LAYOUT_ID = "layout_id";
private static final String IMG_PREFIX = "IMG_";
private static final String IMG_POSTFIX = ".jpg";
private static final String TIME_FORMAT = "yyyyMMdd_HHmmss";
private KeyEventsListener keyEventsListener;
private PhotoSavedListener photoSavedListener;
private String path;
private boolean openPreview;
private boolean saving;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
hideActionBar();
setContentView(R.layout.activity_with_fragment);
if (TextUtils.isEmpty(path = getIntent().getStringExtra(PATH))) {
path = Environment.getExternalStorageDirectory().getPath();
}
openPreview = getIntent().getBooleanExtra(OPEN_PHOTO_PREVIEW, SharedPrefManager.i.isOpenPhotoPreview());
if (openPreview != SharedPrefManager.i.isOpenPhotoPreview()) {
SharedPrefManager.i.setOpenPhotoPreview(openPreview);
}
boolean useFrontCamera = getIntent().getBooleanExtra(USE_FRONT_CAMERA, SharedPrefManager.i.useFrontCamera());
if (useFrontCamera != SharedPrefManager.i.useFrontCamera()) {
SharedPrefManager.i.setUseFrontCamera(useFrontCamera);
}
init();
}
private void init() {
CameraFragment fragment;
int layoutId = getIntent().getIntExtra(LAYOUT_ID, -1);
if (layoutId > 0) {
fragment = CameraFragment.newInstance(layoutId, this, createCameraParams());
} else {
fragment = CameraFragment.newInstance(this, createCameraParams());
}
fragment.setParamsChangedListener(this);
keyEventsListener = fragment;
photoSavedListener = fragment;
getFragmentManager()
.beginTransaction()
.replace(R.id.fragment_content, fragment)
.commit();
}
private Bundle createCameraParams() {
Bundle bundle = new Bundle();
bundle.putInt(CameraFragment.RATIO, SharedPrefManager.i.getCameraRatio());
bundle.putInt(CameraFragment.FLASH_MODE, SharedPrefManager.i.getCameraFlashMode());
bundle.putInt(CameraFragment.HDR_MODE, SharedPrefManager.i.isHDR());
bundle.putInt(CameraFragment.QUALITY, SharedPrefManager.i.getCameraQuality());
bundle.putInt(CameraFragment.FOCUS_MODE, SharedPrefManager.i.getCameraFocusMode());
bundle.putBoolean(CameraFragment.FRONT_CAMERA, SharedPrefManager.i.useFrontCamera());
return bundle;
}
private String createName() {
String timeStamp = new SimpleDateFormat(TIME_FORMAT).format(new Date());
return IMG_PREFIX + timeStamp + IMG_POSTFIX;
}
@Override
public void photoTaken(byte[] data, int orientation) {
savePhoto(data, createName(), path, orientation);
}
@Override
public void rawPhotoTaken(byte[] data) {
Timber.d("rawPhotoTaken: data[%1d]", data.length);
}
private void savePhoto(byte[] data, String name, String path, int orientation) {
saving = true;
new SavingPhotoTask(data, name, path, orientation, this).execute();
}
@Override
public void photoSaved(String path, String name) {
saving = false;
Toast.makeText(this, "Photo " + name + " saved", Toast.LENGTH_SHORT).show();
Timber.d("Photo " + name + " saved");
if (CameraConst.DEBUG) {
printExifOrientation(path);
}
if (openPreview) {
openPreview(path, name);
}
if (photoSavedListener != null) {
photoSavedListener.photoSaved(path, name);
}
}
private void openPreview(String path, String name) {
Intent intent = new Intent(this, PhotoPreviewActivity.class);
intent.putExtra(BasePhotoActivity.EXTRAS.PATH, path);
intent.putExtra(BasePhotoActivity.EXTRAS.NAME, name);
intent.putExtra(BasePhotoActivity.EXTRAS.FROM_CAMERA, true);
startActivityForResult(intent, BasePhotoActivity.EXTRAS.REQUEST_PHOTO_EDIT);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == BasePhotoActivity.EXTRAS.REQUEST_PHOTO_EDIT) {
switch (resultCode) {
case BasePhotoActivity.EXTRAS.RESULT_DELETED:
String path = data.getStringExtra(BasePhotoActivity.EXTRAS.PATH);
PhotoUtil.deletePhoto(path);
break;
}
}
}
private void printExifOrientation(String path) {
try {
ExifInterface exif = new ExifInterface(path);
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
Timber.d("Orientation: " + orientation);
} catch (IOException e) {
Timber.e(e, e.getMessage());
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
keyEventsListener.zoomIn();
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
keyEventsListener.zoomOut();
return true;
case KeyEvent.KEYCODE_BACK:
onBackPressed();
return true;
case KeyEvent.KEYCODE_CAMERA:
keyEventsListener.takePhoto();
return true;
}
return false;
}
@Override
public void onQualityChanged(int id) {
SharedPrefManager.i.setCameraQuality(id);
}
@Override
public void onRatioChanged(int id) {
SharedPrefManager.i.setCameraRatio(id);
}
@Override
public void onFlashModeChanged(int id) {
SharedPrefManager.i.setCameraFlashMode(id);
}
@Override
public void onHDRChanged(int id) {
SharedPrefManager.i.setHDRMode(id);
}
@Override
public void onFocusModeChanged(int id) {
SharedPrefManager.i.setCameraFocusMode(id);
}
@Override
public void onBackPressed() {
if (!saving) {
super.onBackPressed();
}
}
}
/*
* The MIT License (MIT)
*
* Copyright (c) 2014 Zillow
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.yalantis.cameramodule.activity;
import android.graphics.Bitmap;
import android.graphics.RectF;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import com.yalantis.cameramodule.R;
import com.yalantis.cameramodule.fragment.PhotoCropFragment;
import com.yalantis.cameramodule.interfaces.PhotoCroppedCallback;
import com.yalantis.cameramodule.interfaces.PhotoSavedListener;
import com.yalantis.cameramodule.manager.ImageManager;
public class PhotoCropActivity extends BasePhotoActivity implements PhotoCroppedCallback {
private PhotoCropFragment cropFragment;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.photo_crop_options, menu);
return super.onCreateOptionsMenu(menu);
}
public void apply(MenuItem ite) {
cropFragment.applyCrop();
}
public void cancel(MenuItem ite) {
finish();
}
@Override
protected void showPhoto(Bitmap bitmap) {
if (cropFragment == null) {
cropFragment = PhotoCropFragment.newInstance(bitmap);
setFragment(cropFragment);
} else {
cropFragment.setBitmap(bitmap);
}
}
@Override
public void onPhotoCropped(int width, int height, Bitmap croppedBitmap, RectF cropRect) {
ImageManager.i.cropBitmap(path, width, height, croppedBitmap, cropRect, new PhotoSavedListener() {
@Override
public void photoSaved(String path, String name) {
setResult(EXTRAS.RESULT_EDITED, setIntentData());
finish();
}
});
}
}
/*
* The MIT License (MIT)
*
* Copyright (c) 2014 Zillow
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.yalantis.cameramodule.activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import com.yalantis.cameramodule.R;
import com.yalantis.cameramodule.fragment.PhotoPreviewFragment;
public class PhotoPreviewActivity extends BasePhotoActivity {
private PhotoPreviewFragment previewFragment;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getIntent().getBooleanExtra(EXTRAS.FROM_CAMERA, false)) {
setTitle(R.string.lbl_take_another);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.photo_preview_options, menu);
return super.onCreateOptionsMenu(menu);
}
public void deletePhoto(MenuItem item) {
deletePhoto();
}
public void rotateLeft(MenuItem item) {
rotatePhoto(-90);
}
public void rotateRight(MenuItem item) {
rotatePhoto(90);
}
public void openPhotoCropper(MenuItem item) {
Intent intent = new Intent(this, PhotoCropActivity.class);
startActivityForResult(setIntentData(intent), EXTRAS.REQUEST_PHOTO_EDIT);
}
@Override
protected void showPhoto(Bitmap bitmap) {
if (previewFragment == null) {
previewFragment = PhotoPreviewFragment.newInstance(bitmap);
setFragment(previewFragment);
} else {
previewFragment.setBitmap(bitmap);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == EXTRAS.REQUEST_PHOTO_EDIT) {
if (resultCode == EXTRAS.RESULT_EDITED) {
setResult(EXTRAS.RESULT_EDITED, setIntentData());
loadPhoto();
}
}
}
}
/*
* The MIT License (MIT)
*
* Copyright (c) 2014 Zillow
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.yalantis.cameramodule.adapters;
import java.util.List;
import android.content.Context;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.BaseAdapter;
public abstract class BaseListAdapter<T> extends BaseAdapter {
private Context mContext;
private List<T> mList;
private int mLayoutId;
public BaseListAdapter(Context context, List<T> list, int layout) {
mContext = context;
setList(list);
mLayoutId = layout;
}
@Override
public int getCount() {
if (mList != null) {
return mList.size();
}
return 0;
}
public void addItems(List<T> items) {
mList.addAll(items);
notifyDataSetChanged();
}
public void addItem(T type) {
mList.add(type);
notifyDataSetChanged();
}
@Override
public T getItem(int position) {
return position >= 0 && position < mList.size() ? mList.get(position) : null;
}
@Override
public long getItemId(int position) {
return position;
}
public Context getContext() {
return mContext;
}
public Resources getResources() {
return getContext().getResources();
}
public List<T> getList() {
return mList;
}
public void setList(List<T> list) {
this.mList = list;
}
public void removeItem(int position) {
mList.remove(position);
notifyDataSetChanged();
}
public View getLayout() {
return getInflater().inflate(getLayoutId(), null);
}
public int getLayoutId() {
return mLayoutId;
}
public LayoutInflater getInflater() {
return LayoutInflater.from(mContext);
}
public void clear() {
mList.clear();
notifyDataSetChanged();
}
}
/*
* The MIT License (MIT)
*
* Copyright (c) 2014 Zillow
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.yalantis.cameramodule.adapters;
import java.util.List;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.yalantis.cameramodule.R;
public class ObjectToStringAdapter<T> extends BaseListAdapter<T> {
public ObjectToStringAdapter(Context context, List<T> list) {
super(context, list, R.layout.object_to_string_list_item);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return createView(position, convertView, false);
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
return createView(position, convertView, true);
}
private View createView(int position, View convertView, boolean dropDown) {
ViewHolder holder;
if (convertView == null) {
if (dropDown) {
convertView = getDropDownLayout();
} else {
convertView = getLayout();
}
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.title);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
T item = getItem(position);
if (item != null) {
holder.text.setText(item.toString());
}
return convertView;
}
public View getDropDownLayout() {
return getInflater().inflate(R.layout.object_to_string_dropdown_list_item, null);
}
class ViewHolder {
TextView text;
}
}
/*
* The MIT License (MIT)
*
* Copyright (c) 2014 Zillow
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.yalantis.cameramodule.control;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ImageView;
public class PreviewImageView extends ImageView {
public PreviewImageView(Context context) {
super(context);
}
public PreviewImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(getMeasuredWidth(), getMeasuredHeight());
}
}
/*
* The MIT License (MIT)
*
* Copyright (c) 2014 Zillow
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.yalantis.cameramodule.fragment;
import android.app.Activity;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.FragmentManager;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Window;
public abstract class BaseDialogFragment extends DialogFragment {
protected Activity activity;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
return dialog;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
this.activity = activity;
}
public abstract String getFragmentTag();
public void show(FragmentManager manager) {
super.show(manager, getFragmentTag());
}
}
/*
* The MIT License (MIT)
*
* Copyright (c) 2014 Zillow
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.yalantis.cameramodule.fragment;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.os.Handler;
public class BaseFragment extends Fragment {
private Handler handler;
protected Activity activity;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
handler = new Handler();
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
this.activity = activity;
}
}
/*
* The MIT License (MIT)
*
* Copyright (c) 2014 Zillow
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.yalantis.cameramodule.fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.CompoundButton;
import android.widget.Spinner;
import android.widget.Switch;
import com.yalantis.cameramodule.R;
import com.yalantis.cameramodule.adapters.ObjectToStringAdapter;
import com.yalantis.cameramodule.interfaces.CameraParamsChangedListener;
import com.yalantis.cameramodule.model.FocusMode;
import com.yalantis.cameramodule.model.HDRMode;
import com.yalantis.cameramodule.model.Quality;
import com.yalantis.cameramodule.model.Ratio;
import java.util.Arrays;
import java.util.List;
public class CameraSettingsDialogFragment extends BaseDialogFragment {
public static final String TAG = CameraSettingsDialogFragment.class.getSimpleName();
private CameraParamsChangedListener paramsChangedListener;
private Quality quality;
private Ratio ratio;
private FocusMode focusMode;
private HDRMode hdrMode;
private List<Ratio> ratios = Arrays.asList(Ratio.values());
private List<Quality> qualities = Arrays.asList(Quality.values());
private List<FocusMode> focusModes = Arrays.asList(FocusMode.values());
public static CameraSettingsDialogFragment newInstance(Bundle bundle, CameraParamsChangedListener listener) {
CameraSettingsDialogFragment fragment = new CameraSettingsDialogFragment();
fragment.setArguments(bundle);
fragment.paramsChangedListener = listener;
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
expandParams(getArguments());
}
private void expandParams(Bundle params) {
if (params == null) {
params = new Bundle();
}
int id = 0;
if (params.containsKey(CameraFragment.QUALITY)) {
id = params.getInt(CameraFragment.QUALITY, 0);
}
quality = Quality.getQualityById(id);
id = 0;
if (params.containsKey(CameraFragment.RATIO)) {
id = params.getInt(CameraFragment.RATIO, 0);
}
ratio = Ratio.getRatioById(id);
id = 0;
if (params.containsKey(CameraFragment.FOCUS_MODE)) {
id = params.getInt(CameraFragment.FOCUS_MODE);
}
focusMode = FocusMode.getFocusModeById(id);
id = 0;
if (params.containsKey(CameraFragment.HDR_MODE)) {
id = params.getInt(CameraFragment.HDR_MODE);
}
hdrMode = HDRMode.getHDRModeById(id);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dialog_camera_params, container, false);
Spinner ratioSwitcher = (Spinner) view.findViewById(R.id.ratios);
ratioSwitcher.setAdapter(new ObjectToStringAdapter<>(activity, ratios));
ratioSwitcher.setSelection(ratios.indexOf(ratio));
ratioSwitcher.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (ratio == ratios.get(position)) {
return;
}
ratio = ratios.get(position);
onRatioChanged(ratio.getId());
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Spinner qualitySwitcher = (Spinner) view.findViewById(R.id.qualities);
qualitySwitcher.setAdapter(new ObjectToStringAdapter<>(activity, qualities));
qualitySwitcher.setSelection(qualities.indexOf(quality));
qualitySwitcher.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (quality == qualities.get(position)) {
return;
}
quality = qualities.get(position);
onQualityChanged(quality.getId());
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Spinner focusSwitcher = (Spinner) view.findViewById(R.id.focus_modes);
focusSwitcher.setAdapter(new ObjectToStringAdapter<>(activity, focusModes));
focusSwitcher.setSelection(focusModes.indexOf(focusMode));
focusSwitcher.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (focusMode == focusModes.get(position)) {
return;
}
focusMode = focusModes.get(position);
onFocusModeChanged(focusMode.getId());
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
if (hdrMode == HDRMode.NONE) {
view.findViewById(R.id.relativeHdr).setVisibility(View.GONE);
} else {
view.findViewById(R.id.relativeHdr).setVisibility(View.VISIBLE);
Switch hdrSwitch = (Switch) view.findViewById(R.id.switchHDR);
hdrSwitch.setChecked(hdrMode == HDRMode.ON);
hdrSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
onHDRChanged(isChecked ? HDRMode.ON.getId() : HDRMode.OFF.getId());
}
});
}
view.findViewById(R.id.close).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
return view;
}
public void onQualityChanged(int id) {
if (paramsChangedListener != null) {
paramsChangedListener.onQualityChanged(id);
}
}
public void onHDRChanged(int id) {
if (paramsChangedListener != null) {
paramsChangedListener.onHDRChanged(id);
}
}
public void onRatioChanged(int id) {
if (paramsChangedListener != null) {
paramsChangedListener.onRatioChanged(id);
}
}
public void onFocusModeChanged(int id) {
if (paramsChangedListener != null) {
paramsChangedListener.onFocusModeChanged(id);
}
}
@Override
public String getFragmentTag() {
return TAG;
}
}
/*
* The MIT License (MIT)
*
* Copyright (c) 2014 Zillow
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.yalantis.cameramodule.fragment;
import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.edmodo.cropper.CropImageView;
import com.yalantis.cameramodule.R;
import com.yalantis.cameramodule.interfaces.PhotoCroppedCallback;
public class PhotoCropFragment extends BaseFragment {
private Bitmap bitmap;
private CropImageView cropView;
private PhotoCroppedCallback callback;
public static PhotoCropFragment newInstance(Bitmap bitmap) {
PhotoCropFragment fragment = new PhotoCropFragment();
fragment.bitmap = bitmap;
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_photo_crop, container, false);
}
@Override
public void onViewCreated(final View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
cropView = (CropImageView) view.findViewById(R.id.photo);
cropView.setImageBitmap(bitmap);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
if (activity instanceof PhotoCroppedCallback) {
callback = (PhotoCroppedCallback) activity;
} else {
throw new RuntimeException(activity.getClass().getName() + " must implement " + PhotoCroppedCallback.class.getName());
}
}
public void setBitmap(Bitmap bitmap) {
this.bitmap = bitmap;
cropView.setImageBitmap(bitmap);
}
public void applyCrop() {
callback.onPhotoCropped(bitmap.getWidth(), bitmap.getHeight(), cropView.getCroppedImage(), cropView.getActualCropRect());
}
}
/*
* The MIT License (MIT)
*
* Copyright (c) 2014 Zillow
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.yalantis.cameramodule.fragment;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.yalantis.cameramodule.R;
import com.yalantis.cameramodule.control.PinchImageView;
public class PhotoPreviewFragment extends BaseFragment {
private Bitmap bitmap;
private PinchImageView imageView;
public static PhotoPreviewFragment newInstance(Bitmap bitmap) {
PhotoPreviewFragment fragment = new PhotoPreviewFragment();
fragment.bitmap = bitmap;
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_photo_preview, container, false);
}
@Override
public void onViewCreated(final View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
imageView = (PinchImageView) view.findViewById(R.id.photo);
if (bitmap != null && !bitmap.isRecycled()) {
imageView.setImageBitmap(bitmap);
} else {
imageView.setImageResource(R.drawable.no_image);
}
}
public void setBitmap(Bitmap bitmap) {
this.bitmap = bitmap;
imageView.setImageBitmap(bitmap);
}
}
/*
* The MIT License (MIT)
*
* Copyright (c) 2014 Zillow
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.yalantis.cameramodule.interfaces;
public interface CameraParamsChangedListener {
public void onQualityChanged(int id);
public void onRatioChanged(int id);
public void onFlashModeChanged(int id);
public void onHDRChanged(int id);
public void onFocusModeChanged(int id);
}
/*
* The MIT License (MIT)
*
* Copyright (c) 2014 Zillow
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.yalantis.cameramodule.interfaces;
import android.hardware.Camera;
public interface FocusCallback {
public void onFocused(Camera camera);
}
/*
* The MIT License (MIT)
*
* Copyright (c) 2014 Zillow
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.yalantis.cameramodule.interfaces;
import android.content.Context;
public interface Initializer {
public void init(Context context);
public void clear();
}
/*
* The MIT License (MIT)
*
* Copyright (c) 2014 Zillow
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.yalantis.cameramodule.interfaces;
public interface KeyEventsListener {
public void zoomIn();
public void zoomOut();
public void takePhoto();
}
/*
* The MIT License (MIT)
*
* Copyright (c) 2014 Zillow
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.yalantis.cameramodule.interfaces;
public interface PhotoActionsCallback {
public void onOpenPhotoPreview(String path, String name);
public void onAddNote(int zpid, String name);
public void onRetake(String name);
public void onDeletePhoto(String name);
public void onDeleteHomePhotos(int zpid);
public void onDeleteAddressPhotos(String address);
}
/*
* The MIT License (MIT)
*
* Copyright (c) 2014 Zillow
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.yalantis.cameramodule.interfaces;
import android.graphics.Bitmap;
import android.graphics.RectF;
public interface PhotoCroppedCallback {
/**
* @param width
* width before crop
* @param height
* height before crop
* @param croppedBitmap
* cropped bitmap
* @param cropRect
* cropping rectangle
*/
public void onPhotoCropped(int width, int height, Bitmap croppedBitmap, RectF cropRect);
}
/*
* The MIT License (MIT)
*
* Copyright (c) 2014 Zillow
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.yalantis.cameramodule.interfaces;
public interface PhotoSavedListener {
public void photoSaved(String path, String name);
}
/*
* The MIT License (MIT)
*
* Copyright (c) 2014 Zillow
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.yalantis.cameramodule.interfaces;
public interface PhotoTakenCallback {
public void photoTaken(byte[] data, int orientation);
}
/*
* The MIT License (MIT)
*
* Copyright (c) 2014 Zillow
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.yalantis.cameramodule.interfaces;
public interface RawPhotoTakenCallback {
public void rawPhotoTaken(byte[] data);
}
/*
* The MIT License (MIT)
*
* Copyright (c) 2014 Zillow
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.yalantis.cameramodule.interfaces;
import android.graphics.Bitmap;
import com.yalantis.cameramodule.util.ManagedTarget;
public interface StorageCallback {
public void setBitmap(String path, Bitmap bitmap);
public void addTarget(ManagedTarget target);
public void removeTarget(ManagedTarget target);
}
/*
* The MIT License (MIT)
*
* Copyright (c) 2014 Zillow
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.yalantis.cameramodule.manager;
import java.io.File;
import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.RectF;
import android.text.TextUtils;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.PicassoTools;
import com.squareup.picasso.Target;
import com.yalantis.cameramodule.interfaces.Initializer;
import com.yalantis.cameramodule.interfaces.PhotoSavedListener;
import com.yalantis.cameramodule.interfaces.StorageCallback;
import com.yalantis.cameramodule.util.CropPhotoTask;
import com.yalantis.cameramodule.util.ManagedTarget;
import com.yalantis.cameramodule.util.RotatePhotoTask;
import com.yalantis.cameramodule.util.ScaleTransformation;
public enum ImageManager implements Initializer, StorageCallback {
i;
private Context context;
private Picasso picasso;
private HashSet<ManagedTarget> targets;
private Map<String, WeakReference<Bitmap>> bitmapMap;
@Override
public void init(Context context) {
this.context = context;
this.picasso = Picasso.with(context);
bitmapMap = new HashMap<>();
targets = new HashSet<>();
}
public void loadPhoto(String path, int width, int height, Target target) {
File photo = !TextUtils.isEmpty(path) ? new File(path) : null;
if (path == null) {
target.onBitmapFailed(null);
}
Bitmap bitmap = getBitmap(path);
if (bitmap != null && !bitmap.isRecycled()) {
target.onBitmapLoaded(bitmap, Picasso.LoadedFrom.MEMORY);
} else {
ManagedTarget managedTarget = new ManagedTarget(target, path, this);
Picasso.with(context)
.load(photo)
.skipMemoryCache()
.config(Bitmap.Config.ARGB_8888)
.transform(new ScaleTransformation(width, height))
.into(managedTarget);
}
}
public void cropBitmap(String path, int width, int height, Bitmap croppedBitmap, RectF rect, PhotoSavedListener callback) {
setBitmap(path, croppedBitmap);
new CropPhotoTask(path, width, height, rect, callback).execute();
}
public Bitmap rotatePhoto(String path, float angle) {
Bitmap bitmap = getBitmap(path);
if (bitmap != null && !bitmap.isRecycled()) {
Matrix matrix = new Matrix();
matrix.postRotate(angle);
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
}
setBitmap(path, bitmap);
new RotatePhotoTask(path, angle, null).execute();
return bitmap;
}
private Bitmap getBitmap(String path) {
return bitmapMap.get(path) != null ? bitmapMap.get(path).get() : null;
}
@Override
public void clear() {
synchronized (bitmapMap) {
for (WeakReference<Bitmap> reference : bitmapMap.values()) {
if (reference != null) {
Bitmap bitmap = reference.get();
if (bitmap != null && !bitmap.isRecycled()) {
bitmap.recycle();
}
}
}
bitmapMap.clear();
}
PicassoTools.clearCache(picasso);
}
@Override
public void setBitmap(String path, Bitmap bitmap) {
bitmapMap.put(path, new WeakReference<>(bitmap));
}
@Override
public void addTarget(ManagedTarget target) {
removeTarget(target);
targets.add(target);
}
@Override
public void removeTarget(ManagedTarget target) {
if (targets.contains(target)) {
targets.remove(target);
}
}
}
/*
* The MIT License (MIT)
*
* Copyright (c) 2014 Zillow
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.yalantis.cameramodule.manager;
import timber.log.Timber;
import android.content.Context;
import com.yalantis.cameramodule.CameraConst;
import com.yalantis.cameramodule.interfaces.Initializer;
public enum LoggerManager implements Initializer {
i;
private Context context;
@Override
public void init(Context context) {
this.context = context;
if (CameraConst.DEBUG) {
Timber.plant(new Timber.DebugTree());
} else {
Timber.plant(new CrashReportingTree());
}
}
/** A tree which logs important information for crash reporting. */
private static class CrashReportingTree extends Timber.DebugTree {
@Override
public void d(String message, Object... args) {
}
@Override
public void d(Throwable t, String message, Object... args) {
}
@Override
public void i(String message, Object... args) {
}
@Override
public void i(Throwable t, String message, Object... args) {
}
@Override
public void w(String message, Object... args) {
}
@Override
public void w(Throwable t, String message, Object... args) {
}
@Override
public void e(String message, Object... args) {
super.e(message, args);
}
@Override
public void e(Throwable t, String message, Object... args) {
super.e(t, message, args);
}
}
@Override
public void clear() {
}
}
/*
* The MIT License (MIT)
*
* Copyright (c) 2014 Zillow
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.yalantis.cameramodule.manager;
import java.util.HashSet;
import java.util.Set;
import android.content.Context;
import android.content.SharedPreferences;
import com.yalantis.cameramodule.interfaces.Initializer;
import com.yalantis.cameramodule.model.CachedValue;
public enum SharedPrefManager implements Initializer {
i;
private static final String NAME = "sharedPrefs";
public static final String OPEN_PHOTO_PREVIEW = "open_photo_preview";
public static final String CAMERA_RATIO = "camera_ratio";
public static final String CAMERA_QUALITY = "camera_quality";
public static final String CAMERA_FLASH_MODE = "camera_flash_mode";
public static final String CAMERA_HDR_MODE = "camera_hdr_mode";
public static final String CAMERA_FOCUS_MODE = "camera_focus_mode";
public static final String USE_FRONT_CAMERA = "use_front_camera";
private static SharedPreferences sp;
private Set<CachedValue> cachedValues;
private CachedValue<Boolean> openPhotoPreview;
private CachedValue<Integer> cameraRatio;
private CachedValue<Integer> cameraQuality;
private CachedValue<Integer> cameraFlashMode;
private CachedValue<Integer> isCameraHDRMode;
private CachedValue<Integer> cameraFocusMode;
private CachedValue<Boolean> useFrontCamera;
@Override
public void init(Context context) {
sp = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
CachedValue.initialize(sp);
cachedValues = new HashSet<>();
cachedValues.add(openPhotoPreview = new CachedValue<>(OPEN_PHOTO_PREVIEW, true, Boolean.class));
cachedValues.add(isCameraHDRMode = new CachedValue<>(CAMERA_HDR_MODE, 0, Integer.class));
cachedValues.add(cameraRatio = new CachedValue<>(CAMERA_RATIO, 0, Integer.class));
cachedValues.add(cameraQuality = new CachedValue<>(CAMERA_QUALITY, 0, Integer.class));
cachedValues.add(cameraFlashMode = new CachedValue<>(CAMERA_FLASH_MODE, 0, Integer.class));
cachedValues.add(cameraFocusMode = new CachedValue<>(CAMERA_FOCUS_MODE, 0, Integer.class));
cachedValues.add(useFrontCamera = new CachedValue<>(USE_FRONT_CAMERA, false, Boolean.class));
}
public void setHDRMode(int isHDR) {
this.isCameraHDRMode.setValue(isHDR);
}
public int isHDR() {
return isCameraHDRMode.getValue();
}
public boolean isOpenPhotoPreview() {
return openPhotoPreview.getValue();
}
public void setOpenPhotoPreview(boolean enabled) {
this.openPhotoPreview.setValue(enabled);
}
public int getCameraRatio() {
return cameraRatio.getValue();
}
public void setCameraRatio(int cameraRatio) {
this.cameraRatio.setValue(cameraRatio);
}
public int getCameraQuality() {
return cameraQuality.getValue();
}
public void setCameraQuality(int cameraQuality) {
this.cameraQuality.setValue(cameraQuality);
}
public int getCameraFlashMode() {
return cameraFlashMode.getValue();
}
public void setCameraFlashMode(int cameraFlashMode) {
this.cameraFlashMode.setValue(cameraFlashMode);
}
public int getCameraFocusMode() {
return cameraFocusMode.getValue();
}
public void setCameraFocusMode(int cameraFocusMode) {
this.cameraFocusMode.setValue(cameraFocusMode);
}
public boolean useFrontCamera() {
return useFrontCamera.getValue();
}
public void setUseFrontCamera(boolean frontCamera) {
this.useFrontCamera.setValue(frontCamera);
}
@Override
public void clear() {
for (CachedValue value : cachedValues) {
value.clear();
}
sp.edit().clear().commit();
}
}
/*
* The MIT License (MIT)
*
* Copyright (c) 2014 Zillow
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.yalantis.cameramodule.model;
import android.content.SharedPreferences;
public class CachedValue<T> {
private static SharedPreferences sharedPref;
private SharedPreferences sp;
private T value;
private T defValue;
private Class type;
private String name;
private boolean loaded = false;
public CachedValue(String name, Class type) {
this(name, null, null, type);
}
public CachedValue(String name, T defValue, Class type) {
this(name, null, defValue, type);
}
public CachedValue(String name, T value, T defValue, Class type) {
this.sp = sharedPref;
this.name = name;
this.type = type;
this.loaded = value != null;
this.value = value;
this.defValue = defValue;
}
public void setValue(T value) {
loaded = true;
write(this.value = value);
}
public T getValue() {
if (!loaded) {
this.value = load();
loaded = true;
}
return this.value;
}
public String getName() {
return name;
}
private void write(T value) {
SharedPreferences.Editor editor = sp.edit();
if (value instanceof String) {
editor.putString(name, (String) value);
} else if (value instanceof Integer) {
editor.putInt(name, (Integer) value);
} else if (value instanceof Float) {
editor.putFloat(name, (Float) value);
} else if (value instanceof Long) {
editor.putLong(name, (Long) value);
} else if (value instanceof Boolean) {
editor.putBoolean(name, (Boolean) value);
}
editor.commit();
}
@SuppressWarnings("unchecked")
private T load() {
if (type == String.class) {
return (T) sp.getString(name, (String) defValue);
} else if (type == Integer.class) {
return (T) Integer.valueOf(sp.getInt(name, (Integer) defValue));
} else if (type == Float.class) {
return (T) Float.valueOf(sp.getFloat(name, (Float) defValue));
} else if (type == Long.class) {
return (T) Long.valueOf(sp.getLong(name, (Long) defValue));
} else if (type == Boolean.class) {
return (T) Boolean.valueOf(sp.getBoolean(name, (Boolean) defValue));
}
return null;
}
public void delete() {
sp.edit().remove(name).commit();
clear();
}
public static void initialize(SharedPreferences sp) {
CachedValue.sharedPref = sp;
}
public void setSharedPreferences(SharedPreferences sp) {
this.sp = sp;
}
public void clear() {
loaded = false;
this.value = null;
}
}
/*
* The MIT License (MIT)
*
* Copyright (c) 2014 Zillow
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.yalantis.cameramodule.model;
public enum FlashMode {
ON(0, "On"), AUTO(1, "Auto"), OFF(2, "Off");
private int id;
private String name;
FlashMode(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public static FlashMode getFlashModeById(int id) {
for (FlashMode mode : values()) {
if (mode.id == id) {
return mode;
}
}
return null;
}
@Override
public String toString() {
return name;
}
}
/*
* The MIT License (MIT)
*
* Copyright (c) 2014 Zillow
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.yalantis.cameramodule.model;
public enum FocusMode {
AUTO(0, "Auto"), TOUCH(1, "Touch");
private int id;
private String name;
FocusMode(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public static FocusMode getFocusModeById(int id) {
for (FocusMode mode : values()) {
if (mode.id == id) {
return mode;
}
}
return null;
}
@Override
public String toString() {
return name;
}
}
/*
* The MIT License (MIT)
*
* Copyright (c) 2014 Zillow
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.yalantis.cameramodule.model;
public enum HDRMode {
NONE(0, "None"), ON(1, "On"), OFF(2, "Off");
private int id;
private String name;
HDRMode(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public static HDRMode getHDRModeById(int id) {
for (HDRMode mode : values()) {
if (mode.id == id) {
return mode;
}
}
return null;
}
@Override
public String toString() {
return name;
}
}
/*
* The MIT License (MIT)
*
* Copyright (c) 2014 Zillow
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.yalantis.cameramodule.model;
public class PictureSize {
public int width;
public int height;
public Ratio ratio;
public PictureSize(int width, int height, Ratio ratio) {
this.width = width;
this.height = height;
this.ratio = ratio;
}
}
/*
* The MIT License (MIT)
*
* Copyright (c) 2014 Zillow
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.yalantis.cameramodule.model;
public enum Quality {
HIGH(0, "High"), MEDIUM(1, "Medium"), LOW(2, "Low");
private int id;
private String name;
Quality(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public static Quality getQualityById(int id) {
for (Quality mode : values()) {
if (mode.id == id) {
return mode;
}
}
return null;
}
@Override
public String toString() {
return name;
}
}
/*
* The MIT License (MIT)
*
* Copyright (c) 2014 Zillow
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.yalantis.cameramodule.model;
public enum Ratio {
R_4x3(0, 4, 3), R_16x9(1, 16, 9);
private int id;
public int w;
public int h;
Ratio(int id, int w, int h) {
this.id = id;
this.w = w;
this.h = h;
}
public int getId() {
return id;
}
public static Ratio getRatioById(int id) {
for (Ratio ratio : values()) {
if (ratio.id == id) {
return ratio;
}
}
return null;
}
public static Ratio pickRatio(int width, int height) {
for (Ratio ratio : values()) {
if (width / ratio.w == height / ratio.h) {
return ratio;
}
}
return null;
}
@Override
public String toString() {
return w + ":" + h;
}
}
/*
* The MIT License (MIT)
*
* Copyright (c) 2014 Zillow
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.yalantis.cameramodule.util;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import timber.log.Timber;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.RectF;
import android.os.AsyncTask;
import com.yalantis.cameramodule.CameraConst;
import com.yalantis.cameramodule.interfaces.PhotoSavedListener;
public class CropPhotoTask extends AsyncTask<Void, Void, Void> {
private String path;
private int width;
private int height;
private RectF rect;
private PhotoSavedListener callback;
public CropPhotoTask(String path, int width, int height, RectF rect, PhotoSavedListener callback) {
this.path = path;
this.rect = rect;
this.width = width;
this.height = height;
this.callback = callback;
}
@Override
protected Void doInBackground(Void... params) {
Bitmap src = BitmapFactory.decodeFile(path);
float koefW = (float) width / (float) src.getWidth();
float koefH = (float) height / (float) src.getHeight();
rect.top /= koefH;
rect.left /= koefW;
rect.right /= koefW;
rect.bottom /= koefH;
width = (int) rect.width();
height = (int) rect.height();
Bitmap bitmap = Bitmap.createBitmap(src, (int) rect.left, (int) rect.top, width, height);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(new File(path));
bitmap.compress(Bitmap.CompressFormat.JPEG, CameraConst.COMPRESS_QUALITY, fos);
} catch (FileNotFoundException e) {
Timber.e(e, "File not found: " + e.getMessage());
} finally {
try {
if (fos != null) {
fos.close();
}
} catch (IOException e) {
Timber.e(e, e.getMessage());
}
}
bitmap.recycle();
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
if (callback != null) {
callback.photoSaved(path, null);
}
}
}
/*
* The MIT License (MIT)
*
* Copyright (c) 2014 Zillow
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.yalantis.cameramodule.util;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Target;
import com.yalantis.cameramodule.interfaces.StorageCallback;
public class ManagedTarget implements Target {
private Target target;
private String path;
private StorageCallback callback;
public ManagedTarget(Target target, String path, StorageCallback callback) {
this.target = target;
this.path = path;
this.callback = callback;
callback.addTarget(this);
}
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
target.onBitmapLoaded(bitmap, from);
callback.setBitmap(path, bitmap);
callback.removeTarget(this);
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
target.onBitmapFailed(errorDrawable);
callback.removeTarget(this);
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
target.onPrepareLoad(placeHolderDrawable);
}
@Override
public int hashCode() {
return path.hashCode();
}
@Override
public boolean equals(Object o) {
if (super.equals(o)) {
return true;
}
if (o == null) {
return false;
}
if (o.getClass() != this.getClass()) {
return false;
}
ManagedTarget other = (ManagedTarget) o;
return other.path.equals(path);
}
}
/*
* The MIT License (MIT)
*
* Copyright (c) 2014 Zillow
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.yalantis.cameramodule.util;
import java.io.File;
public class PhotoUtil {
public static void deletePhoto(String path) {
File file = new File(path);
file.delete();
}
}
/*
* The MIT License (MIT)
*
* Copyright (c) 2014 Zillow
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.yalantis.cameramodule.util;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import timber.log.Timber;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.AsyncTask;
import com.yalantis.cameramodule.CameraConst;
import com.yalantis.cameramodule.interfaces.PhotoSavedListener;
public class RotatePhotoTask extends AsyncTask<Void, Void, Void> {
private String path;
private float angle;
private PhotoSavedListener callback;
public RotatePhotoTask(String path, float angle, PhotoSavedListener callback) {
this.path = path;
this.angle = angle;
this.callback = callback;
}
@Override
protected Void doInBackground(Void... params) {
Bitmap bitmap = BitmapFactory.decodeFile(path); // todo NPE
Matrix matrix = new Matrix();
matrix.postRotate(angle);
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(new File(path));
bitmap.compress(Bitmap.CompressFormat.JPEG, CameraConst.COMPRESS_QUALITY, fos);
} catch (FileNotFoundException e) {
Timber.e(e, "File not found: " + e.getMessage());
} finally {
try {
if (fos != null) {
fos.close();
}
} catch (IOException e) {
Timber.e(e, e.getMessage());
}
}
bitmap.recycle();
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
if (callback != null) {
callback.photoSaved(path, null);
}
}
}
/*
* The MIT License (MIT)
*
* Copyright (c) 2014 Zillow
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.yalantis.cameramodule.util;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import timber.log.Timber;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import com.yalantis.cameramodule.CameraConst;
import com.yalantis.cameramodule.interfaces.PhotoSavedListener;
public class SavingBitmapTask extends AsyncTask<Void, Void, Void> {
private Bitmap bitmap;
private String path;
private PhotoSavedListener callback;
public SavingBitmapTask(Bitmap bitmap, String path, PhotoSavedListener callback) {
this.bitmap = bitmap;
this.path = path;
this.callback = callback;
}
@Override
protected Void doInBackground(Void... params) {
FileOutputStream fos = null;
try {
fos = new FileOutputStream(new File(path));
if (bitmap != null && !bitmap.isRecycled()) {
bitmap.compress(Bitmap.CompressFormat.JPEG, CameraConst.COMPRESS_QUALITY, fos);
}
} catch (FileNotFoundException e) {
Timber.e(e, "File not found: " + e.getMessage());
} finally {
try {
if (fos != null) {
fos.close();
}
} catch (IOException e) {
Timber.e(e, e.getMessage());
}
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
if (callback != null) {
callback.photoSaved(path, null);
}
}
}
/*
* The MIT License (MIT)
*
* Copyright (c) 2014 Zillow
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.yalantis.cameramodule.util;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import timber.log.Timber;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.os.AsyncTask;
import android.os.Environment;
import com.yalantis.cameramodule.CameraConst;
import com.yalantis.cameramodule.interfaces.PhotoSavedListener;
public class SavingPhotoTask extends AsyncTask<Void, Void, File> {
private byte[] data;
private String name;
private String path;
private int orientation;
private PhotoSavedListener callback;
public SavingPhotoTask(byte[] data, String name, String path, int orientation) {
this(data, name, path, orientation, null);
}
public SavingPhotoTask(byte[] data, String name, String path, int orientation, PhotoSavedListener callback) {
this.data = data;
this.name = name;
this.path = path;
this.orientation = orientation;
this.callback = callback;
}
@Override
protected File doInBackground(Void... params) {
File photo = getOutputMediaFile();
if (photo == null) {
Timber.e("Error creating media file, check storage permissions");
return null;
}
FileOutputStream fos = null;
try {
fos = new FileOutputStream(photo);
if (orientation == ExifInterface.ORIENTATION_UNDEFINED) {
saveByteArray(fos, data);
} else {
saveByteArrayWithOrientation(fos, data, orientation);
}
} catch (FileNotFoundException e) {
Timber.e(e, "File not found: " + e.getMessage());
} catch (IOException e) {
Timber.e(e, "File write failure: " + e.getMessage());
} finally {
try {
if (fos != null) {
fos.close();
}
} catch (IOException e) {
Timber.e(e, e.getMessage());
}
}
return photo;
}
private void saveByteArray(FileOutputStream fos, byte[] data) throws IOException {
long time = System.currentTimeMillis();
fos.write(data);
Timber.d("saveByteArray: %1dms", System.currentTimeMillis() - time);
}
private void saveByteArrayWithOrientation(FileOutputStream fos, byte[] data, int orientation) {
long totalTime = System.currentTimeMillis();
long time = System.currentTimeMillis();
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
Timber.d("decodeByteArray: %1dms", System.currentTimeMillis() - time);
time = System.currentTimeMillis();
if (orientation != 0 && bitmap.getWidth() > bitmap.getHeight()) {
Matrix matrix = new Matrix();
matrix.postRotate(orientation);
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
Timber.d("createBitmap: %1dms", System.currentTimeMillis() - time);
}
time = System.currentTimeMillis();
bitmap.compress(Bitmap.CompressFormat.JPEG, CameraConst.COMPRESS_QUALITY, fos);
Timber.d("compress: %1dms", System.currentTimeMillis() - time);
bitmap.recycle();
Timber.d("saveByteArrayWithOrientation: %1dms", System.currentTimeMillis() - totalTime);
}
@Override
protected void onPostExecute(File file) {
super.onPostExecute(file);
photoSaved(file);
}
private void photoSaved(File photo) {
if (photo != null) {
if (callback != null) {
callback.photoSaved(photo.getPath(), photo.getName());
}
}
}
/**
* Create a File for saving an image
*/
private File getOutputMediaFile() {
// To be safe, we should check that the SDCard is mounted
if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
Timber.e("External storage " + Environment.getExternalStorageState());
return null;
}
File dir = new File(path);
// Create the storage directory if it doesn't exist
if (!dir.exists()) {
if (!dir.mkdirs()) {
Timber.e("Failed to create directory");
return null;
}
}
return new File(dir.getPath() + File.separator + name);
}
}
/*
* The MIT License (MIT)
*
* Copyright (c) 2014 Zillow
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.yalantis.cameramodule.util;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import com.squareup.picasso.Transformation;
public class ScaleTransformation implements Transformation {
private float width;
private float height;
public ScaleTransformation(float width, float height) {
this.width = width;
this.height = height;
}
@Override
public Bitmap transform(Bitmap source) {
float sWidth = source.getWidth();
float sHeight = source.getHeight();
float xScale;
float yScale;
if (sWidth < sHeight) {
yScale = height / sHeight;
xScale = yScale;
} else {
xScale = width / sWidth;
yScale = xScale;
}
Matrix matrix = new Matrix();
matrix.postScale(xScale, yScale);
Bitmap scaledBitmap = Bitmap.createBitmap(source, 0, 0, (int) sWidth, (int) sHeight, matrix, true);
scaledBitmap.getWidth();
scaledBitmap.getHeight();
if (scaledBitmap != source) {
source.recycle();
}
return scaledBitmap;
}
@Override
public String key() {
return "scaleTo" + width + "x" + height;
}
}
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
...@@ -40,8 +40,10 @@ dependencies { ...@@ -40,8 +40,10 @@ dependencies {
compile project(':openCVLibrary2411') compile project(':openCVLibrary2411')
compile 'com.parse.bolts:bolts-android:1.2.1' compile 'com.parse.bolts:bolts-android:1.2.1'
compile 'com.google.code.gson:gson:2.3' compile 'com.google.code.gson:gson:2.3'
compile group: 'commons-io', name: 'commons-io', version: '2.0.1' compile 'commons-io:commons-io:2.0.1'
compile('com.crashlytics.sdk.android:crashlytics:2.5.3@aar') { compile('com.crashlytics.sdk.android:crashlytics:2.5.3@aar') {
transitive = true; transitive = true;
} }
compile project(':CameraModule')
} }
include ':app' include ':app', ':CameraModule'
include ':openCVLibrary2411' include ':openCVLibrary2411'
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment