[Huawei Developers] HMS Community:
Introduction

In this article, we can learn how to save the hospital details by scanning the barcode and saving the details in your contacts directory using Huawei Scan Kit. Due to busy days like journey, office work and personal work, users are not able to save many details. So, this app helps you to save the hospital information by just one scan of barcode from your phone such as Hospital Name, Contact Number, Email address, Website etc.

So, I will provide a series of articles on this Patient Tracking App, in upcoming articles I will integrate other Huawei Kits.

If you are new to this application, follow my previous articles.

https://forums.developer.huawei.com/forumPortal/en/topic/0201902220661040078

https://forums.developer.huawei.com/forumPortal/en/topic/0201908355251870119

https://forums.developer.huawei.com/forumPortal/en/topic/0202914346246890032

https://forums.developer.huawei.com/forumPortal/en/topic/0202920411340450018

https://forums.developer.huawei.com/forumPortal/en/topic/0202926518891830059

What is scan kit?

HUAWEI Scan Kit scans and parses all major 1D and 2D barcodes and generates QR codes, helps you to build quickly barcode scanning functions into your apps.

HUAWEI Scan Kit automatically detects, magnifies and identifies barcodes from a distance and also it can scan a very small barcode in the same way. It supports 13 different formats of barcodes, as follows.

1D barcodes: EAN-8, EAN-13, UPC-A, UPC-E, Codabar, Code 39, Code 93, Code 128 and ITF

2D barcodes: QR Code, Data Matrix, PDF 417 and Aztec

Requirements

1. Any operating system (MacOS, Linux and Windows).

2. Must have a Huawei phone with HMS 4.0.0.300 or later.

3. Must have a laptop or desktop with Android Studio, Jdk 1.8, SDK platform 26 and Gradle 4.6 installed.

4. Minimum API Level 19 is required.

5. Required EMUI 9.0.0 and later version devices.

How to integrate HMS Dependencies

1. First register as Huawei developer and complete identity verification in Huawei developers website, refer to register a Huawei ID.

2. Create a project in android studio, refer Creating an Android Studio Project.

3. Generate a SHA-256 certificate fingerprint.

4. To generate SHA-256 certificate fingerprint. On right-upper corner of android project click Gradle, choose Project Name > Tasks > android, and then click signingReport, as follows.

Note: Project Name depends on the user created name.

5. Create an App in AppGallery Connect.

6. Download the agconnect-services.json file from App information, copy and paste in android Project under app directory, as follows.

7. Enter SHA-256 certificate fingerprint and click tick icon, as follows.

Note: Above steps from Step 1 to 7 is common for all Huawei Kits.

8. Add the below maven URL in build.gradle(Project) file under the repositories of buildscript, dependencies and allprojects, refer Add Configuration.

maven { url 'http://developer.huawei.com/repo/' }
classpath 'com.huawei.agconnect:agcp:1.4.1.300'

9. Add the below plugin and dependencies in build.gradle(Module) file.

apply plugin: 'com.huawei.agconnect'
// Huawei AGC
implementation 'com.huawei.agconnect:agconnect-core:1.5.0.300'
// Scan Kit
implementation 'com.huawei.hms:scan:1.2.5.300'

10. Now Sync the gradle.

11. Add the required permission to the AndroidManifest.xml file.

<!-- Camera permission -->
<uses-permission android:name="android.permission.CAMERA" />
<!-- File read permission -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />

Let us move to development

I have created a project on Android studio with empty activity let's start coding.

In the ScanActivity.kt we can find the button click.

class ScanActivity : AppCompatActivity() {

companion object{
private val CUSTOMIZED_VIEW_SCAN_CODE = 102
}
private var resultText: TextView? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_scan)

resultText = findViewById<View>(R.id.result) as TextView
requestPermission()

}

fun onCustomizedViewClick(view: View?) {
resultText!!.text = ""
this.startActivityForResult(Intent(this, BarcodeScanActivity::class.java), CUSTOMIZED_VIEW_SCAN_CODE)
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)

if (resultCode != RESULT_OK || data == null) {
return
}
// Get return value of HmsScan from the value returned by the onActivityResult method by ScanUtil.RESULT as key value.
val obj: HmsScan? = data.getParcelableExtra(ScanUtil.RESULT)
try {
val json = JSONObject(obj!!.originalValue)
// Log.e("Scan","Result "+json.toString())
val name = json.getString("hospital name")
val phone = json.getString("phone")
val mail = json.getString("email")
val web = json.getString("site")
val i = Intent(Intent.ACTION_INSERT_OR_EDIT)
i.type = ContactsContract.Contacts.CONTENT_ITEM_TYPE
i.putExtra(ContactsContract.Intents.Insert.NAME, name)
i.putExtra(ContactsContract.Intents.Insert.PHONE, phone)
i.putExtra(ContactsContract.Intents.Insert.EMAIL, mail)
i.putExtra(ContactsContract.Intents.Insert.COMPANY, web)

startActivity(i)
} catch (e: JSONException) {
e.printStackTrace()
Toast.makeText(this, "JSON exception", Toast.LENGTH_SHORT).show()
} catch (e: Exception) {
e.printStackTrace()
Toast.makeText(this, "Exception", Toast.LENGTH_SHORT).show()
}

}

