DEV Community

HMS Community
HMS Community

Posted on

Integration of ML Kit in Android KnowMyBoard App using Navigation Components, MVVM, Data Binding

Image description
Introduction

In this article, we will learn how to integrate Huawei ML kit, Account kit and Location kit in Android application KnowMyBoard. Account Kit provides seamless login functionality to the app with large user base.

ML Kit provides app to easily leverage Huawei's long-term proven expertise in machine learning to support diverse artificial intelligence (AI) applications throughout a wide range of industries.

ML kit provides various services, in this application we will be integrating its text related service like text recognition, text detection and text translation services, which helps in achieve the gaol of the application.

Location kit SDK for Android offers location-related APIs for Android apps. These APIs mainly relate to 6 functions like fused location, activity identification, geofence, high-precision location, indoor location, and geocoding. This mode is applicable to mobile phones and Huawei tablets. We are using Location kit to get location of user.

Supported Devices
Image description
Supported Countries/Regions

Supported Countries/Regions

Development Overview

You need to install Android Studio IDE and I assume that you have prior knowledge of Android application development.

Hardware Requirements
A computer (desktop or laptop) running Windows 10.
Android phone (with the USB cable), which is used for debugging.

Software Requirements

  • Java JDK 1.8 or later.
  • Android Studio software or Visual Studio or Code installed.
  • HMS Core (APK) 4.X or later

Integration steps
Step 1. Huawei developer account and complete identity verification in Huawei developer website, refer to register Huawei ID.
Step 2. **Create project in AppGallery Connect
**Step 3.
Adding HMS Core SDK

Let's start coding

*MainActivity.java *

public class MainActivity extends AppCompatActivity {

