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

Go To StackoverFlow.com

30

I was reading an answer to a different question on SO, in which @RomainGuy commented that one could (please correct me if I'm paraphrasing incorrectly) back-port code from later versions of android to earlier versions. Specifically, I am interested in back-porting code for BitmapRegionDecoder from Android version 2.3.3 (Gingerbread) to version 2.2.2 (Froyo).

I would have rather asked the question more generally as what is the best practice / what should be avoided when back-porting code from newer versions of Android to older versions, but stackoverflow hinted that my question might be closed as being too subjective.

Maybe if there is enough interest in the topic, this question could be "morphed" into a more general one..possibly a community wiki?

In any case, I would appreciate any insight into how this is done..whether specific to my use case, or more general advice. Do calls to native methods from within the java class complicate the matter (necessarily involving the NDK)?

If it is indeed possible (and reasonable) to cherry-pick and back-port code in this way, I think many would find it very useful to know how.

2012-04-05 20:00
by snapfractalpop
I will be offering a bounty according to this formula: bounty ~ floor(max(upvotes - downvotes, 0) / 10) * 50. In other words, for each 50 rep I gain for votes on this question, I will set a bounty for 50 points - snapfractalpop 2012-04-19 15:14
I realized that I could not create more than one bounty at a time for one question, so I will set bounties sequentially. Also, I just realized that after awarding a 50 point bounty, that bounty size is no longer available..so the next bounty will be 100 points (if the question gets enough votes). According to the "formula" above, I will continue to award points from this question to answers that provide the most useful information during the bounty period. Ultimately, I will do my best to "break even" on this question, and donate points gained toward the best answers - snapfractalpop 2012-06-04 18:33
How about a 250 rep bounty award for bonnyz's answer, based on your formula ; - corsair992 2015-07-20 20:11
@corsair992 well, it would be 100. That amount will be awarded shortly (and likely to bonnyz after reviewing / testing the solution) - snapfractalpop 2015-07-21 03:21


9

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:

2012-06-02 21:52
by Andrey Ermakov
Thanks for the optimism! Which is the "authoritative" git repository? I want to experiment with this and document my results here for the benefit of others - snapfractalpop 2012-06-03 13:45
All the code can be found at https://github.com/android. It's also convenient to use http://androidxref.com/source/ for search. I'll try to do it myself also - Andrey Ermakov 2012-06-03 13:52
Sorry for the "noob" questions, but which repository would it be under in that list? Should I be cloning more than one of them - snapfractalpop 2012-06-03 13:55
@snapfractalpop added this info to the answer. You should clone two repos and then try to compile skia from gingerbread under froyo (create a NDK app with it). Although it may be much easier to clone all AOSP as described here and add your back-port somewhere into it - Andrey Ermakov 2012-06-03 14:09
Thanks for the useful information! I gave you the green check, so unless somebody else gives an even better anwser, you will likely be awared the bounty tomorrow. Also, I will still be awarding new bounties according to the "formula" - snapfractalpop 2012-06-03 14:14


8

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.

2012-05-31 00:55
by hackbod
I was afraid it might involve native code.. can the native code be used from git repo as well? Also, where is the git repo - snapfractalpop 2012-05-31 02:00
Instructions for getting the source are here: http://source.android.com/source/index.htm - hackbod 2012-06-04 07:05


7

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:

Download

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(...);
2015-07-20 17:47
by bonnyz
Ads