private fun requestPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(arrayOf(android.Manifest.permission.CAMERA, READ_EXTERNAL_STORAGE),1001)
}
}

@SuppressLint("MissingSuperCall")
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String?>, grantResults: IntArray) {
if (permissions == null || grantResults == null || grantResults.size < 2 || grantResults[0] != PackageManager.PERMISSION_GRANTED || grantResults[1] != PackageManager.PERMISSION_GRANTED) {
requestPermission()
}
}

}

In the BarcodeScanActivity.kt we can find the code to scan barcode.

class BarcodeScanActivity : AppCompatActivity() {

companion object {
private var remoteView: RemoteView? = null
//val SCAN_RESULT = "scanResult"
var mScreenWidth = 0
var mScreenHeight = 0
//scan view finder width and height is 350dp
val SCAN_FRAME_SIZE = 300
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_barcode_scan)

// 1. get screen density to calculate viewfinder's rect
val dm = resources.displayMetrics
val density = dm.density
// 2. get screen size
mScreenWidth = resources.displayMetrics.widthPixels
mScreenHeight = resources.displayMetrics.heightPixels
val scanFrameSize = (SCAN_FRAME_SIZE * density).toInt()
// 3. Calculate viewfinder's rect, it is in the middle of the layout.
// set scanning area(Optional, rect can be null. If not configure, default is in the center of layout).
val rect = Rect()
rect.left = mScreenWidth / 2 - scanFrameSize / 2
rect.right = mScreenWidth / 2 + scanFrameSize / 2
rect.top = mScreenHeight / 2 - scanFrameSize / 2
rect.bottom = mScreenHeight / 2 + scanFrameSize / 2

// Initialize RemoteView instance and set calling back for scanning result.
remoteView = RemoteView.Builder().setContext(this).setBoundingBox(rect).setFormat(HmsScan.ALL_SCAN_TYPE).build()
remoteView?.onCreate(savedInstanceState)
remoteView?.setOnResultCallback(OnResultCallback { result -> //judge the result is effective
if (result != null && result.size > 0 && result[0] != null && !TextUtils.isEmpty(result[0].getOriginalValue())) {
val intent = Intent()
intent.putExtra(ScanUtil.RESULT, result[0])
setResult(RESULT_OK, intent)
this.finish()
}else{
Log.e("Barcode","Barcode: No barcode recognized ")
}
})

// Add the defined RemoteView to page layout.
val params = FrameLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT)
val frameLayout = findViewById<FrameLayout>(R.id.rim1)
frameLayout.addView(remoteView, params)

}

// Manage remoteView lifecycle
override fun onStart() {
super.onStart()
remoteView?.onStart()
}
override fun onResume() {
super.onResume()
remoteView?.onResume()
}
override fun onPause() {
super.onPause()
remoteView?.onPause()
}
override fun onDestroy() {
super.onDestroy()
remoteView?.onDestroy()
}
override fun onStop() {
super.onStop()
remoteView?.onStop()
}

}

In the activity_scan.xml we can create the UI screen.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".scan.ScanActivity">

<Button
android:id="@+id/btn_click"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:textAllCaps="false"
android:textSize="20sp"
android:layout_gravity="center"
android:text="Click to Scan"
android:onClick="onCustomizedViewClick"
tools:ignore="OnClick" />
<TextView
android:id="@+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_marginTop="80dp"
android:textColor="#C0F81E"/>

</LinearLayout>

In the activity_barcode_scan.xml we can create the frame layout.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".scan.BarcodeScanActivity">

// customize layout for camera preview to scan
<FrameLayout
android:id="@+id/rim1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#C0C0C0" />
// customize scanning mask
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true"
android:alpha="0.1"
android:background="#FF000000"/>
// customize scanning view finder
<ImageView
android:id="@+id/scan_view_finder"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true"
android:background="#1f00BCD4"
tools:ignore="MissingConstraints" />

</RelativeLayout>

Demo

Find the demo in attachment or click here for original content.

Tips and Tricks

1. Make sure you are already registered as Huawei developer.

2. Set minSDK version to 19 or later, otherwise you will get AndriodManifest merge issue.

3. Make sure you have added the agconnect-services.json file to app folder.

4. Make sure you have added SHA-256 fingerprint without fail.

5. Make sure all the dependencies are added properly.

Conclusion

In this article, we can learn how to save the hospital details by scanning the barcode and saving the details in your contacts directory using Huawei Scan Kit. Due to busy days like journey, office work and personal work, users are not able to save many details. So, this app helps you to save the hospital information by just one scan of barcode from your phone such as Hospital Name, Contact Number, Email address, Website etc.

Reference

Scan Kit - Customized View

Scan Kit - Training Video