    LoginViewModel loginViewModel;
    private MLTextAnalyzer mTextAnalyzer;
    public Uri imagePath;
    Bitmap bitmap;
    ArrayList<String> result = new ArrayList<>();
    MLLocalLangDetector myLocalLangDetector;
    MLLocalTranslator myLocalTranslator;
    String textRecognized;
    ProgressDialog progressDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        loginViewModel = new ViewModelProvider(this).get(LoginViewModel.class);
        MyApplication.setActivity(this);
        progressDialog = new ProgressDialog(this);
        progressDialog.setCancelable(false);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        // Process the authorization result to obtain the authorization code from AuthAccount.
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 8888) {
            Task<AuthAccount> authAccountTask = AccountAuthManager.parseAuthResultFromIntent(data);
            if (authAccountTask.isSuccessful()) {
                // The sign-in is successful, and the user's ID information and authorization code are obtained.
                AuthAccount authAccount = authAccountTask.getResult();
                UserData userData = new UserData();
                userData.setAccessToken(authAccount.getAccessToken());
                userData.setCountryCode(authAccount.getCountryCode());
                userData.setDisplayName(authAccount.getDisplayName());
                userData.setEmail(authAccount.getEmail());
                userData.setFamilyName(authAccount.getFamilyName());
                userData.setGivenName(authAccount.getGivenName());
                userData.setIdToken(authAccount.getIdToken());
                userData.setOpenId(authAccount.getOpenId());
                userData.setUid(authAccount.getUid());
                userData.setPhotoUriString(authAccount.getAvatarUri().toString());
                userData.setUnionId(authAccount.getUnionId());
                Gson gson = new Gson();
                Log.e("TAG", "sign in success : " + gson.toJson(authAccount));
                loginViewModel = new ViewModelProvider(MainActivity.this).get(LoginViewModel.class);
                loginViewModel.sendData(authAccount.getDisplayName());
                progressDialog.dismiss();
            } else {
                // The sign-in failed.
                Log.e("TAG", "sign in failed:" + ((ApiException) authAccountTask.getException()).getStatusCode());
                progressDialog.dismiss();
            }
        }
        if (requestCode == 2323 && resultCode == RESULT_OK && data != null) {
            progressDialog.setMessage("Initializing text detection..");
            progressDialog.show();
            imagePath = data.getData();
            try {
                bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imagePath);
            } catch (IOException e) {
                e.printStackTrace();
                Log.e("TAG", " BITMAP ERROR");
            }
            Log.d("TAG", "Path " + imagePath.getPath());
            try {
                Bitmap selectedBitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), imagePath);
                asyncAnalyzeText(selectedBitmap);
            } catch (IOException e) {
                e.printStackTrace();
                progressDialog.dismiss();
            }
        }
    }

    private void asyncAnalyzeText(Bitmap bitmap) {

        if (mTextAnalyzer == null) {
            createMLTextAnalyzer();
        }

        MLFrame frame = MLFrame.fromBitmap(bitmap);

        Task<MLText> task = mTextAnalyzer.asyncAnalyseFrame(frame);
        task.addOnSuccessListener(new OnSuccessListener<MLText>() {
            @Override
            public void onSuccess(MLText text) {
                progressDialog.setMessage("Initializing language detection..");
                Log.d("TAG", "#" + text.getStringValue());
                textRecognized = text.getStringValue().trim();
                if(!textRecognized.isEmpty()){
                    // Create a local language detector.
                    MLLangDetectorFactory factory = MLLangDetectorFactory.getInstance();
                    MLLocalLangDetectorSetting setting = new MLLocalLangDetectorSetting.Factory()
                            // Set the minimum confidence threshold for language detection.
                            .setTrustedThreshold(0.01f)
                            .create();
                    myLocalLangDetector = factory.getLocalLangDetector(setting);
                    Task<String> firstBestDetectTask = myLocalLangDetector.firstBestDetect(textRecognized);
                    firstBestDetectTask.addOnSuccessListener(new OnSuccessListener<String>() {
                        @Override
                        public void onSuccess(String languageDetected) {
                            progressDialog.setMessage("Initializing text translation..");
                            // Processing logic for detection success.
                            Log.d("TAG", "Lang detect :" + languageDetected);
                            textTranslate(languageDetected, textRecognized, bitmap);
                        }
                    }).addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(Exception e) {
                            // Processing logic for detection failure.

                            Log.e("TAG", "Lang detect error:" + e.getMessage());
                        }
                    });
                }else{
                    progressDialog.dismiss();
                    showErrorDialog("Failed to recognize text.");

                }

            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(Exception e) {
                Log.e("TAG", "Error " + e.getMessage());
            }
        });
    }

    private void showErrorDialog(String msg) {
        AlertDialog alertDialog = new AlertDialog.Builder(this).create();
        alertDialog.setTitle("Error");
        alertDialog.setMessage(msg);

        alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });

        alertDialog.show();
    }

    private void textTranslate(String languageDetected, String textRecognized, Bitmap uri) {
        MLApplication.initialize(getApplication());
        MLApplication.getInstance().setApiKey("fDkimdjcdjjssshwQ==");

        // Create an offline translator.
        MLLocalTranslateSetting setting = new MLLocalTranslateSetting.Factory()
                // Set the source language code. The ISO 639-1 standard is used. This parameter is mandatory. If this parameter is not set, an error may occur.
                .setSourceLangCode(languageDetected)
                // Set the target language code. The ISO 639-1 standard is used. This parameter is mandatory. If this parameter is not set, an error may occur.
                .setTargetLangCode("en")
                .create();

         myLocalTranslator = MLTranslatorFactory.getInstance().getLocalTranslator(setting);
        // Set the model download policy.
        MLModelDownloadStrategy downloadStrategy = new MLModelDownloadStrategy.Factory()
                .needWifi()// It is recommended that you download the package in a Wi-Fi environment.
                .create();
        // Create a download progress listener.
        MLModelDownloadListener modelDownloadListener = new MLModelDownloadListener() {
            @Override
            public void onProcess(long alreadyDownLength, long totalLength) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        // Display the download progress or perform other operations.
                    }
                });
            }
        };

        myLocalTranslator.preparedModel(downloadStrategy, modelDownloadListener).
                addOnSuccessListener(new OnSuccessListener<Void>() {
                    @Override
                    public void onSuccess(Void aVoid) {
                        // Called when the model package is successfully downloaded.
                        // input is a string of less than 5000 characters.
                        final Task<String> task = myLocalTranslator.asyncTranslate(textRecognized);
                        // Before translation, ensure that the models have been successfully downloaded.
                        task.addOnSuccessListener(new OnSuccessListener<String>() {
                            @Override
                            public void onSuccess(String translated) {
                                // Processing logic for detection success.
                                result.clear();
                                result.add(languageDetected.trim());
                                result.add(textRecognized.trim());
                                result.add(translated.trim());
                                loginViewModel.setImage(uri);
                                loginViewModel.setTextRecongnized(result);
                                progressDialog.dismiss();
                            }
                        }).addOnFailureListener(new OnFailureListener() {
                            @Override
                            public void onFailure(Exception e) {
                                // Processing logic for detection failure.
                                progressDialog.dismiss();
                            }
                        });
                    }
                }).addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(Exception e) {
                        // Called when the model package fails to be downloaded.
                        progressDialog.dismiss();
                    }
                });

    }

    private void createMLTextAnalyzer() {
        MLLocalTextSetting setting = new MLLocalTextSetting.Factory()
                .setOCRMode(MLLocalTextSetting.OCR_DETECT_MODE)
                .create();
        mTextAnalyzer = MLAnalyzerFactory.getInstance().getLocalTextAnalyzer(setting);

    }

    @Override
    protected void onStop() {
        if (myLocalLangDetector != null) {
            myLocalLangDetector.stop();
        }
        if (myLocalTranslator!= null) {
            myLocalTranslator.stop();
        }
        if(progressDialog!= null){
            progressDialog.dismiss();
        }
        super.onStop();
    }
}

