From 2b0c9f6230a775dbeca1ffd9a3fd27c203db6460 Mon Sep 17 00:00:00 2001 From: Victor Date: Sun, 29 May 2016 20:48:32 +0200 Subject: [PATCH] Fix #16. Added support for setAspectRatio, useSourceImageAspectRatio and setMaxResultSize UCrop features. --- README.md | 14 +++- .../sample/fragments/SampleFragment.java | 5 +- .../rx_paparazzo/RxPaparazzo.java | 4 +- .../rx_paparazzo/entities/Options.java | 78 +++++++++++++++++++ .../rx_paparazzo/interactors/CropImage.java | 21 ++++- 5 files changed, 113 insertions(+), 9 deletions(-) create mode 100644 rx_paparazzo/src/main/java/com/fuck_boilerplate/rx_paparazzo/entities/Options.java diff --git a/README.md b/README.md index 262705a..f846f9f 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ allprojects { And add next dependencies in the build.gradle of the module: ```gradle dependencies { - compile "com.github.FuckBoilerplate:RxPaparazzo:0.1.1" + compile "com.github.FuckBoilerplate:RxPaparazzo:0.1.2" compile "io.reactivex:rxjava:1.1.5" } ``` @@ -147,16 +147,24 @@ RxPaparazzo.takeImages(activityOrFragment) By calling `crop()` method when building the observable instance, all they images retrieved will be able to be cropped, regardless if the images were retrieved using the built-in camera or gallery, even if multiple images were requested in a single call using `takeImages()` approach. Because uCrop Yalantis library exposes some configuration in order to customize the crop screen, RxPaparazzo exposes an overloaded method of `crop(UCrop.Options)` which allow to pass an instance of [UCrop.Options](https://github.com/Yalantis/uCrop/blob/master/ucrop/src/main/java/com/yalantis/ucrop/UCrop.java#L211). +If you need to configure the aspect ratio, the max result size or using the source image aspect ratio, you must pass an instance of [Options](https://github.com/FuckBoilerplate/RxPaparazzo/blob/master/rx_paparazzo/src/main/java/com/fuck_boilerplate/rx_paparazzo/entities/Options.java) class, which extends from `UCrop.Options` and adds the three missing properties. ```java UCrop.Options options = new UCrop.Options(); options.setToolbarColor(ContextCompat.getColor(getActivity(), R.color.colorPrimaryDark)); - + +RxPaparazzo.takeImage(activityOrFragment).crop(options) +``` + +```java +Options options = new Options(); +options.setToolbarColor(ContextCompat.getColor(getActivity(), R.color.colorPrimaryDark)); +Options.setAspectRatio(25, 50); + RxPaparazzo.takeImage(activityOrFragment) .crop(options) ``` - ## Credits * Runtime permissions: [RxPermissions](https://github.com/tbruyelle/RxPermissions) * Crop: [uCrop](https://github.com/Yalantis/uCrop) diff --git a/app/src/main/java/com/fuck_boilerplate/rx_paparazzo/sample/fragments/SampleFragment.java b/app/src/main/java/com/fuck_boilerplate/rx_paparazzo/sample/fragments/SampleFragment.java index 98acaad..cbf6a83 100644 --- a/app/src/main/java/com/fuck_boilerplate/rx_paparazzo/sample/fragments/SampleFragment.java +++ b/app/src/main/java/com/fuck_boilerplate/rx_paparazzo/sample/fragments/SampleFragment.java @@ -13,6 +13,7 @@ import android.widget.Toast; import com.fuck_boilerplate.rx_paparazzo.RxPaparazzo; +import com.fuck_boilerplate.rx_paparazzo.entities.Options; import com.fuck_boilerplate.rx_paparazzo.entities.Size; import com.fuck_boilerplate.rx_paparazzo.sample.R; import com.fuck_boilerplate.rx_paparazzo.sample.activities.Testable; @@ -74,9 +75,9 @@ private void captureImage() { } private void captureImageWithCrop() { - UCrop.Options options = new UCrop.Options(); + Options options = new Options(); options.setToolbarColor(ContextCompat.getColor(getActivity(), R.color.colorAccent)); - options.setMaxBitmapSize(1000000000); + options.setAspectRatio(25, 75); size = Size.Original; RxPaparazzo.takeImage(this) diff --git a/rx_paparazzo/src/main/java/com/fuck_boilerplate/rx_paparazzo/RxPaparazzo.java b/rx_paparazzo/src/main/java/com/fuck_boilerplate/rx_paparazzo/RxPaparazzo.java index 76ad796..ba3549e 100644 --- a/rx_paparazzo/src/main/java/com/fuck_boilerplate/rx_paparazzo/RxPaparazzo.java +++ b/rx_paparazzo/src/main/java/com/fuck_boilerplate/rx_paparazzo/RxPaparazzo.java @@ -101,7 +101,7 @@ public BuilderImage crop() { /** * Call it when crop option is required as such as configuring the options of the cropping action. */ - public BuilderImage crop(UCrop.Options options) { + public BuilderImage crop(O options) { this.config.setCrop(options); return this; } @@ -151,7 +151,7 @@ public BuilderImages crop() { /** * Call it when crop option is required as such as configuring the options of the cropping action. */ - public BuilderImages crop(UCrop.Options options) { + public BuilderImages crop(O options) { this.config.setCrop(options); return this; } diff --git a/rx_paparazzo/src/main/java/com/fuck_boilerplate/rx_paparazzo/entities/Options.java b/rx_paparazzo/src/main/java/com/fuck_boilerplate/rx_paparazzo/entities/Options.java new file mode 100644 index 0000000..c03ffde --- /dev/null +++ b/rx_paparazzo/src/main/java/com/fuck_boilerplate/rx_paparazzo/entities/Options.java @@ -0,0 +1,78 @@ +/* + * Copyright 2016 FuckBoilerplate + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.fuck_boilerplate.rx_paparazzo.entities; + +import android.support.annotation.IntRange; + +import com.yalantis.ucrop.UCrop; + +public class Options extends UCrop.Options { + private boolean useSourceImageAspectRatio; + private float x, y; + private int width, height; + + /** + * Set an aspect ratio for crop bounds. + * User won't see the menu with other ratios options. + * + * @param x aspect ratio X + * @param y aspect ratio Y + */ + public void setAspectRatio(float x, float y) { + this.x = x; + this.y = y; + } + + /** + * Set an aspect ratio for crop bounds that is evaluated from source image width and height. + * User won't see the menu with other ratios options. + */ + public void useSourceImageAspectRatio() { + useSourceImageAspectRatio = true; + } + + /** + * Set maximum size for result cropped image. + * + * @param width max cropped image width + * @param height max cropped image height + */ + public void setMaxResultSize(@IntRange(from = 100) int width, @IntRange(from = 100) int height) { + this.width = width; + this.height = height; + } + + public boolean isUseSourceImageAspectRatio() { + return useSourceImageAspectRatio; + } + + public float getX() { + return x; + } + + public float getY() { + return y; + } + + public int getWidth() { + return width; + } + + public int getHeight() { + return height; + } +} diff --git a/rx_paparazzo/src/main/java/com/fuck_boilerplate/rx_paparazzo/interactors/CropImage.java b/rx_paparazzo/src/main/java/com/fuck_boilerplate/rx_paparazzo/interactors/CropImage.java index 5fdae50..3282d2d 100644 --- a/rx_paparazzo/src/main/java/com/fuck_boilerplate/rx_paparazzo/interactors/CropImage.java +++ b/rx_paparazzo/src/main/java/com/fuck_boilerplate/rx_paparazzo/interactors/CropImage.java @@ -20,6 +20,7 @@ import android.net.Uri; import com.fuck_boilerplate.rx_paparazzo.entities.Config; +import com.fuck_boilerplate.rx_paparazzo.entities.Options; import com.fuck_boilerplate.rx_paparazzo.entities.TargetUi; import com.yalantis.ucrop.UCrop; @@ -71,12 +72,28 @@ public Observable getIntent() { return getOutputUri().map(new Func1() { @Override public Intent call(Uri outputUri) { - return UCrop.of(uri, outputUri).withOptions(config.getOptions()) - .getIntent(targetUi.getContext()); + UCrop.Options options = config.getOptions(); + if (options == null) return UCrop.of(uri, outputUri).getIntent(targetUi.getContext()); + + if (options instanceof Options) { + return getIntentWithOptions((Options) options, outputUri); + } else { + return UCrop.of(uri, outputUri).withOptions(config.getOptions()) + .getIntent(targetUi.getContext()); + } } }); } + private Intent getIntentWithOptions(Options options, Uri outputUri) { + UCrop uCrop = UCrop.of(uri, outputUri); + + uCrop = uCrop.withOptions(options); + if (options.getX() != 0) uCrop = uCrop.withAspectRatio(options.getX(), options.getY()); + if (options.getWidth() != 0) uCrop = uCrop.withMaxResultSize(options.getWidth(), options.getHeight()); + + return uCrop.getIntent(targetUi.getContext()); + } private Observable getOutputUri() { return getPath.with(uri).react().map(new Func1() { @Override