The OCR service provides the capability of converting image information into character information. After printed materials such as bills, cards, forms, newspapers, and books are converted into image information through optical input methods like scanning or taking photos, OCR technologies are used to convert the image information into character information that can be processed by computers. This facilitates the extraction of characters, screen coordinates, and bounding boxes for users. Currently, this service can recognize simplified Chinese, English, Japanese, Korean, and traditional Chinese.
Start version: 4.0.0(10)
OCR configuration, which is used to specify whether text direction detection is supported.
System capability: SystemCapability.AI.OCR.TextRecognition
Start version: 4.0.0(10)
Name | Type | Read-Only | Optional | Description |
|---|---|---|---|---|
isDirectionDetectionSupported | boolean | No | Yes | Indicates whether text direction detection is supported.
The default value is true. If you can determine that the current image is correct, you can set this parameter to false to improve performance. |
Visual information to be recognized. Currently, only the visual information of the PixelMap type in color data format RGBA_8888 is supported.
System capability: SystemCapability.AI.OCR.TextRecognition
Start version: 4.0.0(10)
Name | Type | Read-Only | Optional | Description |
|---|---|---|---|---|
pixelMap | image.PixelMap | Yes | No | Image to be recognized. |
Pixel point position.
System capability: SystemCapability.AI.OCR.TextRecognition
Start version: 4.0.0(10)
Name | Type | Read-Only | Optional | Description |
|---|---|---|---|---|
x | number | Yes | No | Horizontal (X) coordinate of a pixel point. |
y | number | Yes | No | Vertical (Y) coordinate of a pixel point. |
Information about a word, including the content and the coordinates of its bounding box.
System capability: SystemCapability.AI.OCR.TextRecognition
Start version: 4.0.0(10)
Name | Type | Read-Only | Optional | Description |
|---|---|---|---|---|
value | string | Yes | No | Text content of a word. |
cornerPoints | Array<PixelPoint> | Yes | No | List of bounding box points of a word, which is returned clockwise. |
Information about a line of text.
System capability: SystemCapability.AI.OCR.TextRecognition
Start version: 4.0.0(10)
Name | Type | Read-Only | Optional | Description |
|---|---|---|---|---|
value | string | Yes | No | Text content of a line. |
cornerPoints | Array<PixelPoint> | Yes | No | List of bounding box points of a line, which is returned clockwise. |
words | Array<TextWord> | Yes | No | Words contained in a line. |
Information about a block of text.
System capability: SystemCapability.AI.OCR.TextRecognition
Start version: 4.0.0(10)
Name | Type | Read-Only | Optional | Description |
|---|---|---|---|---|
value | string | Yes | No | Text content of a block. |
lines | Array<TextLine> | Yes | No | Lines contained in a block. |
OCR result, including the text content and coordinate information.
System capability: SystemCapability.AI.OCR.TextRecognition
Start version: 4.0.0(10)
Name | Type | Read-Only | Optional | Description |
|---|---|---|---|---|
value | string | Yes | No | Recognized text content. |
blocks | Array<TextBlock> | Yes | No | Specific block information in the text content. |
recognizeText(visionInfo: VisionInfo, callback: AsyncCallback<TextRecognitionResult>): void
Text contained in recognized visual information. This API returns the result asynchronously through a callback.
System capability: SystemCapability.AI.OCR.TextRecognition
Start version: 4.0.0(10)
Parameters:
Name | Type | Mandatory | Description |
|---|---|---|---|
visionInfo | Yes | Visual information to be recognized. | |
callback | AsyncCallback<TextRecognitionResult> | Yes | Callback function, which returns the OCR object. |
Error codes:
For details about the error codes, please refer to ArkTS API Error Codes.
Error Code | Error Message |
|---|---|
200 | Run timed out, please try again later. |
401 | The parameter check failed. |
1001400001 | Failed to run, please try again. |
1001400002 | The service is abnormal. |
Example:
import { textRecognition } from '@kit.CoreVisionKit'
import { image } from '@kit.ImageKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { picker, fileIo } from '@kit.CoreFileKit';
let imageSource: image.ImageSource | undefined = undefined;
let chooseImage: PixelMap | undefined = undefined;
// Convert the image into PixelMap. The image can be obtained from the gallery.
let photoPicker = new picker.PhotoViewPicker();
photoPicker.select({
MIMEType: picker.PhotoViewMIMETypes.IMAGE_TYPE, maxSelectNumber: 1
}).then((res: picker.PhotoSelectResult) => {
let uri = res.photoUris[0];
if (uri === undefined) {
hilog.error(0x0000, 'OCRDemo', "Failed to get uri.");
return;
}
setTimeout(async () => {
let fileSource = await fileIo.open(uri, fileIo.OpenMode.READ_ONLY);
imageSource = image.createImageSource(fileSource.fd);
chooseImage = await imageSource.createPixelMap();
hilog.info(0x0000, 'OCRDemo', `chooseImage: ${chooseImage.toString()}`);
if (!chooseImage) {
return;
}
// Call the OCR API.
let visionInfo: textRecognition.VisionInfo = {
pixelMap: chooseImage
};
textRecognition.recognizeText(visionInfo, (error: BusinessError, data: textRecognition.TextRecognitionResult) => {
if (error.code !== 0) {
hilog.error(0x0000, 'OCRDemo', `Failed to recognize text. Code: ${error.code}, message: ${error.message}`);
return;
}
// The recognition is successful. Obtain the corresponding result.
let recognitionString = data.toString();
hilog.info(0x0000, 'OCRDemo', `Succeeded in recognizing text: ${recognitionString}`);
if(chooseImage && imageSource) {
chooseImage.release();
imageSource.release();
}
});
}, 100)
}).catch((err: BusinessError) => {
hilog.error(0x0000, 'OCRDemo', `Failed to get photo image uri. Code: ${err.code}, message: ${err.message}`);
}) recognizeText(visionInfo: VisionInfo, configuration?: TextRecognitionConfiguration): Promise<TextRecognitionResult>
Text contained in recognized visual information. This API returns the result asynchronously through a promise.
System capability: SystemCapability.AI.OCR.TextRecognition
Start version: 4.0.0(10)
Parameters:
Name | Type | Optional | Description |
|---|---|---|---|
visionInfo | No | Visual information to be recognized. | |
configuration | Yes | Recognition configuration. |
Returns:
Type | Description |
|---|---|
Promise<TextRecognitionResult> | OCR result object returned through a promise. |
Error codes:
For details about the error codes, please refer to ArkTS API Error Codes.
Error Code | Error Message |
|---|---|
200 | Run timed out, please try again later. |
401 | The parameter check failed. |
1001400001 | Failed to run, please try again. |
1001400002 | The service is abnormal. |
Example:
import { textRecognition } from '@kit.CoreVisionKit'
import { image } from '@kit.ImageKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { picker, fileIo } from '@kit.CoreFileKit';
let imageSource: image.ImageSource | undefined = undefined;
let chooseImage: PixelMap | undefined = undefined;
// Convert the image into PixelMap. The image can be obtained from the gallery.
let photoPicker = new picker.PhotoViewPicker();
photoPicker.select({
MIMEType: picker.PhotoViewMIMETypes.IMAGE_TYPE, maxSelectNumber: 1
}).then((res: picker.PhotoSelectResult) => {
let uri = res.photoUris[0];
if (uri === undefined) {
hilog.error(0x0000, 'OCRDemo', "Failed to get uri.");
return;
}
setTimeout(async () => {
let fileSource = await fileIo.open(uri, fileIo.OpenMode.READ_ONLY);
imageSource = image.createImageSource(fileSource.fd);
chooseImage = await imageSource.createPixelMap();
hilog.info(0x0000, 'OCRDemo', `chooseImage: ${chooseImage.toString()}`);
if (!chooseImage) {
return;
}
// Call the OCR API.
let visionInfo: textRecognition.VisionInfo = {
pixelMap: chooseImage
};
let recognitionResult = await textRecognition.recognizeText(visionInfo);
// The recognition is successful. Obtain the corresponding result.
let recognitionString = recognitionResult.toString();
hilog.info(0x0000, 'OCRDemo', `Succeeded in recognizing text: ${recognitionString}`);
if(chooseImage && imageSource) {
chooseImage.release();
imageSource.release();
}
}, 100)
}).catch((err: BusinessError) => {
hilog.error(0x0000, 'OCRDemo', `Failed to get photo image uri. Code: ${err.code}, message: ${err.message}`);
}) recognizeText(visionInfo: VisionInfo, configuration: TextRecognitionConfiguration, callback: AsyncCallback<TextRecognitionResult>): void
Text contained in recognized visual information. This API returns the result asynchronously through a callback.
System capability: SystemCapability.AI.OCR.TextRecognition
Start version: 4.0.0(10)
Parameters:
Name | Type | Optional | Description |
|---|---|---|---|
visionInfo | No | Visual information to be recognized. | |
configuration | No | Recognition configuration. | |
callback | AsyncCallback<TextRecognitionResult> | No | Callback of the recognition result, which can be used for UI display or interaction. |
Error codes:
For details about the error codes, please refer to ArkTS API Error Codes.
Error Code | Error Message |
|---|---|
200 | Run timed out, please try again later. |
401 | The parameter check failed. |
1001400001 | Failed to run, please try again. |
1001400002 | The service is abnormal. |
import { textRecognition } from '@kit.CoreVisionKit'
import { image } from '@kit.ImageKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { picker, fileIo } from '@kit.CoreFileKit';
let imageSource: image.ImageSource | undefined = undefined;
let chooseImage: PixelMap | undefined = undefined;
// Convert the image into PixelMap. The image can be obtained from the gallery.
let photoPicker = new picker.PhotoViewPicker();
photoPicker.select({
MIMEType: picker.PhotoViewMIMETypes.IMAGE_TYPE, maxSelectNumber: 1
}).then((res: picker.PhotoSelectResult) => {
let uri = res.photoUris[0];
if (uri === undefined) {
hilog.error(0x0000, 'OCRDemo', "Failed to get uri.");
return;
}
setTimeout(async () => {
let fileSource = await fileIo.open(uri, fileIo.OpenMode.READ_ONLY);
imageSource = image.createImageSource(fileSource.fd);
chooseImage = await imageSource.createPixelMap();
hilog.info(0x0000, 'OCRDemo', `chooseImage: ${chooseImage.toString()}`);
if (!chooseImage) {
return;
}
// Call the OCR API.
let visionInfo: textRecognition.VisionInfo = {
pixelMap: chooseImage
};
let textConfiguration: textRecognition.TextRecognitionConfiguration = {
isDirectionDetectionSupported: false
};
textRecognition.recognizeText(visionInfo, textConfiguration, (error: BusinessError, data: textRecognition.TextRecognitionResult) => {
if (error.code !== 0) {
hilog.error(0x0000, 'OCRDemo', `Failed to recognize text. Code: ${error.code}, message: ${error.message}`);
return;
}
// The recognition is successful. Obtain the corresponding result.
let recognitionString = data.toString();
hilog.info(0x0000, 'OCRDemo', `Succeeded in recognizing text: ${recognitionString}`);
if(chooseImage && imageSource) {
chooseImage.release();
imageSource.release();
}
});
}, 100)
}).catch((err: BusinessError) => {
hilog.error(0x0000, 'OCRDemo', `Failed to get photo image uri. Code: ${err.code}, message: ${err.message}`);
}) getSupportedLanguages(): Promise<Array<string>>
Obtains the list of languages supported by OCR. This API returns the result asynchronously through a promise.
System capability: SystemCapability.AI.OCR.TextRecognition
Start version: 4.0.0(10)
Returns:
Type | Description |
|---|---|
Promise<Array<string>> | List of supported languages, which is returned through a promise. |
Error codes:
For details about the error codes, please refer to ArkTS API Error Codes.
Error Code | Error Message |
|---|---|
1001400001 | Failed to run, please try again. |
1001400002 | The service is abnormal. |
Example:
import { textRecognition } from '@kit.CoreVisionKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
textRecognition.getSupportedLanguages().then((data: Array<string>) => {
let languageString = data.join(', ');
hilog.info(0x0000, 'OCRDemo', `Succeeded in recognizing text: ${languageString}`);
}, (err: BusinessError) => {
hilog.error(0x0000, 'OCRDemo', `Failed to obtain the language. Code: ${err.code}, message: ${err.message}`);
}); getSupportedLanguages(callback: AsyncCallback<Array<string>>): void
Obtains the list of languages supported by OCR. This API returns the result asynchronously through a callback.
System capability: SystemCapability.AI.OCR.TextRecognition
Start version: 4.0.0(10)
Parameters:
Name | Type | Optional | Description |
|---|---|---|---|
callback | AsyncCallback<Array<string>> | No |
Error codes:
For details about the error codes, please refer to ArkTS API Error Codes.
Error Code | Error Message |
|---|---|
1001400001 | Failed to run, please try again. |
1001400002 | The service is abnormal. |
Example:
import { textRecognition } from '@kit.CoreVisionKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
textRecognition.getSupportedLanguages().then((data: Array<string>) => {
let languageString = data.join(', ');
hilog.info(0x0000, 'OCRDemo', `Succeeded in recognizing text: ${languageString}`);
}, (err: BusinessError) => {
hilog.error(0x0000, 'OCRDemo', `Failed to obtain the language. Code: ${err.code}, message: ${err.message}`);
});