Enter fullscreen mode Exit fullscreen mode

LoginViewModel.java

public class LoginViewModel extends AndroidViewModel {

    AccountAuthService service;
    private MutableLiveData<String> message = new MutableLiveData<>();
    private MutableLiveData<ArrayList<String>> textRecongnized = new MutableLiveData<>();
    private MutableLiveData<Bitmap> image = new MutableLiveData<>();
    private MutableLiveData<LocationResult> locationResult = new MutableLiveData<>();

    public void sendData(String msg) {
        message.setValue(msg);
    }

    public LiveData<String> getMessage() {
        return message;
    }

    public MutableLiveData<ArrayList<String>> getTextRecongnized() {
        return textRecongnized;
    }

    public void setImage(Bitmap imagePath) {

        this.image.setValue(imagePath);
    }

    public MutableLiveData<LocationResult> getLocationResult() {
        return locationResult;
    }

    public void setLocationResult(LocationResult locationResult) {
        this.locationResult.setValue(locationResult);
    }

    public void setTextRecongnized(ArrayList<String> textRecongnized) {
        this.textRecongnized.setValue(textRecongnized);

    }

    public MutableLiveData<Bitmap> getImagePath() {
        return image;
    }

    public LoginViewModel(@NonNull Application application) {
        super(application);
    }

    public void logoutHuaweiID() {
        if (service != null) {
            service.signOut();
            sendData("KnowMyBoard");
            Toast.makeText(getApplication(), "You are logged out from Huawei ID", Toast.LENGTH_LONG).show();
        }
    }

    public void loginClicked(View view) {
        AccountAuthParams authParams = new AccountAuthParamsHelper(AccountAuthParams.DEFAULT_AUTH_REQUEST_PARAM).setAuthorizationCode().createParams();
        service = AccountAuthManager.getService(MyApplication.getActivity(), authParams);
        MyApplication.getActivity().startActivityForResult(service.getSignInIntent(), 8888);

    }

    public void cancelAuthorization() {
        if (service != null) {
            // service indicates the AccountAuthService instance generated using the getService method during the sign-in authorization.
            service.cancelAuthorization().addOnCompleteListener(new OnCompleteListener<Void>() {
                @Override
                public void onComplete(Task<Void> task) {
                    if (task.isSuccessful()) {
                        // Processing after a successful authorization cancellation.
                        Log.i("TAG", "onSuccess: ");
                        sendData("KnowMyBoard");
                        Toast.makeText(getApplication(), "Cancelled authorization", Toast.LENGTH_LONG).show();
                    } else {
                        // Handle the exception.
                        Exception exception = task.getException();
                        if (exception instanceof ApiException) {
                            int statusCode = ((ApiException) exception).getStatusCode();
                            Log.i("TAG", "onFailure: " + statusCode);
                            Toast.makeText(getApplication(), "Failed to cancel authorization", Toast.LENGTH_LONG).show();
                        }
                    }
                }
            });
        } else {
            Toast.makeText(getApplication(), "Login required", Toast.LENGTH_LONG).show();
        }

    }

}

Enter fullscreen mode Exit fullscreen mode

RequestLocationData.java

