[英]How can I use BitmapRegionDecoder code in android 2.2.2 (Froyo)?
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.我正在阅读关于 SO的另一个问题的答案,其中@RomainGuy 评论说可以(如果我解释不正确,请纠正我)将代码从 android 的更高版本反向移植到早期版本。 Specifically, I am interested in back-porting code for BitmapRegionDecoder from Android version 2.3.3 (Gingerbread) to version 2.2.2 (Froyo).
具体来说,我有兴趣将 BitmapRegionDecoder 的代码从 Android 版本 2.3.3 (Gingerbread) 反向移植到版本 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.我宁愿更笼统地问这个问题,因为最佳实践是什么/当将代码从 Android 的较新版本反向移植到旧版本时应该避免什么,但 stackoverflow 暗示我的问题可能会因为过于主观而被关闭。
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)?
从 java class 内部调用本地方法是否会使事情复杂化(必然涉及 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.如果确实有可能(并且合理地)以这种方式挑选和向后移植代码,我想很多人会发现知道如何做非常有用。
As @hackbod mentioned BitmapRegionDecoder
is based on external skia
library.正如@hackbod 提到的,
BitmapRegionDecoder
基于外部skia
库。 Yet it's may be a benefit.但这可能是一个好处。
Let's examine original source:让我们检查原始来源:
BitmapRegionDecoder.java
. 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. Class 不使用我们需要向后移植的任何新的 Java API。
BitmapRegionDecoder.cpp
. BitmapRegionDecoder.cpp
。 Header files it includes consist of ones which are present in Froyo
except these two:它包含的 Header 个文件由
Froyo
中存在的文件组成,除了以下两个:
AutoDecodeCancel.h
. AutoDecodeCancel.h
。 The only line it's used in:唯一使用它的行:
AutoDecoderCancel adc(options, decoder);
This class handles SkDecoder
instances lifecycle.这个 class 处理
SkDecoder
实例生命周期。 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.好消息是我们可能不需要向后移植它,因为它应该可以从
Gingerbeard
获取整个skia
库并在Froyo
下编译它,因为它是外部的并且不包含任何新的依赖项。
PS I didn't actually dive deep in the code so please correct me if there's anything I overlooked. PS 我实际上并没有深入研究代码,所以如果有任何我忽略的地方,请纠正我。
Source code we need is located in following repositories on branches froyo-release
and gingerbread-mr4-release
:我们需要的源代码位于分支 froyo
froyo-release
和gingerbread-mr4-release
的以下存储库中:
include/core
and include/images
include/core
和include/images
graphics/java/android/graphics/BitmapRegionDecoder.java
graphics/java/android/graphics/BitmapRegionDecoder.java
core/jni/android/graphics/...
core/jni/android/graphics/...
You can back-port some code, if it can exist on top of the SDK you are porting it to.您可以向后移植一些代码,如果它可以存在于您要移植到的 SDK 之上。
You can't back-port anything.你不能向后移植任何东西。 For example, you couldn't back-port a kernel feature.
例如,您无法向后移植 kernel 功能。 :)
:)
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.
它的实现位于 Skia 和 jpeg 解码器之上,它们都是本机代码。 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.
您可以尝试从平台复制/粘贴代码,使用 JNI 将其与您的代码粘合在一起,但这将是大量工作,并且留给您需要继续维护的本机代码。
Sorry there is no easy solution for this.抱歉,没有简单的解决方案。
You should consider BitmapRegionDecoderCompat , an API 8+ version of the standard BitmapRegionDecoder (API 10+).您应该考虑BitmapRegionDecoderCompat ,它是标准BitmapRegionDecoder (API 10+) 的API 8+版本。
decodeBestRegion()
, which extracts the "best" image subregion given your parameters (gravity, size).decodeBestRegion()
,它根据您的参数(重力,大小)提取“最佳”图像子区域。 This method also works on API < 10.In order to use it in your project you can manually download and add the library as an AAR file:为了在您的项目中使用它,您可以手动下载库并将其添加为AAR文件:
or you can add the dependecy in your build.gradle (requires jCenter repository):或者您可以在build.gradle中添加依赖项(需要jCenter存储库):
dependencies {
//...your dependecies
compile 'org.bonnyfone:brdcompat:0.1'
}
As stated in the docs, in order to migrate to BRDCompat you just need to change the base class name from BitmapRegionDecoder
to BitmapRegionDecoderCompat
:如文档中所述,为了迁移到 BRDCompat,您只需将基本名称 class 从
BitmapRegionDecoder
为BitmapRegionDecoderCompat
:
//BitmapRegionDecoder brd = BitmapRegionDecoder.newInstance(...);
BitmapRegionDecoderCompat brd = BitmapRegionDecoderCompat.newInstance(...);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.