How can I use BitmapRegionDecoder code in android 2.2.2 (Froyo)?

白昼怎懂夜的黑 提交于 2019-11-28 06:17:14

As @hackbod mentioned BitmapRegionDecoder is based on external skia library. Yet it's may be a benefit.

Let's examine original source:

  • BitmapRegionDecoder.java. Mostly defines wrappers around native methods:

    private static native Bitmap nativeDecodeRegion(int lbm,
        int start_x, int start_y, int width, int height,
        BitmapFactory.Options options);
    private static native int nativeGetWidth(int lbm);
    private static native int nativeGetHeight(int lbm);
    private static native void nativeClean(int lbm);
    // ...multiply nativeNewInstance overloads follow
    

    Class doesn't use any new Java APIs we'd need to backport.

  • BitmapRegionDecoder.cpp. Header files it includes consist of ones which are present in Froyo except these two:

    • AutoDecodeCancel.h. The only line it's used in:

      AutoDecoderCancel   adc(options, decoder);
      

      This class handles SkDecoder instances lifecycle. It's a small piece of code and may be well back-ported.

    • SkBitmapRegionDecoder.h

      As filename states this is a core component. In fact, all previous were a kind of wrappers around it. The good news is that we may not need to back-port it as it should be possible to take a whole skia library from the Gingerbeard and compile it under Froyo as it is external and doesn't contain any new dependencies.

P.S. I didn't actually dive deep in the code so please correct me if there's anything I overlooked.

Update:

Source code we need is located in following repositories on branches froyo-release and gingerbread-mr4-release:

You can back-port some code, if it can exist on top of the SDK you are porting it to.

You can't back-port anything. For example, you couldn't back-port a kernel feature. :)

In this case, there is no easy solution to back-porting it. The implementation of this sits on top of Skia and the jpeg decoder, which are both native code. You will need to do your own implementation of that code. You could try copy/pasting the code from the platform, gluing it in with your code with JNI, but this will be a significant amount of work and leave you with native code you need to continue to maintain.

Sorry there is no easy solution for this.

You should consider BitmapRegionDecoderCompat, an API 8+ version of the standard BitmapRegionDecoder (API 10+).

Features

  • It operates in "compat" mode on devices running API < 10 using a basic Java/Android fallback (which means it won't be as efficient/fast as the native JNI implementation of API 10+, but it will avoid ugly boilerplates and manual fallbacks).
  • It uses the native JNI implementation when running on API 10+.
  • It adds extra usuful methods like decodeBestRegion(), which extracts the "best" image subregion given your parameters (gravity, size). This method also works on API < 10.

Download

In order to use it in your project you can manually download and add the library as an AAR file:

or you can add the dependecy in your build.gradle (requires jCenter repository):

dependencies {
    //...your dependecies
    compile 'org.bonnyfone:brdcompat:0.1'
}

Usage

As stated in the docs, in order to migrate to BRDCompat you just need to change the base class name from BitmapRegionDecoder to BitmapRegionDecoderCompat:

//BitmapRegionDecoder brd = BitmapRegionDecoder.newInstance(...);
BitmapRegionDecoderCompat brd = BitmapRegionDecoderCompat.newInstance(...);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!