crowelch / RxPaparazzo

RxJava extension for Android to take images using camera and gallery

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Android Arsenal

RxJava extension for Android to access camera and gallery to take images.

RxPaparazzo

What is a Paparazzo?

A freelance photographer who aggressively pursues celebrities for the purpose of taking candid photographs.

This library does that. Not really. But it was a funny name, thought. Was it?

Features:

  • Runtime permissions. Not worries about the tricky Android runtime permissions system. RxPaparazzo relies on RxPermissions to deal with that.
  • Take a photo using the built-in camera.
  • Access to gallery.
  • Crop images. RxPaparazzo relies on UCrop to perform beautiful cuts to any face, body or place.
  • Honors the observable chain (it means you can go crazy chaining operators). RxOnActivityResult allows RxPaparazzo to transform every intent into an observable for a wonderful chaining process.

Setup

Add the JitPack repository in your build.gradle (top level module):

allprojects {
    repositories {
        jcenter()
        maven { url "https://jitpack.io" }
    }
}

And add next dependencies in the build.gradle of the module:

dependencies {
    compile "com.github.miguelbcr:RxPaparazzo:0.2.0"
    compile "io.reactivex:rxjava:1.1.5"
}

Usage

Because RxPaparazzo uses RxActivityResult to deal with intent calls, all its requirements and features are inherited too.

Before attempting to use RxPaparazzo, you need to call RxPaparazzo.register in your Android Application class, supplying as parameter the current instance.

public class SampleApp extends Application {

    @Override public void onCreate() {
        super.onCreate();
        RxPaparazzo.register(this);
    }
}

Every feature RxPaparazzo exposes can be accessed from both, an activity or a fragment instance.

Limitation:: Your fragments need to extend from android.support.v4.app.Fragment instead of android.app.Fragment, otherwise they won't be notified.

The generic type of the observable returned by RxPaparazzo when subscribing to any of its features is always an instance of Response class.

This instance holds a reference to the current Activity/Fragment, accessible calling targetUI() method. Because the original one may be recreated it would be unsafe calling it. Instead, you must call any method/variable of your Activity/Fragment from this instance encapsulated in the response instance.

Also, this instance holds a reference to the data as the appropriate response, as such as the result code of the specific operation.

Calling built-in camera to take a photo.

RxPaparazzo.takeImage(activityOrFragment)
        .usingCamera()
        .subscribe(response -> {
            if (response.resultCode() != RESULT_OK) {
                response.targetUI().showUserCanceled();
                return;
            }

            response.targetUI().loadImage(response.data());
        });

The response instance holds a reference to the path where the image was persisted.

By default, the path is under app name folder on the root of the external storage, but you can save the images in internal storage by using .useInternalStorage()

Calling the gallery to retrieve an image.

RxPaparazzo.takeImage(activityOrFragment)
        .usingGallery()
        .subscribe(response -> {
            if (response.resultCode() != RESULT_OK) {
                response.targetUI().showUserCanceled();
                return;
            }

            response.targetUI().loadImage(response.data());
        });

The response instance holds a reference to the path where the image was persisted. Same as the previous example.

Calling the gallery to retrieve multiple image

RxPaparazzo.takeImages(activityOrFragment)
        .usingGallery()
        .subscribe(response -> {
            if (response.resultCode() != RESULT_OK) {
                response.targetUI().showUserCanceled();
                return;
            }

            if (response.data().size() == 1) response.targetUI().loadImage(response.data().get(0));
            else response.targetUI().loadImages(response.data());
        });

The response instance holds a reference to the paths where the images were persisted.

Note: if the level Android api device is minor than 18, only one image will be retrieved.

Customizations

When asking RxPaparazzo for an image -whether it was retrieved using the built-in camera or via gallery, it's possible to apply some configurations to the action.

Size options

Size values enum can be used to set the size of the image to retrieve. There are 3 options:

  • Small: 1/8 aprox. of the screen resolution
  • Screen: The size image matches aprox. the screen resolution.
  • Original: The original size of the image.

Screen value will be set as default.

RxPaparazzo.takeImages(activityOrFragment)
                .size(Size.Small)
                .usingGallery()

Cropping support for image.

This feature is available thanks to the amazing library uCrop authored by Yalantis group.

RxPaparazzo.takeImages(activityOrFragment)
                .crop()

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. 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 class, which extends from UCrop.Options and adds the three missing properties.

UCrop.Options options = new UCrop.Options();
options.setToolbarColor(ContextCompat.getColor(getActivity(), R.color.colorPrimaryDark));

RxPaparazzo.takeImage(activityOrFragment).crop(options)
Options options = new Options();
options.setToolbarColor(ContextCompat.getColor(getActivity(), R.color.colorPrimaryDark));
options.setAspectRatio(25, 50); 

RxPaparazzo.takeImage(activityOrFragment)
         .crop(options)

Credits

Authors

Víctor Albertos

Miguel García

Another author's libraries using RxJava:

  • RxCache: Reactive caching library for Android and Java.
  • RxGcm: RxJava extension for Gcm which acts as an architectural approach to easily satisfy the requirements of an android app when dealing with push notifications.
  • RxActivityResult: A reactive-tiny-badass-vindictive library to break with the OnActivityResult implementation as it breaks the observables chain.

About

RxJava extension for Android to take images using camera and gallery

License:Apache License 2.0


Languages

Language:Java 100.0%