Before API development, you need to make necessary development preparations, ensure that the Maven repository address of the HMS Core SDK has been configured in your project, and the SDK of this service has been integrated.
1. Create a landmark analyzer. You can create the analyzer using the MLRemoteLandmarkAnalyzerSetting class.
- // Method 1: Use default parameter settings.
- MLRemoteLandmarkAnalyzer analyzer = MLAnalyzerFactory.getInstance().getRemoteLandmarkAnalyzer();
- // Method 2: Use customized parameter settings.
- /**
- * setLargestNumOfReturns: maximum number of recognition results.
- * setPatternType: analyzer mode.
- * MLRemoteLandmarkAnalyzerSetting.STEADY_PATTERN: The value 1 indicates the stable mode.
- * MLRemoteLandmarkAnalyzerSetting.NEWEST_PATTERN: The value 2 indicates the latest mode.
- */
- MLRemoteLandmarkAnalyzerSetting settings = new MLRemoteLandmarkAnalyzerSetting.Factory()
- .setLargestNumOfReturns(1)
- .setPatternType(MLRemoteLandmarkAnalyzerSetting.STEADY_PATTERN)
- .create();
- MLRemoteLandmarkAnalyzer analyzer = MLAnalyzerFactory.getInstance().getRemoteLandmarkAnalyzer(settings);
2. Create an MLFrame object by using android.graphics.Bitmap. JPG, JPEG, PNG, and BMP images are supported. It is recommended that the image size be greater than or equal to 640 x 640 px.
- MLFrame mlFrame = new MLFrame.Creator().setBitmap(bitmap).create();
3. Implement landmark recognition.
- Task<List<MLRemoteLandmark>> task = analyzer.asyncAnalyseFrame(mlFrame);
- task.addOnSuccessListener(new OnSuccessListener<List<MLRemoteLandmark>>() {
- public void onSuccess(List<MLRemoteLandmark> landmarkResults) {
- // Processing logic for recognition success.
- }})
- .addOnFailureListener(new OnFailureListener() {
- public void onFailure(Exception e) {
- // Processing logic for recognition failure.
- // Recognition failure.
- try {
- MLException mlException = (MLException)e;
- // Obtain the result codes. You can process the result codes and customize respective messages displayed to users. For details about the result codes, please refer to MLException.
- int errorCode = mlException.getErrCode();
- // Obtain the error information. You can quickly locate the fault based on the result code.
- String errorMessage = mlException.getMessage();
- } catch (Exception error) {
- // Handle the conversion error.
- }
- }
- });
4. After the recognition is complete, stop the analyzer to release recognition resources.
- if (analyzer != null) {
- try {
- analyzer.stop();
- } catch (IOException e) {
- // Exception handling.
- }
- }