رمزگشای تصویر
با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
NDK ImageDecoder
API یک API استاندارد برای برنامههای Android C/C++ برای رمزگشایی مستقیم تصاویر ارائه میکند. توسعه دهندگان برنامه دیگر نیازی به استفاده از API های جاوا (از طریق JNI) یا کتابخانه های رمزگشایی تصویر شخص ثالث ندارند. این API، همراه با توابع رمزگذاری در ماژول Bitmap ، موارد زیر را فعال می کند:
- برنامهها و کتابخانههای بومی میتوانند کوچکتر باشند، زیرا دیگر مجبور نیستند کتابخانههای رمزگشایی خود را پیوند دهند.
- برنامهها و کتابخانهها بهطور خودکار از بهروزرسانیهای امنیتی پلتفرم برای رمزگشایی کتابخانهها بهره میبرند.
- برنامه ها می توانند تصاویر را مستقیماً در حافظه ای که ارائه می کنند رمزگشایی کنند. سپس برنامه ها می توانند داده های تصویر را پس از پردازش (در صورت تمایل) و ارسال آن به OpenGL یا کد طراحی خود.
در این صفحه نحوه استفاده از API برای رمزگشایی یک تصویر توضیح داده شده است.
در دسترس بودن و قابلیت
ImageDecoder
API در برنامه هایی موجود است که اندروید 11 (سطح API 30) یا بالاتر را هدف قرار می دهند. پیاده سازی داخل فایل های زیر است:
-
imagedecoder.h
برای رمزگشا -
bitmap.h
برای رمزگذار -
libjnigraphics.so
API از فرمت های تصویر زیر پشتیبانی می کند:
به منظور پوشش همه موارد استفاده از تصاویر خام رمزگشایی شده، این API اشیاء سطح بالاتری مانند موارد ساخته شده در بالای تصاویر رمزگشایی شده در چارچوب جاوا را ارائه نمی دهد، مانند:
- اشیاء
Drawable
-
NinePatch
: اگر در یک تصویر رمزگذاری شده وجود داشته باشد، تکه های NinePatch نادیده گرفته می شوند. - تراکم بیت مپ :
AImageDecoder
هیچ گونه تنظیم خودکار اندازه را بر اساس چگالی صفحه انجام نمی دهد، اما امکان رمزگشایی به اندازه های مختلف را از طریق AImageDecoder_setTargetSize()
می دهد. - انیمیشن ها: فقط اولین فریم یک فایل متحرک GIF یا WebP را رمزگشایی می کند.
رمزگشایی یک تصویر
رمزگشایی با نوعی از ورودی که تصویر رمزگذاری شده را نشان می دهد شروع می شود. AImageDecoder
چندین نوع ورودی را می پذیرد:
-
AAsset
(در زیر نشان داده شده است) - توصیف کننده فایل
- بافر
کد زیر نشان می دهد که چگونه می توان یک Asset
تصویر را از یک فایل باز کرد، آن را رمزگشایی کرد و سپس رمزگشا و دارایی را به درستی از بین برد. برای مشاهده نمونه رندر تصویر رمزگشایی شده به نمونه قوری مراجعه کنید.
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 و/یا وابستههای آن هستند.
تاریخ آخرین بهروزرسانی 2025-08-27 بهوقت ساعت هماهنگ جهانی.
[null,null,["تاریخ آخرین بهروزرسانی 2025-08-27 بهوقت ساعت هماهنگ جهانی."],[],[],null,["The 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\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\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);"]]