# XComponent **XComponent** provides a surface for graphics rendering and media data input into your view. You can customize the position and size of the surface as needed. For details, see [Native XComponent](../../../ui/napi-xcomponent-guidelines.md). > **NOTE** > > This component is supported since API version 8. Updates will be marked with a superscript to indicate their earliest API version. ## Child Components Not supported ## APIs ### XComponent12+ XComponent(options: XComponentOptions) **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.ArkUI.ArkUI.Full **Parameters** | Name | Type | Mandatory| Description | | ------- | --------------------------------------- | ---- | ------------------------------ | | options | [XComponentOptions](#xcomponentoptions12) | Yes | Options of the **XComponent**.| ### XComponent10+ XComponent(value: {id: string, type: XComponentType, libraryname?: string, controller?: XComponentController}) This API is deprecated since API version 12. You are advised to use [XComponent(options: XComponentOptions)](#xcomponent12) instead. **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.ArkUI.ArkUI.Full **Parameters** | Name | Type | Mandatory| Description | | ----------- | --------------------------------------------- | ---- | ------------------------------------------------------------ | | id | string | Yes | Unique ID of the component. The value can contain a maximum of 128 characters. | | type | [XComponentType](ts-appendix-enums.md#xcomponenttype10) | Yes | Type of the component. | | libraryname | string | No | Name of the dynamic library compiled and output by the native layer (the corresponding dynamic library does not support cross-module loading). This parameter is effective only when **type** is **SURFACE** or **TEXTURE**.| | controller | [XComponentController](#xcomponentcontroller) | No | Controller bound to the component, which can be used to invoke methods of the component. This parameter is effective only when **type** is **SURFACE** or **TEXTURE**.| ### XComponent(deprecated) XComponent(value: {id: string, type: string, libraryname?: string, controller?: XComponentController}) > **NOTE** > > This API is deprecated since API version 12. You are advised to use [XComponent(options: XComponentOptions)](#xcomponent12) instead. **System capability**: SystemCapability.ArkUI.ArkUI.Full **Parameters** | Name | Type | Mandatory| Description | | ----------- | --------------------------------------------- | ---- | ------------------------------------------------------------ | | id | string | Yes | Unique ID of the component. The value can contain a maximum of 128 characters. | | type | string | Yes | Type of the component. The options are as follows:
- **"surface"**: The custom content is displayed individually on the screen. This option is used for displaying EGL/OpenGL ES and media data.
- **"component"**9+: The component acts a container where non-UI logic can be executed to dynamically load and display content.
Any other value is handled as **"surface"**.| | libraryname | string | No | Name of the dynamic library compiled and output by the native layer (the corresponding dynamic library does not support cross-module loading). This parameter is effective only when **type** is **"surface"**.| | controller | [XComponentcontroller](#xcomponentcontroller) | No | Controller bound to the component, which can be used to invoke methods of the component. This parameter is valid only when the component type is **"surface"**.| ## XComponentOptions12+ Defines the options of the **XComponent**. **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.ArkUI.ArkUI.Full | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | type | [XComponentType](ts-appendix-enums.md#xcomponenttype10) | Yes | Type of the component.| | controller | [XComponentController](#xcomponentcontroller) | Yes| Controller bound to the component, which can be used to invoke methods of the component. This parameter is valid only when **type** is **SURFACE** or **TEXTURE**.| | imageAIOptions | [ImageAIOptions](ts-image-common.md#imageaioptions) | No| AI image analysis options. You can configure the analysis type or bind an analyzer controller through this parameter.| ## Attributes In addition to universal attributes, the following attributes are supported. > > **NOTE** > > The **foregroundColor**, **obscured**, and **pixelStretchEffect** attributes are not supported. When **type** is set to **SURFACE**, the following are not supported either: attribute modifier, custom drawing, background options (except **backgroundColor**), image effects (except **shadow**), **maskShape**, and **foregroundEffect** attributes. > > For the **XComponent** component of the TEXTURE or SURFACE type, if the [renderFit](./ts-universal-attributes-renderfit.md) attribute is not set, it defaults to **RenderFit.RESIZE_FILL**. > > For the **XComponent** component of the SURFACE type with an opaque black background, the [renderFit](./ts-universal-attributes-renderfit.md) attribute can only be set to **RenderFit.RESIZE_FILL**. Other **RenderFit** enum values are not recommended. > > For the **XComponent** component created using the [ArkUI NDK API](../../../ui/ndk-access-the-arkts-page.md), the [getAttribute](../_ark_u_i___native_node_a_p_i__1.md#getattribute) function is not supported for obtaining the **renderFit** attribute value. ### enableAnalyzer12+ enableAnalyzer(enable: boolean) Sets whether to enable the AI image analyzer, which supports subject recognition, text recognition, and object lookup. For the settings to take effect, this attribute must be used together with [StartImageAnalyzer](#startimageanalyzer12) and [StopImageAnalyzer](#stopimageanalyzer12) of **XComponentController**. This feature cannot be used together with the [overlay](ts-universal-attributes-overlay.md) attribute. If both are set, the **CustomBuilder** attribute in **overlay** has no effect. This feature also depends on device capabilities. **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.ArkUI.ArkUI.Full **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | enable | boolean | Yes| Whether to enable the AI image analyzer.| > **NOTE** > > This API has effect only when **type** is set to **SURFACE** or **TEXTURE**. ### enableSecure13+ enableSecure(isSecure: boolean) Sets whether to enable the secure surface to protect the content rendered within the component from being captured or recorded. **Atomic service API**: This API can be used in atomic services since API version 13. **System capability**: SystemCapability.ArkUI.ArkUI.Full **Parameters** | Name | Type | Mandatory| Description | | -------- | ------- | ---- | ---------------------- | | isSecure | boolean | Yes | Whether to enable the secure surface.| > **NOTE** > > This attribute is effective only when **type** is set to **SURFACE**. > > The **XComponent** created using the[ArkUI NDK APIs](../../../ui/ndk-build-ui-overview.md) is not supported. ## Events Since API version 12, the [universal events](ts-universal-events-click.md) are supported when **type** is set to **SURFACE** or **TEXTURE**. > **NOTE** > > When the **libraryname** parameter is set, [click events](ts-universal-events-click.md), [touch events](ts-universal-events-touch.md), [show/hide events](ts-universal-events-show-hide.md), [key events](ts-universal-events-key.md), [focus events](ts-universal-focus-event.md), and [mouse events](ts-universal-mouse-key.md) only respond to event APIs on the C-API side. **Atomic service API**: This API can be used in atomic services since API version 12. The following events are effective only when **type** is set to **SURFACE** or **TEXTURE**. ### onLoad onLoad(callback: (event?: object) => void ) Triggered when the plug-in is loaded. **Atomic service API**: This API can be used in atomic services since API version 14. **System capability**: SystemCapability.ArkUI.ArkUI.Full **Parameters** | Name | Type | Mandatory | Description | | ----- | ------ | ---- | ---------------------------------------- | | event | object | No | Context of the **XComponent** object. The APIs contained in the context are defined at the native layer by developers.| ### onDestroy onDestroy(event: VoidCallback ) Triggered when the plug-in is destroyed. **Atomic service API**: This API can be used in atomic services since API version 14. **System capability**: SystemCapability.ArkUI.ArkUI.Full **Parameters** | Name | Type | Mandatory | Description | | ----- | ------ | ---- | ---------------------------------------- | | event | [VoidCallback](ts-types.md#voidcallback12) | Yes | Callback after the **XComponent** is destroyed.| ## XComponentController Defines the controller of the **XComponent**. You can bind the controller to the **XComponent** to call the component APIs through the controller. **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.ArkUI.ArkUI.Full ### Creating an Object ``` xcomponentController: XComponentController = new XComponentController() ``` **Atomic service API**: This API can be used in atomic services since API version 12. ### constructor constructor() A constructor used to create a **XComponentController** instance. **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.ArkUI.ArkUI.Full ### getXComponentSurfaceId9+ getXComponentSurfaceId(): string Obtains the ID of the surface held by the **XComponent**, which can then be used for @ohos APIs. For details, see [Camera Management](../../apis-camera-kit/js-apis-camera.md). This API works only when **type** of the **XComponent** is set to **SURFACE("surface")** or **TEXTURE**. **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.ArkUI.ArkUI.Full **Return value** | Type | Description | | ------ | ----------------------- | | string | ID of the surface held by the **XComponent**.| ### setXComponentSurfaceSize(deprecated) setXComponentSurfaceSize(value: {surfaceWidth: number, surfaceHeight: number}): void Sets the width and height of the surface held by the **XComponent**. This API works only when **type** of the **XComponent** is set to **SURFACE("surface")** or **TEXTURE**. This API is deprecated since API version 12. You are advised to use [setXComponentSurfaceRect](#setxcomponentsurfacerect12) instead. **System capability**: SystemCapability.ArkUI.ArkUI.Full **Parameters** | Name | Type | Mandatory | Description | | ------------- | ------ | ---- | ----------------------- | | surfaceWidth | number | Yes | Width of the surface held by the **XComponent**.| | surfaceHeight | number | Yes | Height of the surface held by the **XComponent**.| ### getXComponentContext getXComponentContext(): Object Obtains the context of an **XComponent** object. This API works only when **type** of the **XComponent** is set to **SURFACE("surface")** or **TEXTURE**. **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.ArkUI.ArkUI.Full **Return value** | Type | Description | | ------ | ------------------------------------------------------------ | | Object | Context of the **XComponent** object. The APIs contained in the context are defined by developers. The context is passed in as the first parameter of the **onLoad** callback.| ### setXComponentSurfaceRect12+ setXComponentSurfaceRect(rect: SurfaceRect): void Sets the rectangle for the surface held by the **XComponent**. This API works only when type of the **XComponent** is set to **SURFACE("surface")** or **TEXTURE**. **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.ArkUI.ArkUI.Full **Parameters** | Name| Type | Mandatory| Description | | ------ | ------------------------------------ | ---- | --------------------------------- | | rect | [SurfaceRect](#surfacerect12) | Yes | Rectangle of the surface held by the **XComponent**.| > **NOTE** > > If **offsetX** or **offsetY** in the **rect** parameter is not set, the surface rectangle is centered along the x-axis or y-axis relative to the upper left corner of the **XComponent**. > > If **surfaceWidth** and **surfaceHeight** in the **rect** parameter are set to **0** or a negative value, this API has no effect. > > This API has a higher priority than attributes that can change the content offset and size, such as [border](ts-universal-attributes-border.md#border) and [padding](ts-universal-attributes-size.md#padding). ### getXComponentSurfaceRect12+ getXComponentSurfaceRect(): SurfaceRect Obtains the rectangle of the surface held by the **XComponent**. This API works only when type of the **XComponent** is set to **SURFACE("surface")** or **TEXTURE**. **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.ArkUI.ArkUI.Full **Return value** | Type | Description | | ------------------------------------ | ------------------------------------- | | [SurfaceRect](#surfacerect12) | Rectangle of the surface held by the **XComponent**.| ### onSurfaceCreated12+ onSurfaceCreated(surfaceId: string): void Triggered when the surface held by the **XComponent** is created. This API works only when **type** of the **XComponent** is set to **SURFACE("surface")** or **TEXTURE**. **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.ArkUI.ArkUI.Full **Parameters** | Name | Type| Mandatory| Description | | --------- | -------- | ---- | ------------------------------------------------- | | surfaceId | string | Yes | ID of the surface held by the **XComponent**.| > **NOTE** > > The callback is triggered only when the **libraryname** parameter is not set for the **XComponent**. ### onSurfaceChanged12+ onSurfaceChanged(surfaceId: string, rect: SurfaceRect): void Triggered when the surface held by the **XComponent** has its size changed (including the time when the **XComponent** is created with the specified size). This API works only when **type** of the **XComponent** is set to **SURFACE** (**"surface"**) or **TEXTURE**. **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.ArkUI.ArkUI.Full **Parameters** | Name | Type | Mandatory| Description | | --------- | ------------------------------------- | ---- | ------------------------------------------------------- | | surfaceId | string | Yes | ID of the surface held by the **XComponent**. | | rect | [SurfaceRect](#surfacerect12) | Yes | Rectangle for displaying the surface held by the **XComponent**.| > **NOTE** > > The callback is triggered only when the **libraryname** parameter is not set for the **XComponent**. ### onSurfaceDestroyed12+ onSurfaceDestroyed(surfaceId: string): void Triggered when the surface held by the **XComponent** is destroyed. This API works only when **type** of the **XComponent** is set to **SURFACE** (**"surface"**) or **TEXTURE**. **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.ArkUI.ArkUI.Full **Parameters** | Name | Type| Mandatory| Description | | --------- | -------- | ---- | ------------------------------------------------- | | surfaceId | string | Yes | ID of the surface held by the **XComponent**.| > **NOTE** > > The callback is triggered only when the **libraryname** parameter is not set for the **XComponent**. ### startImageAnalyzer12+ startImageAnalyzer(config: ImageAnalyzerConfig): Promise\ Starts AI image analysis in the given settings. Before calling this API, make sure the AI image analyzer is [enabled](#enableanalyzer12).
Because the image frame used for analysis is the one captured when this API is called, pay attention to the invoking time of this API.
If this API is repeatedly called before the execution is complete, an error callback is triggered. > **NOTE** > > The image analysis type cannot be dynamically modified. > > This API depends on device capabilities. If it is called on an incompatible device, an error code is returned. **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.ArkUI.ArkUI.Full **Parameters** | Name| Type | Mandatory| Description | | ------ | --------- | ---- | ---------------------------------------------------------------------- | | config | [ImageAnalyzerConfig](ts-image-common.md#imageanalyzerconfig) | Yes | Settings of the AI image analyzer.| **Return value** | Type | Description | | ----------------- | ------------------------------------ | | Promise\ | Promise used to return the result.| **Error codes** For details about the error codes, see [AI Image Analyzer Error Codes](../errorcode-image-analyzer.md). | ID| Error Message | | -------- | -------------------------------------------- | | 110001 | Image analysis feature is unsupported. | | 110002 | Image analysis is currently being executed. | | 110003 | Image analysis is stopped. | ### stopImageAnalyzer12+ stopImageAnalyzer(): void Stops AI image analysis. The content displayed by the AI image analyzer will be destroyed. > **NOTE** > > If this API is called when the **startImageAnalyzer** API has not yet returned any result, an error callback is triggered. > > This feature depends on device capabilities. **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.ArkUI.ArkUI.Full ### setXComponentSurfaceRotation12+ setXComponentSurfaceRotation(rotationOptions: SurfaceRotationOptions): void Sets whether to lock the orientation of the surface held by this **XComponent** when the screen rotates. This API is effective only when the **XComponent** type is **SURFACE** (**"surface"**). **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.ArkUI.ArkUI.Full **Parameters** | Name| Type | Mandatory| Description | | ------ | ------------------------------------ | ---- | --------------------------------- | | rotationOptions | [SurfaceRotationOptions](#surfacerotationoptions12) | Yes| Whether to lock the orientation of the surface held by the current **XComponent** when the screen rotates.| > **NOTE** > > If **rotationOptions** is not set, the surface held by this **XComponent** rotates with the screen by default. > > The orientation lock is only applied during the rotation process and is released once the rotation is complete. > > The setting takes effect only when the screen is rotated by 90°, that is, when it switches between landscape and portrait modes. > > Make sure the Buffer's width and height remain constant after locking the orientation to prevent distortion. ### getXComponentSurfaceRotation12+ getXComponentSurfaceRotation(): Required\ Obtains whether the orientation of the surface held by this **XComponent** is locked when the screen rotates. This API is effective only when the **XComponent** type is **SURFACE** (**"surface"**). **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.ArkUI.ArkUI.Full **Return value** | Type | Description | | ------------------------------------ | ------------------------------------- | | [SurfaceRotationOptions](#surfacerotationoptions12) | Whether the orientation of the surface held by the current **XComponent** is locked when the screen rotates.| ## SurfaceRotationOptions12+ Defines whether the orientation of the surface held by the current **XComponent** is locked when the screen rotates. **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.ArkUI.ArkUI.Full | Name | Type | Mandatory| Description | | ------------- | ------ | ---- | ------------------------------------------------------------ | | lock | boolean | No | Whether the orientation of the surface is locked when the screen rotates. If this parameter is not set, the default value **false** is used, indicating that the orientation is not locked.| ## SurfaceRect12+ Describes the rectangle of the surface held by the **XComponent**. **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.ArkUI.ArkUI.Full | Name | Type | Mandatory| Description | | ------------- | ------ | ---- | ------------------------------------------------------------ | | offsetX | number | No | X coordinate of the surface rectangle relative to the upper left corner of the **XComponent**.
Unit: px| | offsetY | number | No | Y coordinate of the surface rectangle relative to the upper left corner of the **XComponent**.
Unit: px| | surfaceWidth | number | Yes | Width of the surface rectangle.
Unit: px | | surfaceHeight | number | Yes | Height of the surface rectangle.
Unit: px | > **NOTE** > > The **surfaceWidth** and **surfaceHeight** attributes default to the size of the **XComponent** if the [setXComponentSurfaceRect](ts-basic-components-xcomponent.md#setxcomponentsurfacerect12) API is not called and neither [border](ts-universal-attributes-border.md#border) nor [padding](ts-universal-attributes-size.md#padding) is set. > > Make sure the values of **surfaceWidth** and **surfaceHeight** do not exceed 8192 px. Exceeding this limit may lead to rendering issues. ## Example You can preview how this component looks on a real device, but not in DevEco Studio Previewer. ### Example 1: Enabling AI Image Analyzer This example shows how to use the **enableAnalyzer** attribute to enable AI image analyzer. You can use **XComponentController** to start or stop AI analysis on images. ```ts // xxx.ets import { BusinessError } from '@kit.BasicServicesKit'; import nativeRender from 'libnativerender.so'; class CustomXComponentController extends XComponentController { onSurfaceCreated(surfaceId: string): void { console.log(`onSurfaceCreated surfaceId: ${surfaceId}`); nativeRender.SetSurfaceId(BigInt(surfaceId)); } onSurfaceChanged(surfaceId: string, rect: SurfaceRect): void { console.log(`onSurfaceChanged surfaceId: ${surfaceId}, rect: ${JSON.stringify(rect)}}`); nativeRender.ChangeSurface(BigInt(surfaceId), rect.surfaceWidth, rect.surfaceHeight); } onSurfaceDestroyed(surfaceId: string): void { console.log(`onSurfaceDestroyed surfaceId: ${surfaceId}`); nativeRender.DestroySurface(BigInt(surfaceId)); } } @Entry @Component struct XComponentExample { xComponentController: XComponentController = new CustomXComponentController(); private config: ImageAnalyzerConfig = { types: [ImageAnalyzerType.SUBJECT, ImageAnalyzerType.TEXT] }; private aiController: ImageAnalyzerController = new ImageAnalyzerController(); private options: ImageAIOptions = { types: [ImageAnalyzerType.SUBJECT, ImageAnalyzerType.TEXT], aiController: this.aiController }; @State xcWidth: string = "320px"; @State xcHeight: string = "480px"; @State currentStatus: string = "index"; build() { Column({ space: 5 }) { Button("change size") .onClick(() => { this.xcWidth = "640px"; this.xcHeight = "720px"; }) Button('start AI analyze') .onClick(() => { this.xComponentController.startImageAnalyzer(this.config) .then(() => { console.log("analysis complete"); }) .catch((error: BusinessError) => { console.log("error code: " + error.code); }) }) Button('stop AI analyze') .onClick(() => { this.xComponentController.stopImageAnalyzer(); }) Button('get analyzer types') .onClick(() => { this.aiController.getImageAnalyzerSupportTypes(); }) Button('Draw Star') .fontSize('16fp') .fontWeight(500) .margin({ bottom: 24 }) .onClick(() => { let surfaceId = this.xComponentController.getXComponentSurfaceId(); this.xComponentController.getXComponentSurfaceRect(); nativeRender.DrawPattern(BigInt(surfaceId)); let hasDraw: boolean = false; if (nativeRender.GetXComponentStatus(BigInt(surfaceId))) { hasDraw = nativeRender.GetXComponentStatus(BigInt(surfaceId)).hasDraw; } if (hasDraw) { this.currentStatus = "draw star"; } }) XComponent({ type: XComponentType.SURFACE, controller: this.xComponentController, imageAIOptions: this.options }) .width(this.xcWidth) .height(this.xcHeight) .enableAnalyzer(true) Text(this.currentStatus) .fontSize('24fp') .fontWeight(500) } .width("100%") } } ``` ### Example 2: Locking the Surface Orientation During Screen Rotation This example shows how to use **setXComponentSurfaceRotation** to lock the surface orientation during screen rotation so that the surface does not rotate with the screen. ```ts // xxx.ets @Entry @Component struct Index{ @State isLock: boolean = true; @State xc_width: number = 500; @State xc_height: number = 700; myXComponentController: XComponentController = new XComponentController(); build() { Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Start }) { XComponent({ id: 'xComponentId', type: XComponentType.SURFACE, libraryname: 'nativerender', controller: this.myXComponentController }) .width(this.xc_width) .height(this.xc_height) .onLoad(() => { let surfaceRotation: SurfaceRotationOptions = { lock: this.isLock }; this.myXComponentController.setXComponentSurfaceRotation(surfaceRotation); console.log("Surface getXComponentSurfaceRotation lock = " + this.myXComponentController.getXComponentSurfaceRotation().lock); }) } .width('100%') .height('100%') } } ```