Android Open Source - subsampling-scale-image-view Skia Image Region Decoder






From Project

Back to project page subsampling-scale-image-view.

License

The source code is released under:

Apache License

If you think the Android project subsampling-scale-image-view listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.davemorrissey.labs.subscaleview.decoder;
//www  .  j a  v  a2 s.  com
import android.content.ContentResolver;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.graphics.*;
import android.graphics.Bitmap.Config;
import android.net.Uri;
import android.text.TextUtils;

import java.util.List;

/**
 * Default implementation of {@link com.davemorrissey.labs.subscaleview.decoder.ImageRegionDecoder}
 * using Android's {@link android.graphics.BitmapRegionDecoder}, based on the Skia library. This
 * works well in most circumstances and has reasonable performance due to the cached decoder instance,
 * however it has some problems with grayscale, indexed and CMYK images.
 */
public class SkiaImageRegionDecoder implements ImageRegionDecoder {

    private BitmapRegionDecoder decoder;
    private final Object decoderLock = new Object();

    private static final String FILE_PREFIX = "file://";
    private static final String ASSET_PREFIX = FILE_PREFIX + "/android_asset/";
    private static final String RESOURCE_PREFIX = ContentResolver.SCHEME_ANDROID_RESOURCE + "://";

    @Override
    public Point init(Context context, Uri uri) throws Exception {
        String uriString = uri.toString();
        if (uriString.startsWith(RESOURCE_PREFIX)) {
            Resources res;
            String packageName = uri.getAuthority();
            if (context.getPackageName().equals(packageName)) {
                res = context.getResources();
            } else {
                PackageManager pm = context.getPackageManager();
                res = pm.getResourcesForApplication(packageName);
            }

            int id = 0;
            List<String> segments = uri.getPathSegments();
            int size = segments.size();
            if (size == 2 && segments.get(0).equals("drawable")) {
                String resName = segments.get(1);
                id = res.getIdentifier(resName, "drawable", packageName);
            } else if (size == 1 && TextUtils.isDigitsOnly(segments.get(0))) {
                try {
                    id = Integer.parseInt(segments.get(0));
                } catch (NumberFormatException ignored) {
                }
            }

            decoder = BitmapRegionDecoder.newInstance(context.getResources().openRawResource(id), false);
        } else if (uriString.startsWith(ASSET_PREFIX)) {
            String assetName = uriString.substring(ASSET_PREFIX.length());
            decoder = BitmapRegionDecoder.newInstance(context.getAssets().open(assetName, AssetManager.ACCESS_RANDOM), false);
        } else if (uriString.startsWith(FILE_PREFIX)) {
            decoder = BitmapRegionDecoder.newInstance(uriString.substring(FILE_PREFIX.length()), false);
        } else {
            ContentResolver contentResolver = context.getContentResolver();
            decoder = BitmapRegionDecoder.newInstance(contentResolver.openInputStream(uri), false);
        }
        return new Point(decoder.getWidth(), decoder.getHeight());
    }

    @Override
    public Bitmap decodeRegion(Rect sRect, int sampleSize) {
        synchronized (decoderLock) {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = sampleSize;
            options.inPreferredConfig = Config.RGB_565;
            options.inDither = true;
            return decoder.decodeRegion(sRect, options);
        }
    }

    @Override
    public boolean isReady() {
        return decoder != null && !decoder.isRecycled();
    }

    @Override
    public void recycle() {
        decoder.recycle();
    }
}




Java Source Code List

com.davemorrissey.labs.subscaleview.ImageViewState.java
com.davemorrissey.labs.subscaleview.ScaleImageView.java
com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView.java
com.davemorrissey.labs.subscaleview.decoder.ImageDecoder.java
com.davemorrissey.labs.subscaleview.decoder.ImageRegionDecoder.java
com.davemorrissey.labs.subscaleview.decoder.SkiaImageDecoder.java
com.davemorrissey.labs.subscaleview.decoder.SkiaImageRegionDecoder.java
com.davemorrissey.labs.subscaleview.sample.MainActivity.java
com.davemorrissey.labs.subscaleview.sample.SampleActivity.java
com.davemorrissey.labs.subscaleview.sample.animation.AnimationActivity.java
com.davemorrissey.labs.subscaleview.sample.basicfeatures.BasicFeaturesActivity.java
com.davemorrissey.labs.subscaleview.sample.configuration.ConfigurationActivity.java
com.davemorrissey.labs.subscaleview.sample.eventhandling.EventHandlingActivity.java
com.davemorrissey.labs.subscaleview.sample.eventhandlingadvanced.AdvancedEventHandlingActivity.java
com.davemorrissey.labs.subscaleview.sample.extension.ExtensionActivity.java
com.davemorrissey.labs.subscaleview.sample.extension.ExtensionCircleFragment.java
com.davemorrissey.labs.subscaleview.sample.extension.ExtensionFreehandFragment.java
com.davemorrissey.labs.subscaleview.sample.extension.ExtensionPinFragment.java
com.davemorrissey.labs.subscaleview.sample.extension.views.CircleView.java
com.davemorrissey.labs.subscaleview.sample.extension.views.FreehandView.java
com.davemorrissey.labs.subscaleview.sample.extension.views.PinView.java
com.davemorrissey.labs.subscaleview.sample.imagedisplay.ImageDisplayActivity.java
com.davemorrissey.labs.subscaleview.sample.imagedisplay.ImageDisplayLargeFragment.java
com.davemorrissey.labs.subscaleview.sample.imagedisplay.ImageDisplayRotateFragment.java
com.davemorrissey.labs.subscaleview.sample.imagedisplay.decoders.RapidImageDecoder.java
com.davemorrissey.labs.subscaleview.sample.imagedisplay.decoders.RapidImageRegionDecoder.java
com.davemorrissey.labs.subscaleview.sample.viewpager.ViewPagerActivity.java
com.davemorrissey.labs.subscaleview.sample.viewpager.ViewPagerFragment.java