目录

class info
com.huawei.hms.mlsdk.text.MLTextAnalyzer

Represents the text analyzer, which detects text (MLText) in an image. There are two types of text analyzers. 

One is the on-device analyzer, which runs a detection model on the device and returns the detection result. 

The other is the on-cloud analyzer, which runs a detection model on the cloud after the cloud API is called.

An on-device text analyzer can be created using MLAnalyzerFactory.getInstance().getLocalTextAnalyzer() or MLAnalyzerFactory.getInstance().getLocalTextAnalyzer(MLLocalTextSetting settings). The former uses the default configuration, and the latter uses the customized configuration. 

An on-cloud text analyzer can be created using MLAnalyzerFactory.getInstance().getRemoteTextAnalyzer() or MLAnalyzerFactory.getInstance().getRemoteTextAnalyzer(MLRemoteTextSetting settings). The former uses the default configuration, and the latter uses the customized configuration.

Sample Code

  1. // Example of on-device text detection.
  2. MLTextAnalyzer analyzer = MLAnalyzerFactory.getInstance().getLocalTextAnalyzer();
  3. //Create an MLFraMLFramesing the bitmap.
  4. MLFrame frame = MLFrame.fromBitmap(bitmap);
  5. // Detect text in an image.
  6. Task<MLText> task = analyzer.asyncAnalyseFrame(frame);
  7. task.addOnSuccessListener(new OnSuccessListener<MLText>() {
  8.    @Override
  9.    public void onSuccess(MLText text) {
  10.     // Detection success.
  11.    }
  12. }).addOnFailureListener(new OnFailureListener() {
  13.    @Override
  14.    public void onFailure(Exception e) {
  15.     // Detection failure.
  16.    }
  17. });
  1. // Example of on-cloud text detection.
  2. MLTextAnalyzer analyzer = MLAnalyzerFactory.getInstance().getRemoteTextAnalyzer();
  3. //Create an MLFraMLFramesing the bitmap.
  4. MLFrame frame = MLFrame.fromBitmap(bitmap);
  5. // Detect text in an image.
  6. Task<MLText> task = analyzer.asyncAnalyseFrame(frame);
  7. task.addOnSuccessListener(new OnSuccessListener<MLText>() {
  8.    @Override
  9.    public void onSuccess(MLText text) {
  10.     // Detection success.
  11.    }
  12. }).addOnFailureListener(new OnFailureListener() {
  13.    @Override
  14.    public void onFailure(Exception e) {
  15.     // Detection failure.
  16.    }
  17. });

Constant Summary

TypeNameDesc
intOCR_LOCAL_TYPEOn-device detection mode.
intOCR_REMOTE_TYPEOn-cloud detection mode.

Public Method Summary

Return typeMethod name
android.util.SparseArray<MLText.Block>analyseFrame(MLFrame frame)
com.huawei.hmf.tasks.Task<MLText>asyncAnalyseFrame(MLFrame frame)
voidclose()throws IOException
voidstop()throws IOException
intgetAnalyseType()
booleanisAvailable()

Constants

Constant
public static final int OCR_LOCAL_TYPE

On-device detection mode.
Constant value: 0

Constant
public static final int OCR_REMOTE_TYPE

On-cloud detection mode.
Constant value: 1

Public Methods

Method
public android.util.SparseArray<MLText.Block> analyseFrame(MLFrame frame)
Detects text information in a supplied image. This method applies only to the scenario where an analyzer is created using MLTextAnalyzer.Factory.

Parameters

Parameter nameParameter desc
frameImage to be detected.

Return

Typedesc
android.util.SparseArray<MLText.Block>Returns the detected text block information, including the content, language, and text bounding box.
Method
public com.huawei.hmf.tasks.Task<MLText> asyncAnalyseFrame(MLFrame frame)
Asynchronously detects text information in an image. This method applies only to the scenario where an analyzer is created using MLAnalyzerFactory.getInstance().getLocalTextAnalyzer() or MLAnalyzerFactory.getInstance().getRemoteTextAnalyzer().

Parameters

Parameter nameParameter desc
frameImage to be detected.

Return

Typedesc
com.huawei.hmf.tasks.Task<MLText>Returns the asynchronous task that encapsulates the detection result.
Method
public void close() throws IOException
Releases resources, including input and output streams.

Throws

Exception nameException desc
IOExceptionException that occurs in the input and output streams during resource release.
Method
public void stop() throws IOException
Releases resources, including input and output streams.

Throws

Exception nameException desc
IOExceptionException that occurs in the input and output streams during resource release.
Method
public int getAnalyseType()
Obtains the detection mode.

Return

Typedesc
int

Returns the detection mode

0: OCR_LOCAL_TYPE, indicating the on-device detection mode

1: OCR_REMOTE_TYPE, indicating the on-cloud detection mode
Method
public boolean isAvailable()
Indicates whether the analyzer is available.

Return

Typedesc
booleantrue: The analyzer is available.
false: The detector is unavailable.