Skip to content

Commit

Permalink
Fix #16. Added support for setAspectRatio, useSourceImageAspectRatio …
Browse files Browse the repository at this point in the history
…and setMaxResultSize UCrop features.
  • Loading branch information
Victor committed May 29, 2016
1 parent 2009e5e commit 2b0c9f6
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 9 deletions.
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
```
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public BuilderImage<T> crop() {
/**
* Call it when crop option is required as such as configuring the options of the cropping action.
*/
public BuilderImage<T> crop(UCrop.Options options) {
public <O extends UCrop.Options> BuilderImage<T> crop(O options) {
this.config.setCrop(options);
return this;
}
Expand Down Expand Up @@ -151,7 +151,7 @@ public BuilderImages<T> crop() {
/**
* Call it when crop option is required as such as configuring the options of the cropping action.
*/
public BuilderImages<T> crop(UCrop.Options options) {
public <O extends UCrop.Options> BuilderImages<T> crop(O options) {
this.config.setCrop(options);
return this;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -71,12 +72,28 @@ public Observable<Intent> getIntent() {
return getOutputUri().map(new Func1<Uri, Intent>() {
@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<Uri> getOutputUri() {
return getPath.with(uri).react().map(new Func1<String, Uri>() {
@Override
Expand Down

0 comments on commit 2b0c9f6

Please sign in to comment.