目录

Assigning Permissions

Before calling HUAWEI Scan Kit, you must assign required permissions in the manifest file. Android provides two scanning permissions: CAMERA (camera permission) and READ_EXTERNAL_STORAGE (file reading permission).

  1. <!--Camera permission-->
  2. <uses-permission android:name="android.permission.CAMERA" />
  3. <!--File reading permission-->
  4. <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
  5. <!--Used feature-->
  6. <uses-feature android:name="android.hardware.camera" />
  7. <uses-feature android:name="android.hardware.camera.autofocus" />

After specifying permissions in the manifest file, you will need to dynamically request the permissions in the code.

  1. // CAMERA_REQ_CODE is user-defined and is used to receive the permission verification result.
  2. ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE}, CAMERA_REQ_CODE);

You check whether the corresponding permission is enabled and determine whether to continue scanning the barcode.

  1. // Use the onRequestPermissionsResult function to receive the permission verification result.
  2. @Override
  3. public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
  4. // Check whether requestCode is set to the value of CAMERA_REQ_CODE during permission application, and then check whether the permission is enabled.
  5.    if (requestCode == CAMERA_REQ_CODE && grantResults.length == 2 && grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  6.        // Call the barcode scanning API to build the scanning capability.
  7.        ...
  8.    }
  9. }