GithubHelp home page GithubHelp logo

leviyehonatan / extimageview Goto Github PK

View Code? Open in Web Editor NEW

This project forked from wwdablu/extimageview

0.0 1.0 0.0 11.11 MB

Android library allowing cropping of images while displaying them to the user. Allows cropping in either defined mode or freeform mode.

License: MIT License

Java 100.00%

extimageview's Introduction

ExtImageView

Android library allowing cropping of images while displaying them to the user. It allows three (3) forms of cropping:

  • Defined (Rectangle)
  • Freeform (any custom shape)
  • Trapezoid (for trapezoid to rect cropping)

Gradle

maven { url 'https://jitpack.io' }

dependencies {
    implementation 'com.github.wwdablu:ExtImageView:x.y.z'
}

Usage

You can define it in XML as follows:

  • Defined
<com.wwdablu.soumya.extimageview.rect.ExtRectImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/iv_display"
    />
  • Freeform
<com.wwdablu.soumya.extimageview.free.ExtFreeImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/iv_display_free"
    />
  • Trapezoid
<com.wwdablu.soumya.extimageview.trapez.ExtTrapezImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/iv_capture_preview"
    />

Once the layout has been inflated we need to pass the actual bitmap which will be used for cropping. For example:

BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = false;
options.inDensity = 0;
options.inTargetDensity = 0;
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.sample);

extImageView.setImageBitmap(bitmap);

Crop(ping)

To crop the image after selection, we simply need to call the crop method. In here we pass a Result as a callback to get notified once the crop process is completed or if an exception is generated.

extImageView.crop(resultHandler);

To get the cropped image, we need to call getCroppedBitmap which will then return the bitmap. For example:

extFreeImageView.getCroppedBitmap(new Result<Bitmap>() {
    @Override
    public void onComplete(Bitmap data) {
        runOnUiThread(() -> {

            findViewById(R.id.iv_display_free).setVisibility(View.GONE);
            findViewById(R.id.iv_display_cropped).setVisibility(View.VISIBLE);

            ((AppCompatImageView) findViewById(R.id.iv_display_cropped))
                    .setImageBitmap(data);
        });
    }

    @Override
    public void onError(Throwable throwable) {
        runOnUiThread(() -> Toast.makeText(MainActivity.this,
                "Could not get cropped bitmap" + throwable.getMessage(),
                Toast.LENGTH_SHORT).show());
    }
});

Note

It creates temporary bitmap files in the cache folder for the crop process. Once the view is detached from the window, these files are removed. This is done automatically. But if the app crashes or in any other scenario detach is not called then these files will linger. If you want you can explicitly remove files from the cache folder with the name ending with _o.png.

Sample

extimageview's People

Contributors

wwdablu avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.