Code Monkey home page Code Monkey logo

rapiddecoder's Introduction

Installation

Add this repository to build.gradle in your project's root.

allprojects {
    repositories {
        maven {
            url 'https://github.com/suckgamony/RapidDecoder/raw/master/repository'
        }
    }
}

Add dependencies to build.gradle in your module.

dependencies {
    compile 'rapid.decoder:library:0.3.0'
    compile 'rapid.decoder:jpeg-decoder:0.3.0'
    compile 'rapid.decoder:png-decoder:0.3.0'
}

jpeg-decoder and png-decoder are optional. Refer to Builtin decoder.

Basic decoding

To decode a bitmap from resource:

import rapid.decoder.BitmapDecoder;

Bitmap bitmap = BitmapDecoder.from(getResources(), R.drawable.image).decode();

Bitmap can also be decoded from other sources like this:

// Decodes bitmap from byte array
byte[] bytes;
Bitmap bitmap = BitmapDecoder.from(bytes).decode();

// Decodes bitmap from file
Bitmap bitmap = BitmapDecoder.from("/sdcard/image.png").decode();

// Decodes bitmap from network
Bitmap bitmap = BitmapDecoder.from("http://server.com/image.jpeg").decode();

// Decodes bitmap from content provider
Bitmap bitmap = BitmapDecoder.from("content://app/user/0/profile").decode();

// Decodes bitmap from other app's resource
Bitmap bitmap = BitmapDecoder.from("android.resource://com.app/drawable/ic_launcher")
                             .decode();

// Decodes bitmap from stream
InputStream is;
Bitmap bitmap = BitmapDecoder.from(is).decode();

// Decodes from database
final SQLiteDatabase db;
Bitmap bitmap = BitmapDecoder
        .from(new Queriable() {
            @Override
            public Cursor query() {
                return db.query("table", new String[]{"column"}, "id=1", null, null,
                        null, null);
            }
        })
        .decode();

Advanced decoding

Scaling

All of the scaling operations automatically decode bounds of bitmaps and calculate inSampleSize, so you don't need to consider about them at all.

// Scaling to 400x300
Bitmap bitmap = BitmapDecoder.from(getResouces(), R.drawable.image)
                             .scale(400, 300)
                             .decode();

// Scaling by 50%
Bitmap bitmap = BitmapDecoder.from("/sdcard/image.png")
                             .scaleBy(0.5)
                             .decode();

Regional decoding

Only partial area of bitmap can be decoded. (Supports down to Froyo)

// Decodes the area (100, 200)-(300, 400) of the bitmap scaling it by 50%.
Bitmap bitmap = BitmapDecoder.from("/sdcard/image.jpeg")
                             .region(100, 200, 300, 400)
                             .scaleBy(0.5)
                             .decode();

Mutable decoding

You can directly modify decoded bitmap if it was decoded as mutable. This also supports down to Froyo.

Bitmap bitmap2 = something;
Bitmap bitmap = BitmapDecoder.from(getResources(), R.drawable.image)
                             .mutable()
                             .decode();
Canvas canvas = new Canvas(bitmap);
canvas.draw(bitmap2, 0, 0, null):

Direct drawing

It is possible to draw bitmap directly to canvas from BitmapDecoder. It's generally faster than drawing bitmap after full decoding.