apublic class RequestLocationData {
    static String TAG = "TAG";
    SettingsClient settingsClient;
    private int isLocationSettingSuccess = 0;
    private LocationRequest myLocationRequest;
    // Define a fusedLocationProviderClient object.
    private FusedLocationProviderClient fusedLocationProviderClient;
    LocationCallback myLocationCallback;
    Context context;
    Activity activity;
    LocationResult locationResult;
    LoginViewModel loginViewModel;
    public RequestLocationData(Context context, FragmentActivity activity, LoginViewModel loginViewModel) {
        setContext(context);
        setActivity(activity);
        setLoginViewModel(loginViewModel);
    }

    public LoginViewModel getLoginViewModel() {
        return loginViewModel;
    }

    public void setLoginViewModel(LoginViewModel loginViewModel) {
        this.loginViewModel = loginViewModel;
    }

    public Context getContext() {
        return context;
    }

    public void setContext(Context context) {
        this.context = context;
    }

    public Activity getActivity() {
        return activity;
    }

    public void setActivity(Activity activity) {
        this.activity = activity;
    }

    public void initFusionLocationProviderClint(){
       // Instantiate the fusedLocationProviderClient object.
       fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(getActivity());
       settingsClient = LocationServices.getSettingsClient(getActivity());
    }
    public void checkDeviceLocationSettings() {
        LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
        myLocationRequest = new LocationRequest();
        builder.addLocationRequest(myLocationRequest);
        LocationSettingsRequest locationSettingsRequest = builder.build();
        // Check the device location settings.
        settingsClient.checkLocationSettings(locationSettingsRequest)
                // Define the listener for success in calling the API for checking device location settings.
                .addOnSuccessListener(locationSettingsResponse -> {
                    LocationSettingsStates locationSettingsStates =
                            locationSettingsResponse.getLocationSettingsStates();
                    StringBuilder stringBuilder = new StringBuilder();
                    // Check whether the location function is enabled.
                    stringBuilder.append(",\nisLocationUsable=")
                            .append(locationSettingsStates.isLocationUsable());
                    // Check whether HMS Core (APK) is available.
                    stringBuilder.append(",\nisHMSLocationUsable=")
                            .append(locationSettingsStates.isHMSLocationUsable());
                    Log.i(TAG, "checkLocationSetting onComplete:" + stringBuilder.toString());
                    // Set the location type.
                    myLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
                    // Set the number of location updates to 1.
                    myLocationRequest.setNumUpdates(1);
                    isLocationSettingSuccess = 1;


                })
                // Define callback for failure in checking the device location settings.
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(Exception e) {
                        Log.i(TAG, "checkLocationSetting onFailure:" + e.getMessage());
                    }
                });

    }

    public void checkPermission() {
        // Dynamically apply for required permissions if the API level is 28 or lower.
        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) {
            Log.i(TAG, "android sdk <= 28 Q");
            if (ActivityCompat.checkSelfPermission(getContext(),
                    Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                    && ActivityCompat.checkSelfPermission(getContext(),
                    Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                String[] strings =
                        {Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.MANAGE_MEDIA,Manifest.permission.MEDIA_CONTENT_CONTROL,Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION};
                ActivityCompat.requestPermissions(getActivity(), strings, 1);
            }
        } else {
            // Dynamically apply for the android.permission.ACCESS_BACKGROUND_LOCATION permission in addition to the preceding permissions if the API level is higher than 28.
            if (ActivityCompat.checkSelfPermission(getActivity(),
                    Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                    && ActivityCompat.checkSelfPermission(getContext(),
                    Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED
                    && ActivityCompat.checkSelfPermission(getContext(),
                    "android.permission.ACCESS_BACKGROUND_LOCATION") != PackageManager.PERMISSION_GRANTED) {
                String[] strings = {android.Manifest.permission.ACCESS_FINE_LOCATION,
                        android.Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.MEDIA_CONTENT_CONTROL,Manifest.permission.MANAGE_MEDIA,
                        "android.permission.ACCESS_BACKGROUND_LOCATION"};
                ActivityCompat.requestPermissions(getActivity(), strings, 2);
            }
        }
    }
    public LocationResult refreshLocation() {

        if (isLocationSettingSuccess == 1) {
            myLocationCallback = new LocationCallback() {
                @Override
                public void onLocationResult(LocationResult locationResult) {
                    if (locationResult != null) {                 
                        getLoginViewModel().setLocationResult(locationResult);
                    }
                }
            };
            fusedLocationProviderClient.requestLocationUpdates(myLocationRequest, myLocationCallback, Looper.getMainLooper());

        } else {
            Log.d(TAG, "Failed to get location settings");
        }
        return locationResult;
    }

    public void disableLocationData(){
        fusedLocationProviderClient.disableBackgroundLocation();
        fusedLocationProviderClient.removeLocationUpdates(myLocationCallback);
    }

}

Enter fullscreen mode Exit fullscreen mode

navigation_graph.xml

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/navigation_graph"
    app:startDestination="@id/loginFragment">
    <fragment
        android:id="@+id/loginFragment"
        android:name="com.huawei.hms.knowmyboard.dtse.activity.fragments.LoginFragment"
        android:label="LoginFragment">

        <action
            android:id="@+id/action_loginFragment_to_mainFragment"
            app:destination="@id/mainFragment" />
    </fragment>
    <fragment
        android:id="@+id/mainFragment"
        android:name="com.huawei.hms.knowmyboard.dtse.activity.fragments.MainFragment"
        android:label="MainFragment">

        <action
            android:id="@+id/action_mainFragment_to_loginFragment"
            app:destination="@id/loginFragment" />
    </fragment>
</navigation>
Enter fullscreen mode Exit fullscreen mode

LoginFragment.java

public class LoginFragment extends Fragment {

    FragmentLoginBinding loginBinding;
    LoginViewModel loginViewModel;
    Menu menu;
    SharedPreferences prefs;
    SharedPreferences.Editor editor;
    NavController navController;
    private String MY_PREF_NAME = "my_pref_name";
    private String TAG = "TAG";

    public LoginFragment() {
        // Required empty public constructor
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        navController = Navigation.findNavController(view);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        loginBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_login, container, false);
        loginViewModel = new ViewModelProvider(getActivity()).get(LoginViewModel.class);
        loginBinding.setLoginViewModel(loginViewModel);
        Log.d(TAG, " Pref " + getPreferenceValue());
        if (getPreferenceValue().equals("user_name")) {
            loginBinding.btnHuaweiIdAuth.setVisibility(View.VISIBLE);
        } else {
            enableMenu(menu);
            getActivity().setTitle(getPreferenceValue());
            loginBinding.btnHuaweiIdAuth.setVisibility(View.GONE);
        }
        loginBinding.imageLogo.setOnClickListener(v -> {
            navController.navigate(R.id.action_loginFragment_to_mainFragment);
        });


        loginViewModel.getMessage().observeForever(new Observer<String>() {
            @Override
            public void onChanged(String message) {
                updateMessage(message);
                if (!message.equals(getResources().getString(R.string.app_name))) {
                    setPreferenceValue(message);
                    enableMenu(menu);
                    loginBinding.btnHuaweiIdAuth.setVisibility(View.GONE);
                } else {
                    disableMenu(menu);
                    loginBinding.btnHuaweiIdAuth.setVisibility(View.VISIBLE);
                    setPreferenceValue("user_name");
                }

            }
        });

        return loginBinding.getRoot();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
        menu.clear();
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.main, menu);
        this.menu = menu;
        disableMenu(menu);

    }

    private void disableMenu(Menu menu) {
        try {
            if (menu != null) {
                if (getPreferenceValue().equals("user_name")) {
                    menu.findItem(R.id.menu_login_logout).setVisible(false);
                    menu.findItem(R.id.menu_cancel_auth).setVisible(false);
                    getActivity().setTitle(getResources().getString(R.string.app_name));
                } else {
                    menu.findItem(R.id.menu_login_logout).setVisible(true);
                    menu.findItem(R.id.menu_cancel_auth).setVisible(true);
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void enableMenu(Menu menu) {
        try {
            menu.findItem(R.id.menu_login_logout).setVisible(true);
            menu.findItem(R.id.menu_cancel_auth).setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_cancel_auth:
                setPreferenceValue("user_name");
                loginViewModel.cancelAuthorization();
                loginBinding.btnHuaweiIdAuth.setVisibility(View.VISIBLE);
                disableMenu(menu);
                return true;
            case R.id.menu_login_logout:

                setPreferenceValue("user_name");
                loginViewModel.logoutHuaweiID();
                loginBinding.btnHuaweiIdAuth.setVisibility(View.VISIBLE);
                disableMenu(menu);
                return true;

            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    public void updateMessage(String msg) {
        getActivity().setTitle(msg);

    }

    void setPreferenceValue(String userName) {
        editor = getActivity().getSharedPreferences(MY_PREF_NAME, MODE_PRIVATE).edit();
        editor.putString("user_name", userName);
        editor.apply();
    }

    String getPreferenceValue() {
        prefs = getActivity().getSharedPreferences(MY_PREF_NAME, MODE_PRIVATE);
        return prefs.getString("user_name", "user_name");

    }

}

Enter fullscreen mode Exit fullscreen mode

MainFragment.java

public class MainFragment extends Fragment {


    static String TAG = "TAG";
    FragmentMainFragmentBinding binding;
    LoginViewModel loginViewModel;
    RequestLocationData locationData;

    public MainFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_main_fragment, container, false);
        loginViewModel = new ViewModelProvider(getActivity()).get(LoginViewModel.class);
        binding.setLoginViewModel(loginViewModel);
        locationData = new RequestLocationData(getContext(),getActivity(),loginViewModel);
        locationData.initFusionLocationProviderClint();
        locationData.checkPermission();
        locationData.checkDeviceLocationSettings();

        binding.buttonScan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                chooseImage();
            }
        });
        loginViewModel.getImagePath().observeForever(new Observer<Bitmap>() {
            @Override
            public void onChanged(Bitmap bitmap) {
                try{
                      binding.imageView.setImageBitmap(bitmap);
                }catch (Exception e){
                    e.printStackTrace();
                    Log.e("TAG","Error : "+e.getMessage());
                }
            }
        });
        loginViewModel.getTextRecongnized().observeForever(new Observer<ArrayList<String>>() {
            @Override
            public void onChanged(ArrayList<String> res) {
                Log.i("TAG","OBSERVER : "+"Language : "+getStringResourceByName(res.get(0).trim())+
                        " Detected text : "+res.get(1).trim()+
                        " Translated text : "+res.get(2).trim());
                  binding.textLanguage.setText("Language : "+getStringResourceByName(res.get(0)));
                  binding.textDetected.setText("Detected text : "+res.get(1));
                  binding.textTranslated.setText("Translated text : "+res.get(2));
            }
        });

        loginViewModel.getLocationResult().observeForever(new Observer<LocationResult>() {
            @Override
            public void onChanged(LocationResult locationResult) {
                binding.textDetected.setText("Latitude " + locationResult.getLastHWLocation().getLatitude() + " Longitude " + locationResult.getLastHWLocation().getLongitude());
            }
        });

        return binding.getRoot();

    }

    private String getStringResourceByName(String aString) {
        String packageName = getActivity().getPackageName();
        int resId = getResources()
                .getIdentifier(aString, "string", packageName);
        if (resId == 0) {
            return aString;
        } else {
            return getString(resId);
        }
    }
    private void chooseImage() {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("image/*");
        getActivity().startActivityForResult(intent, 2323);
    }

    @Override
    public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
        menu.clear();
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.main_fragment_menu, menu);


    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.option_refresh_location:
                //refreshLocation();
                locationData.refreshLocation();
                return true;

        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onStop() {
        super.onStop();
        locationData.disableLocationData();
    }
}

Enter fullscreen mode Exit fullscreen mode

Result

Image description

Tricks and Tips

Makes sure that agconnect-services.json file added.
Make sure required dependencies are added
Make sure that service is enabled in AGC
Images has clear visibility of text
Enable data binding in gradle.build file
Conclusion

In this article, we have learnt how to integrate Huawei Account kit, Location kit and ML kit in Android application KnowMyBoard. You can also go through previous article part-1 here. Hoping that the ML kit capabilities are helpful to you as well, like this sample, you can make use of ML kit to recognition of text and detection of language and translation of text, likewise you can make use of ML kit in tourism app which helps app users to understand different sign boards and get rid of the language barrier.

Thank you so much for reading. I hope this article helps you to understand the integration of Huawei ML kit, Account kit and Location kit in Android application KnowMyBoard.

Reference

ML KitTraining video

Location KitTraining video

Checkout in forum

Top comments (0)