1 package com.davemorrissey.labs.subscaleview.decoder;
2 
3 import android.content.Context;
4 import android.graphics.Bitmap;
5 import android.graphics.Point;
6 import android.graphics.Rect;
7 import android.net.Uri;
8 
9 /**
10  * Interface for image decoding classes, allowing the default {@link android.graphics.BitmapRegionDecoder}
11  * based on the Skia library to be replaced with a custom class.
12  */
13 public interface ImageRegionDecoder {
14 
15     /**
16      * Initialise the decoder. When possible, perform initial setup work once in this method. The
17      * dimensions of the image must be returned. The URI can be in one of the following formats:
18      * <br>
19      * File: <code>file:///scard/picture.jpg</code>
20      * <br>
21      * Asset: <code>file:///android_asset/picture.png</code>
22      * <br>
23      * Resource: <code>android.resource://com.example.app/drawable/picture</code>
24      * @param context Application context. A reference may be held, but must be cleared on recycle.
25      * @param uri URI of the image.
26      * @return Dimensions of the image.
27      * @throws Exception if initialisation fails.
28      */
init(Context context, Uri uri)29     Point init(Context context, Uri uri) throws Exception;
30 
31     /**
32      * <p>
33      * Decode a region of the image with the given sample size. This method is called off the UI
34      * thread so it can safely load the image on the current thread. It is called from
35      * {@link android.os.AsyncTask}s running in an executor that may have multiple threads, so
36      * implementations must be thread safe. Adding <code>synchronized</code> to the method signature
37      * is the simplest way to achieve this, but bear in mind the {@link #recycle()} method can be
38      * called concurrently.
39      * </p><p>
40      * See {@link SkiaImageRegionDecoder} and {@link SkiaPooledImageRegionDecoder} for examples of
41      * internal locking and synchronization.
42      * </p>
43      * @param sRect Source image rectangle to decode.
44      * @param sampleSize Sample size.
45      * @return The decoded region. It is safe to return null if decoding fails.
46      */
decodeRegion(Rect sRect, int sampleSize)47     Bitmap decodeRegion(Rect sRect, int sampleSize);
48 
49     /**
50      * Status check. Should return false before initialisation and after recycle.
51      * @return true if the decoder is ready to be used.
52      */
isReady()53     boolean isReady();
54 
55     /**
56      * This method will be called when the decoder is no longer required. It should clean up any resources still in use.
57      */
recycle()58     void recycle();
59 
60 }
61