int32_twidth,height,stride,index;uint8_t*image_pixel_buffer=nullptr;constexprconstcharkSampleImageName[]="default.jpg";boolload_image_result=util::LoadImageFromAssetManager(kSampleImageName,&width,&height,&stride,&image_pixel_buffer);uint8_t*grayscale_buffer=nullptr;util::ConvertRgbaToGrayscale(image_pixel_buffer,width,height,stride,&grayscale_buffer);int32_tgrayscale_stride=stride/4;constArStatusstatus=ArAugmentedImageDatabase_addImage(ar_session_,ar_augmented_image_database,kSampleImageName,grayscale_buffer,width,height,grayscale_stride,&index);// If the physical size of the image is known, you can instead use// ArStatus ArAugmentedImageDatabase_addImageWithPhysicalSize// This will improve the initial detection speed. ARCore will still actively// estimate the physical size of the image as it is viewed from multiple// viewpoints.delete[]image_pixel_buffer;delete[]grayscale_buffer;
// Update loop, in onDrawFrameArTrackableList*updated_image_list=nullptr;ArTrackableList_create(ar_session_,&updated_image_list);ArFrame_getUpdatedTrackables(ar_session_,ar_frame_,AR_TRACKABLE_AUGMENTED_IMAGE,updated_image_list);int32_timage_list_size;ArTrackableList_getSize(ar_session_,updated_image_list,&image_list_size);for(inti=0;i < image_list_size;++i){ArTrackable*ar_trackable=nullptr;ArTrackableList_acquireItem(ar_session_,updated_image_list,i,&ar_trackable);ArAugmentedImage*image=ArAsAugmentedImage(ar_trackable);ArTrackingStatetracking_state;ArTrackable_getTrackingState(ar_session_,ar_trackable,&tracking_state);intimage_index;ArAugmentedImage_getIndex(ar_session_,image,&image_index);if(tracking_state==AR_TRACKING_STATE_TRACKING){util::ScopedArPosescopedArPose(ar_session_);ArAugmentedImage_getCenterPose(ar_session_,image,scopedArPose.GetArPose());ArAnchor*image_anchor=nullptr;constArStatusstatus=ArTrackable_acquireNewAnchor(ar_session_,ar_trackable,scopedArPose.GetArPose(),&image_anchor);// For example, you can now render content at the image anchor, choosing// content based on the image index (or name).}}
[null,null,["最后更新时间 (UTC):2025-07-26。"],[[["\u003cp\u003eDevelop AR apps that recognize and track images using Augmented Images in ARCore.\u003c/p\u003e\n"],["\u003cp\u003eCreate and manage an image database, adding reference images for your app to detect.\u003c/p\u003e\n"],["\u003cp\u003eEnable image tracking in your ARCore session and poll for matched images during runtime.\u003c/p\u003e\n"],["\u003cp\u003eUnderstand how to handle different use cases, such as fixed and moving images, by considering tracking state and method.\u003c/p\u003e\n"],["\u003cp\u003eARCore might erroneously set tracking method to FULL_TRACKING for two similar images with rapid switching, potentially updating the anchor of the first to the second.\u003c/p\u003e\n"]]],[],null,["# Augmented Images Developer Guide for Android NDK\n\nLearn how to use Augmented Images in your own apps.\n\nPrerequisites\n-------------\n\nMake sure that you understand [fundamental AR concepts](/ar/develop/fundamentals)\nand how to [configure an ARCore session](/ar/develop/c/session-config) before proceeding.\n\nCreate an image database\n------------------------\n\nCreate an [`ArAugmentedImageDatabase`](/ar/reference/c/group/ar-augmented-image-database#araugmentedimagedatabase_1) to store reference images. There are two ways:\n\n- Create an empty database\n\n```c\nArAugmentedImageDatabase* ar_augmented_image_database = NULL;\nArAugmentedImageDatabase_create(ar_session, &ar_augmented_image_database);\n```\n\n- Read from an image database file. Refer to the \\[**AugmentedImage** C sample app\\] for [`util::LoadFileFromAssetManager`](https://github.com/google-ar/arcore-android-sdk/blob/master/samples/augmented_image_c/app/src/main/cpp/util.cc).\n\n```c\nstd::string database_buffer;\nutil::LoadFileFromAssetManager(asset_manager_, \"sample_database.imgdb\",\n &database_buffer);\nuint8_t* raw_buffer = reinterpret_cast\u003cuint8_t*\u003e(&database_buffer.front());\n\nArAugmentedImageDatabase* ar_augmented_image_database = NULL;\nconst ArStatus status = ArAugmentedImageDatabase_deserialize(\n ar_session, raw_buffer, database_buffer.size(),\n &ar_augmented_image_database);\n```\n\nDatabase files can be created either with the [Image Tool](/ar/develop/c/augmented-images/arcoreimg) or by calling [`ArAugmentedImageDatabase_serialize()`](/ar/reference/c/group/augmented-image-database#araugmentedimagedatabase_serialize).\n\nAdd images to database\n----------------------\n\nThis step is optional if your desired reference images have already been loaded from the database file. To add an image at runtime, call [`ArAugmentedImageDatabase_addImage()`](/ar/reference/c/group/augmented-image-database#araugmentedimagedatabase_addimage) as shown below. Refer to the [**augmented_image_c** sample app](https://github.com/google-ar/arcore-android-sdk/blob/master/samples/augmented_image_c) for [functions in the `util` namespace](https://github.com/google-ar/arcore-android-sdk/blob/master/samples/augmented_image_c/app/src/main/cpp/util.cc). \n\n```c\nint32_t width, height, stride, index;\nuint8_t* image_pixel_buffer = nullptr;\nconstexpr const char kSampleImageName[] = \"default.jpg\";\nbool load_image_result = util::LoadImageFromAssetManager(\n kSampleImageName, &width, &height, &stride, &image_pixel_buffer);\n\nuint8_t* grayscale_buffer = nullptr;\nutil::ConvertRgbaToGrayscale(image_pixel_buffer, width, height, stride,\n &grayscale_buffer);\n\nint32_t grayscale_stride = stride / 4;\nconst ArStatus status = ArAugmentedImageDatabase_addImage(\n ar_session_, ar_augmented_image_database, kSampleImageName,\n grayscale_buffer, width, height, grayscale_stride, &index);\n\n// If the physical size of the image is known, you can instead use\n// ArStatus ArAugmentedImageDatabase_addImageWithPhysicalSize\n// This will improve the initial detection speed. ARCore will still actively\n// estimate the physical size of the image as it is viewed from multiple\n// viewpoints.\n\ndelete[] image_pixel_buffer;\ndelete[] grayscale_buffer;\n```\n\nThe `index` and `name` values are used later to identify which reference image was detected.\n\nEnable image tracking\n---------------------\n\nConfigure your ARCore session to track images by registering the image database: \n\n```c\nArConfig_setAugmentedImageDatabase(ar_session_, ar_config,\n ar_augmented_image_database);\nconst ArStatus status = ArSession_configure(ar_session_, ar_config);\n```\n\nDuring the session, ARCore uses feature points from the camera image to match against those in the image database.\n\nFind augmented images in an AR session\n--------------------------------------\n\nTo get the matched images, poll for updated [`ArAugmentedImage`s](/ar/reference/c/group/augmented-image#araugmentedimage_1) in your frame update loop. \n\n```c\n// Update loop, in onDrawFrame\nArTrackableList* updated_image_list = nullptr;\nArTrackableList_create(ar_session_, &updated_image_list);\nArFrame_getUpdatedTrackables(\n ar_session_, ar_frame_, AR_TRACKABLE_AUGMENTED_IMAGE, updated_image_list);\n\nint32_t image_list_size;\nArTrackableList_getSize(ar_session_, updated_image_list, &image_list_size);\n\nfor (int i = 0; i \u003c image_list_size; ++i) {\n ArTrackable* ar_trackable = nullptr;\n ArTrackableList_acquireItem(ar_session_, updated_image_list, i,\n &ar_trackable);\n ArAugmentedImage* image = ArAsAugmentedImage(ar_trackable);\n\n ArTrackingState tracking_state;\n ArTrackable_getTrackingState(ar_session_, ar_trackable, &tracking_state);\n\n int image_index;\n ArAugmentedImage_getIndex(ar_session_, image, &image_index);\n\n if (tracking_state == AR_TRACKING_STATE_TRACKING) {\n util::ScopedArPose scopedArPose(ar_session_);\n ArAugmentedImage_getCenterPose(ar_session_, image,\n scopedArPose.GetArPose());\n\n ArAnchor* image_anchor = nullptr;\n const ArStatus status = ArTrackable_acquireNewAnchor(\n ar_session_, ar_trackable, scopedArPose.GetArPose(), &image_anchor);\n\n // For example, you can now render content at the image anchor, choosing\n // content based on the image index (or name).\n }\n}\n```\n\n### Supporting different use cases\n\nWhen ARCore detects an Augmented Image, it creates a [`Trackable`](/ar/reference/c/group/trackable) for that\nAugmented Image and sets [`ArTrackingState`](/ar/reference/c/group/shared-types#artrackingstate)\nto `TRACKING` and [`ArAugmentedImageTrackingMethod`](/ar/reference/c/group/augmented-image#araugmentedimagetrackingmethod) to `FULL_TRACKING`. When the tracked image\nmoves out of camera view, ARCore continues to set\n[`ArTrackingState`](/ar/reference/c/group/shared-types#artrackingstate) to `TRACKING`,\nbut changes the [`ArAugmentedImageTrackingMethod`](/ar/reference/c/group/augmented-image#araugmentedimagetrackingmethod)\nto `LAST_KNOWN_POSE` while continuing to provide the orientation and position of\nthe image.\n\nYour app should use the tracking state and tracking method differently depending on the intended use\ncase.\n\n- **Fixed images** . Most use cases involving images that are fixed in place\n (that is, not expected to move) can simply use `ArTrackingState` to determine\n whether the image has been detected and whether its location is known.\n `ArAugmentedImageTrackingMethod` can be ignored.\n\n- **Moving images** . If your app needs to track a moving image, use both\n `ArTrackingState` and `ArAugmentedImageTrackingMethod` to determine whether the image has been\n detected and whether its position is known.\n\n| Use case | Fixed image | Moving image |\n| Example | A poster hung on a wall | An advertisement on the side of a bus |\n| The pose can be considered valid when | `ArTrackingState == TRACKING` | `ArTrackingState == TRACKING` and `ArAugmentedImageTrackingMethod == FULL_TRACKING` |\n|---------------------------------------|-------------------------------|-------------------------------------------------------------------------------------|\n\n| **Note:** If an image in the camera view changes very rapidly to a second image that has roughly the same size and position, ARCore may erroneously set the [`ArAugmentedImageTrackingMethod`](/ar/reference/c/group/augmented-image#araugmentedimagetrackingmethod) to `FULL_TRACKING` for both images and also update the anchor of the first Augmented Image to the position of the new image. For example, this could happen if you were pointing the camera at an image in a web browser, and then switched to another browser tab containing a second similarly sized and positioned image."]]