Bitmap bitmap = Bitmap.createBitmap(width, height, Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
BitmapDecoder.from("/image.png").scaleBy(0.2, 0.5).draw(canvas, x, y);

Post processing

You can hook decoded bitmap and replace it to something you want.

// Make rounded image
Bitmap bitmap = BitmapDecoder.from("http://somewhere.com/image.jpeg")
        .postProcessor(new BitmapPostProcessor() {
                @Override
                public Bitmap process(Bitmap bitmap) {
                    Bitmap bitmap2 = Bitmap.createBitmap(bitmap.getWidth(),
                        bitmap.getHeight(), Bitmap.Config.ARGB_8888);
                    Canvas canvas = new Canvas(bitmap2);
                    
                    Paint paint = new Paint();
                    paint.setColor(0xffffffff);
                    RectF area = new RectF(0, 0, bitmap2.getWidth(), bitmap2.getHeight());
                    canvas.drawRoundRect(area, 10, 10, paint);
                    
                    paint.reset();
                    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
                    canvas.drawBitmap(bitmap, 0, 0, paint);
                    
                    return bitmap2;
                }
        })
        .decode();

In this case you MUST NOT recycle the given bitmap.

Builtin decoder

If requsted operations can not be done just by Android APIs, RapidDecoder uses builtin decoder to accomplish them. Currently there are 2 builtin decoders.

  • png-decoder - LibPNG
  • jpeg-decoder - Modified version of jpgd

You have to add appropriate dependencies to get benefits of backward compatibility. See Installation.

Then when will it be needed? It will require builtin decoder if one of the following operations are requsted.

  • Regional decoding on Android < 2.3.3
  • Regional & mutable decoding
  • Mutable decoding on Android < 3.0
  • Scaling more than 50% without scale filter

Framing

You can apply ScaleType to make a decoded bitmap fit into certain size.

Bitmap bitmap = BitmapDecoder.from("content://authority/path")
        .frame(frameWidth, frameHeight, ImageView.ScaleType.CENTER_CROP)
        .decode();

When the image needs to be cropped, it uses region() internally so that only required area can be decoded. In this reason, it takes less memory and less time than implementing it only in APIs provided by Android.

You can also provide your own custom framing method by extending FramedDecoder and FramingMethod. But note that it's not documened yet.

Caching

BitmapDecoder provides memory cache and disk cache. Disk cache only works for network images. Memory cache can be used for decoding bitmaps from network, file, and Android resource by default.

It is needed to initialize caches before any decoding operation.

// Allocate 2MB for memory cache
BitmapDecoder.initMemoryCache(2 * 1024 * 1024);
// Allocate proper amount proportional to screen size for memory cache
BitmapDecoder.initMemoryCache(context);

// Allocate default 8MB for disk cache
BitmapDecoder.initDiskCache(context);
// Allocate 32MB for disk cache
Bitmapdecoder.initDiskCache(context, 32 * 1024 * 1024);

That's it. There's nothing to set anymore. Subsequent decoding will automatically uses caches.

It's also able to make caches disabled temporarily.

// Do not use memory cache this time
Bitmap bitmap = BitmapDecoder.from("/image.jpeg")
        .useMemoryCache(false)
        .decode();
        
// Do not use disk cache this time
Bitmap bitmap = BitmapDecoder.from("http://web.com/image.png", false)
        .decode();

Loading bitmap into view

Bitmaps can be loaded directly into view using into().

BitmapDecoder.from("/image.png").into(view);

Decoding is done in background and it will be displayed fading in on the view. Bitmap will be loaded as an image if the view is ImageView, or it will be loaded as a background.

View binders

If you want to set more parameters to customize behaviours, you should use view binders. Above code is exactly equivalent to below:

import rapid.decoder.binder.ImageViewBinder;
import rapid.decoder.binder.ViewBackgroundBinder;

if (view instanceof ImageView) {
    BitmapDecoder.from("/image.png").into(
            ImageViewBinder.obtain((ImageView) view));
} else {
    BitmapDecoder.from("/image.png").into(
            ViewBackgroundBinder.obtain(view));
}

You can also load bitmaps into TextView's compound drawable by using TextViewBinder.

BitmapDecoder.from("/image.png").into(
        TextViewBinder.obtain(textView, Gravity.LEFT, width, height));

Bitmap will be fade in on loaded by default. That behaviour can be changed the way like this:

BitmapDecoder.from("/image.png").into(
        ImageViewBinder.obtain(imageView).effect(Effect.NO_EFFECT));

There are currently 3 effects provided: NO_EFFECT, FADE_IN, FADE_IN_IF_SYNC. All of these are defined in Effect class. Also you can create your own effect by inheriting Effect class. It's not yet documented but it's easy to understand source code.

Placeholder and error image can be set as following:

ViewBinder<ImageView> binder = ImageViewBinder.obtain(imageView)
        .placeholder(R.drawable.placeholder)
        .errorImage(R.drawable.error);
BitmapDecoder.from("/image.png").into(binder);

By the way, you may want to create a drawable which displays decoded bitmap other than BitmapDrawable. In this case, you can achieve it by overriding createDrawable() from ViewBinder.

BitmapDecoder.from("/image.png").into(
        new ImageViewBinder(imageView) {
            @Override
            public Drawable createDrawable(Context context, Bitmap bitmap) {
                return new YourOwnDrawable(context, bitmap);
            }
        });

It can also be framed with ScaleType. In this case, frame size is determined by the given view's layout.

ViewBinder<ImageView> binder = ImageViewBinder.obtain(imageView)
        .scaleType(ImageView.ScaleType.CENTER_CROP);
BitmapDecoder.from("/image.png").into(binder);

rapiddecoder's People

Contributors

skgmn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

rapiddecoder's Issues

How to decode from ZipInputStream

I have some image files in a Zip file and RapidDecoder was able to decode the first two images and then it crashed. I checked the log and i found out that RapidDecoder was closing the stream after read. Please is there a solution to make it not close the stream?

JavaDocs ? Available features?

I would like to know better about this library.
Are there any JavaDocs ? any list of available features (besides the wiki page) ?

For example, I would like to know if it supports WebP encoding/decoding, if it can have face detection, if it supports direct downsampling of an internet image link (instead of creating 2 input streams or download the file and then check it out),...

Also, I would like to know if using this library ensures that the bitmap isn't held in the heap till you tell it to.

Drawing on canvas

Hi.
I have some hi resolution images(4000 * 5000) and i want to make a large enough canvas(like 10000*15000 named BIGIMG) on the disk to draw those images onto it in one big image with specific coordinates(like the first one on 2000,3000 and second on on 8000,7000) and read the final image and show it on ImageView.
i've used scaling and regional decoding and they work fine on version 0.3.0 but when i use direct drawing or post processing to draw(provided examples), it shows no change on the image.
please direct me on how to do this.
thanks for this great library...

Decoding with BitmapFactory.Option

How can I use your library to decode an image, using BitmapFactory.Option to set some properties such as inPurgeable, inInputShareable,...

Can you please define how to decode image that is selected from sdcard.

I Used the below code for this but it always says file not found. but the same is working in case of gallery.

                 File cameraFile = new File(
                        Environment.getExternalStorageDirectory(),
                        "photo.jgp");
                if (cameraFile.getPath() != null) {
                    Bitmap bitmap = BitmapDecoder.from(context, cameraFile.getPath()).decode();
                    mImageView.setImageBitmap(bitmap);
                }

Null pointer Exception

Hi !

I'm trying to use your decoder for my app and for most part it works great. But when I load many images it starts throwing null pointer exceptions.

gradle file
compile 'rapid.decoder:library:0.2.6'
compile 'rapid.decoder:png-decoder:0.2.6'

My simple use case, a need pictures from texsture atlas for long animations
BitmapDecoder.from(contex.getResources(), contex.getResources().getIdentifier(picture.page, "drawable", "foo")).region(picture.getLeft(), picture.getTop(), picture.getRight(), picture.getBottom()).decode();

*And sooner or later i get java.lang.NullPointerException *
11-17 18:42:32.453 2276-2276/ W/System.err﹕ java.lang.NullPointerException
11-17 18:42:32.453 2276-2276/ W/System.err﹕ at android.graphics.Bitmap.createBitmap(Bitmap.java:810)
11-17 18:42:32.453 2276-2276/ W/System.err﹕ at android.graphics.Bitmap.createBitmap(Bitmap.java:787)
11-17 18:42:32.453 2276-2276/W/System.err﹕ at android.graphics.Bitmap.createBitmap(Bitmap.java:754)
11-17 18:42:32.453 2276-2276/ W/System.err﹕ at rapid.decoder.BitmapLoader.decode(BitmapLoader.java:184)
11-17 18:42:32.453 2276-2276/ W/System.err﹕ at foo.DrawCharacterFrame.drawFrameOfAnimation(DrawCharacterFrame.java:159)

Any help would be appreciated. Keep up good work 👍

It's a good library

this is not an issues!
This is a very good library, the compression quality and intelligibility are very good, it's roughly the same contrast with Tencent's QQ Qzone , thanks the author, also hoped that the author will continue to maintain the library, thanks again

Problem with scaling with region decoding

Say I have 690*10700 image. I want to show it's top part (0;0) - (690;1200). I have variable "regional" in executeDecoding method of BitmapLoader set to false. But it is obviously wrong.

Out of memory

Hi all,
I using regional decoding as:
Bitmap bitmap = BitmapDecoder.from("abc.jpeg")
.region(0, 0, 1080, 1920)
.scaleBy(1)
.decode();
This for image has high solution (14400 × 9600). App is crashed. How to fix it?
Thanks

Scaled problem

Bitmap bitmap = BitmapDecoder.from("file name").decode();
return normal bitmap, but

Bitmap bitmap = BitmapDecoder.from("file name")
                        .scaleBy(2.0f)
                        .decode();

return empty bitmap

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.