目录

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.

q. Create an analyzer for product visual search. You can create the analyzer using the MLRemoteProductVisionSearchAnalyzerSetting class.

  1. // Method 1: Use default parameter settings.
  2. MLRemoteProductVisionSearchAnalyzer analyzer  
  3.    = MLAnalyzerFactory.getInstance().getRemoteProductVisionSearchAnalyzer();
  4. // Method 2: Use customized parameter settings.
  5. MLRemoteProductVisionSearchAnalyzerSetting settings = new MLRemoteProductVisionSearchAnalyzerSetting.Factory()
  6. // Set the maximum number of products that can be returned.
  7.    .setLargestNumOfReturns(16)
  8.    .create();
  9. MLRemoteProductVisionSearchAnalyzer analyzer  
  10.    = MLAnalyzerFactory.getInstance().getRemoteProductVisionSearchAnalyzer(settings);

2. Create an MLFrame object by using android.graphics.Bitmap. JPG, JPEG, PNG, and BMP images are supported.

  1. // Create an MLFrame object using the bitmap, which is the image data in bitmap format.
  2. MLFrame mlFrame = new MLFrame.Creator().setBitmap(bitmap).create();

3. Implement image detection.

  1. Task<List<MLProductVisionSearch>> task = analyzer.asyncAnalyseFrame(mlFrame);
  2. task.addOnSuccessListener(new OnSuccessListener<List<MLProductVisionSearch>>() {
  3.    public void onSuccess(List<MLProductVisionSearch> products) {
  4.       // Processing logic for detection success.
  5.    }})
  6.    .addOnFailureListener(new OnFailureListener() {
  7.    public void onFailure(Exception e) {
  8.       // Processing logic for detection failure.
  9.        // Recognition failure.
  10.        try {
  11.            MLException mlException = (MLException)e;
  12.    // 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.
  13.            int errorCode = mlException.getErrCode();
  14.   // Obtain the error information. You can quickly locate the fault based on the result code.
  15.            String errorMessage = mlException.getMessage();
  16.        } catch (Exception error) {
  17.           // Handle the conversion error.
  18.        }
  19.   }
  20. });

4. After the recognition is complete, stop the analyzer to release recognition resources.

  1. if (analyzer != null) {
  2.    analyzer.stop();
  3. }