We use essential cookies for the website to function, as well as analytics cookies for analyzing and creating statistics of the website performance. To agree to the use of analytics cookies, click "Accept All". You can manage your preferences at any time by clicking "Cookie Settings" on the footer. More Information.

Only Essential Cookies
Accept All
ReferencesScan KitAndroidcom.huawei.hms.hmsscankitClass SummarryScanUtil

ScanUtil

Expand

Class Info

public class ScanUtil

Barcode scanning utility class, which is used to check permissions and start the barcode scanning screen. This class supports barcode scanning in Default View mode and Bitmap mode and provides an API for generating barcodes.

Public Field Summary

Expand

Qualifier and Type

Field and Description

static final int

CAMERA_INIT_ERROR

The camera failed to be initialized.

static final int

ERROR_NO_CAMERA_PERMISSION

No camera access permission.

static final int

ERROR_NO_READ_PERMISSION

The app does not have the read permission on the file directory.

static final String

RESULT

Result key.

static final String

RESULT_CODE

Result key, indicating the returned result.

static final int

SCAN_NO_DETECTED

No barcode is detected.

static final int

SUCCESS

Barcode scanning screen started successfully.

Public Method Summary

Expand

Qualifier and Type

Method Name and Description

static Bitmap

buildBitmap(String content, int type, int width, int height, HmsBuildBitmapOption options) throws WriterException

Generates a 1D or 2D barcode.

static Bitmap

compressBitmap(Context context, String path)

Obtains the bitmap from a specific path, compresses the bitmap to a lower sampling rate, and then returns the compressed bitmap that meets the size requirements.

static HmsScanResult

decode(Context context, HmsScanFrame frame, HmsScanFrameOptions options)

Decodes a scanned image that can contain either a single barcode or multiple barcodes.

static HmsScan[]

decodeWithBitmap(Context context, Bitmap bitmap, HmsScanAnalyzerOptions format)

Decodes a scanned image in Bitmap mode.

static int

startScan(Activity activity, int requestCode, HmsScanAnalyzerOptions option)

Scans barcodes in Default View mode.

static void

setOperationTrackingEnabled(boolean enabled)

Enables the O&M capability. This capability is enabled by default.

Public Fields

CAMERA_INIT_ERROR

Expand

Field

public static final int CAMERA_INIT_ERROR

The camera failed to be initialized.

For details, see Result Codes.

ERROR_NO_CAMERA_PERMISSION

Expand

Field

public static final int ERROR_NO_CAMERA_PERMISSION

No camera access permission.

For details, see Result Codes.

ERROR_NO_READ_PERMISSION

Expand

Field

public static final int ERROR_NO_READ_PERMISSION

The app does not have the read permission on the file directory.

For details, see Result Codes.

RESULT

Expand

Field

public static final String RESULT

Result key.

Constant value: SCAN_RESULT

RESULT_CODE

Expand

Field

public static final String RESULT_CODE

Result key, indicating the returned result.

Constant value: SCAN_RESULT_CODE

SCAN_NO_DETECTED

Expand

Field

public static final int SCAN_NO_DETECTED

No barcode is detected.

Constant value: 4096

SUCCESS

Expand

Field

public static final int SUCCESS

Barcode scanning screen started successfully.

Constant value: 0

Public Methods

buildBitmap

Expand

Method

public static Bitmap buildBitmap(String content, int type, int width, int height, HmsBuildBitmapOption options) throws WriterException

Generates a 1D or 2D barcode.

Parameters

Expand

Name

Description

content

Barcode content. For details about barcode generation configurations, please refer to Recommended Settings for Barcode Generation.

type

Barcode format. Currently, Scan Kit supports the following barcode formats: EAN-8, EAN-13, UPC-A, UPC-E, Codabar, Code 39, Code 93, Code 128, ITF-14, QR Code, DataMatrix, PDF417, and Aztec. For details, see Public Field Summary.

width

Width of a barcode to be generated.

height

Height of a barcode to be generated.

options

Barcode generation options.

Throws

Expand

Name

Description

WriterException

Barcode generation exception.

Returns

Expand

Type

Description

Bitmap

Generated barcode.

compressBitmap

Expand

Method

public static Bitmap compressBitmap(Context context, String path)

Obtains the bitmap from a specific path, compresses the bitmap to a lower sampling rate, and then returns the compressed bitmap that meets the size requirements.

Parameters

Expand

Name

Description

context

Request context, which is generally the activity object that is being called, for example, MainActivity.this.

path

Bitmap path.

Returns

Expand

Type

Description

Bitmap

Compressed bitmap.

decode

Expand

Method

public static HmsScanResult decode(Context context, HmsScanFrame frame, HmsScanFrameOptions options)

Decodes a scanned image that can contain either a single barcode or multiple barcodes.

Parameters

Expand

Name

Description

context

App context.

frame

Image data of the barcode scanning request. The following formats are supported: NV21 and bitmap.

options

Scanning format options.

Returns

Expand

Type

Description

HmsScanResult

Result of the barcode scanning request.

  • state: scanning result code. 0 indicates success, and other values indicate failure.
  • HmsScan[]: scanning result

decodeWithBitmap

Expand

Method

public static HmsScan[] decodeWithBitmap(Context context, Bitmap bitmap, HmsScanAnalyzerOptions format)

Decodes a scanned image in Bitmap mode.

Parameters

Expand

Name

Description

context

Request context, which is generally the activity object that is being called, for example, MainActivity.this.

bitmap

Image to be decoded. For a desired effect, it is recommended that the image dimensions range from 960 x 540 px to 1920 x 1080 px, with a maximum image size of 50 MB.

format

Specified barcode format.

Returns

Expand

Type

Description

HmsScan[]

Image decoding result.

startScan

Expand

Method

public static int startScan(Activity activity, int requestCode, HmsScanAnalyzerOptions option)

Scans barcodes in Default View mode. Before the scanning, it checks whether your app has the camera access permission and the storage permission required for scanning. If so, the scanning screen defined by Scan Kit is displayed; otherwise, your app will exit the scanning screen.

Parameters

Expand

Name

Description

activity

Scanning request activity.

requestCode

Scanning request code, which is used to check whether the activity of the barcode scanning screen is returned.

option

Scanning format options.

Returns

Expand

Type

Description

int

Scanning request result.

setOperationTrackingEnabled

Expand

Method

public static void setOperationTrackingEnabled(boolean enabled)

Enables the O&M capability. This capability is enabled by default.

Parameters

Expand

Name

Description

enabled

Indicates whether to collect information for statistical analysis and user experience improvement.

  • true: yes
  • false: no
Search in References
Enter a keyword.