이미지 디코더
컬렉션을 사용해 정리하기
내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요.
NDK ImageDecoder
API는 Android C/C++ 앱이 이미지를 직접 디코딩하도록 표준 API를 제공합니다. 앱 개발자는 자바 API(JNI를 통해) 또는 타사 이미지 디코딩 라이브러리를 더 이상 사용할 필요가 없습니다. 이 API는 비트맵 모듈의 인코딩 함수와 함께 다음을 가능하게 합니다.
- 네이티브 앱 및 라이브러리는 자체 디코딩 라이브러리를 더 이상 연결할 필요가 없으므로 크기를 줄일 수 있습니다.
- 앱 및 라이브러리는 플랫폼 보안 업데이트에서 디코딩 라이브러리에 이르기까지 이러한 기능의 이점을 자동으로 활용합니다.
- 앱은 제공하는 메모리로 이미지를 직접 디코딩할 수 있습니다. 그런 다음 앱은 원한다면 이미지 데이터를 후처리하여 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는 다음과 같이 자바 프레임워크 내의 디코딩된 이미지를 기반으로 빌드된 객체와 같은 상위 수준 객체를 제공하지 않습니다.
이미지 디코딩
디코딩은 인코딩된 이미지를 나타내는 몇 가지 입력 형식으로 시작됩니다.
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);
이 페이지에 나와 있는 콘텐츠와 코드 샘플에는 콘텐츠 라이선스에서 설명하는 라이선스가 적용됩니다. 자바 및 OpenJDK는 Oracle 및 Oracle 계열사의 상표 또는 등록 상표입니다.
최종 업데이트: 2025-08-21(UTC)
[null,null,["최종 업데이트: 2025-08-21(UTC)"],[],[],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);"]]