图像解码器
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
NDK ImageDecoder
API 提供了一种标准 API,供 Android C/C++ 应用直接解码图像。应用开发者不再需要使用 Java API(通过 JNI)或第三方图像解码库。此 API 与 Bitmap 模块中的编码函数结合使用,可实现以下功能:
- 缩减原生应用和库的大小,因为它们不再需要链接自己的解码库。
- 应用和库可自动从解码库的平台安全更新中受益。
- 应用可以直接将图像解码到其提供的内存中。如果需要的话,应用接下来可以对图像数据进行后处理,并将其传递给 OpenGL 或其绘制代码。
本页介绍了如何使用 API 解码图像。
可用性和功能
ImageDecoder
API 适用于以 Android 11(API 级别 30)或更高版本为目标平台的应用。实现位于以下文件中:
imagedecoder.h
,适用于解码器
bitmap.h
,适用于编码器
libjnigraphics.so
该 API 支持以下图片格式:
- JPEG
- PNG
- GIF
- WebP
BMP
ICO
WBMP
HEIF
数字底片(通过 DNG SDK)
为了涵盖已解码原始图片的所有用法,此 API 不提供更高级别的对象,例如在 Java 框架内基于解码图片构建的对象,例如:
解码图像
从表示编码图像的某种形式的输入开始解码。AImageDecoder
接受多种类型的输入:
以下代码显示了如何从文件中打开图片 Asset
,对其进行解码,然后正确处理解码器和素材资源。如需查看渲染已解码图片的示例,请参阅 Teapot 示例。
AAssetManager* nativeManager = AAssetManager_fromJava(env, jAssets);
const char* file = // Filename
AAsset* asset = AAssetManager_open(nativeManager, file, AASSET_MODE_STREAMING);
AImageDecoder* decoder;
int result = AImageDecoder_createFromAAsset(asset, &decoder);
if (result != ANDROID_IMAGE_DECODER_SUCCESS) {
// An error occurred, and the file could not be decoded.
}
const AImageDecoderHeaderInfo* info = AImageDecoder_getHeaderInfo(decoder);
int32_t width = AImageDecoderHeaderInfo_getWidth(info);
int32_t height = AImageDecoderHeaderInfo_getHeight(info);
AndroidBitmapFormat format =
(AndroidBitmapFormat) AImageDecoderHeaderInfo_getAndroidBitmapFormat(info);
size_t stride = AImageDecoder_getMinimumStride(decoder); // Image decoder does not
// use padding by default
size_t size = height * stride;
void* pixels = malloc(size);
result = AImageDecoder_decodeImage(decoder, pixels, stride, size);
if (result != ANDROID_IMAGE_DECODER_SUCCESS) {
// An error occurred, and the file could not be decoded.
}
// We’re done with the decoder, so now it’s safe to delete it.
AImageDecoder_delete(decoder);
// The decoder is no longer accessing the AAsset, so it is safe to
// close it.
AAsset_close(asset);
// Draw the pixels somewhere
// Free the pixels when done drawing with them
free(pixels);
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-21。
[null,null,["最后更新时间 (UTC):2025-08-21。"],[],[],null,["# Image decoder\n\nThe NDK [`ImageDecoder`](/ndk/reference/group/image-decoder) API provides a\nstandard API for Android C/C++ apps to decode images directly. App developers no\nlonger need to use the Java APIs (via JNI) or third-party image decoding\nlibraries. This API, along with encoding functions in the\n[Bitmap](/ndk/reference/group/bitmap) module, enables the following:\n\n- Native apps and libraries can be smaller because they no longer have to link their own decoding libraries.\n- Apps and libraries automatically benefit from platform security updates to decoding libraries.\n- Apps can decode images directly into memory they provide. Apps can then post-process the image data (if desired) and pass it to OpenGL or their drawing code.\n\nThis page describes how to use the API to decode an image.\n\nAvailability and capability\n---------------------------\n\nThe `ImageDecoder` API is available on apps that target Android 11 (API level 30)\nor higher. The implementation is inside the following files:\n\n- `imagedecoder.h` for the decoder\n- `bitmap.h` for the encoder\n- `libjnigraphics.so`\n\nThe API supports the following image formats:\n\n- JPEG\n- PNG\n- GIF\n- WebP\n- BMP\n\n- ICO\n\n- WBMP\n\n- HEIF\n\n- Digital negatives (via the DNG SDK)\n\nIn order to cover all usages of the decoded raw images, this API does not\nprovide higher level objects like those built on top of decoded images inside\nthe Java framework, such as:\n\n- [`Drawable` objects](/reference/android/graphics/drawable/Drawable).\n- [`NinePatch`](/reference/android/graphics/NinePatch): If present in an encoded image, NinePatch chunks are ignored.\n- [Bitmap density](/reference/android/graphics/Bitmap#getDensity()): `AImageDecoder` does not do any automatic size adjustment based on the screen's density, but it does allow decoding to a different size via [`AImageDecoder_setTargetSize()`](/ndk/reference/group/image-decoder#aimagedecoder_settargetsize).\n- Animations: Only decodes the first frame of an animated GIF or WebP file.\n\nDecode an image\n---------------\n\nDecoding starts with some form of input representing the encoded image.\n`AImageDecoder` accepts multiple types of input:\n\n- [`AAsset`](/ndk/reference/group/asset) (shown below)\n- File descriptor\n- Buffer\n\nThe following code shows how to open an image `Asset` from a file, decode it,\nand then properly dispose of the decoder and asset. To see an example of\nrendering the decoded image, see the\n[teapot sample](https://github.com/android/ndk-samples/tree/develop/teapots/image-decoder/src/main/cpp/Texture.cpp#30). \n\n AAssetManager* nativeManager = AAssetManager_fromJava(env, jAssets);\n const char* file = // Filename\n AAsset* asset = AAssetManager_open(nativeManager, file, AASSET_MODE_STREAMING);\n AImageDecoder* decoder;\n int result = AImageDecoder_createFromAAsset(asset, &decoder);\n if (result != ANDROID_IMAGE_DECODER_SUCCESS) {\n // An error occurred, and the file could not be decoded.\n }\n\n const AImageDecoderHeaderInfo* info = AImageDecoder_getHeaderInfo(decoder);\n int32_t width = AImageDecoderHeaderInfo_getWidth(info);\n int32_t height = AImageDecoderHeaderInfo_getHeight(info);\n AndroidBitmapFormat format =\n (AndroidBitmapFormat) AImageDecoderHeaderInfo_getAndroidBitmapFormat(info);\n size_t stride = AImageDecoder_getMinimumStride(decoder); // Image decoder does not\n // use padding by default\n size_t size = height * stride;\n void* pixels = malloc(size);\n\n result = AImageDecoder_decodeImage(decoder, pixels, stride, size);\n if (result != ANDROID_IMAGE_DECODER_SUCCESS) {\n // An error occurred, and the file could not be decoded.\n }\n\n // We're done with the decoder, so now it's safe to delete it.\n AImageDecoder_delete(decoder);\n\n // The decoder is no longer accessing the AAsset, so it is safe to\n // close it.\n AAsset_close(asset);\n\n // Draw the pixels somewhere\n\n // Free the pixels when done drawing with them\n free(pixels);"]]