Expert: Integrate to Generate and Scan QR Code APIs on Harmony OS

{
"app": {
"bundleName": "com.hms.harmony",
"vendor": "huawei",
"version": {
"code": 1000000,
"name": "1.0.0"
},
"apiVersion": {
"compatible": 5,
"target": 5
}
},
"deviceConfig": {},
"module": {
"package": "com.huawei.cookbook",
"name": ".MyApplication",
"deviceType": [
"phone",
"tv",
"tablet"
],
"distro": {
"deliveryWithInstall": true,
"moduleName": "entry",
"moduleType": "entry",
"installationFree": true
},
"reqPermissions": [
{
"name": "ohos.permission.CAMERA"
}
],
"abilities": [
{
"skills": [
{
"entities": [
"entity.system.home"
],
"actions": [
"action.system.home"
]
}
],
"orientation": "portrait",
"name": "com.huawei.cookbook.MainAbility",
"icon": "$media:icon",
"description": "$string:description",
"label": "$string:app_name",
"type": "page",
"launchType": "standard"
},
{
"orientation": "portrait",
"name": "com.huawei.cookbook.ScanAbility",
"icon": "$media:icon",
"description": "$string:description",
"label": "$string:app_name",
"type": "page",
"launchType": "standard"
}
]
}
}
apply plugin: 'com.huawei.ohos.app'ohos {
compileSdkVersion 5
defaultConfig {
compatibleSdkVersion 5
}
}
buildscript {
repositories {
maven {
url 'https://repo.huaweicloud.com/repository/maven/'
}
maven {
url 'https://developer.huawei.com/repo/'
}
jcenter()
}
dependencies {
classpath 'com.huawei.ohos:hap:2.4.4.2'
classpath 'com.huawei.ohos:decctest:1.0.0.7'
}
}
allprojects {
repositories {
maven {
url 'https://repo.huaweicloud.com/repository/maven/'
}
maven {
url 'https://developer.huawei.com/repo/'
}
jcenter()
}
}
apply plugin: 'com.huawei.ohos.hap'
apply plugin: 'com.huawei.ohos.decctest'
ohos {
compileSdkVersion 5
defaultConfig {
compatibleSdkVersion 5
}
buildTypes {
release {
proguardOpt {
proguardEnabled false
rulesFiles 'proguard-rules.pro'
}
}
}
signingConfigs {
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar', '*.har'])
testImplementation 'junit:junit:4.13'
ohosTestImplementation 'com.huawei.ohos.testkit:runner:1.0.0.100'
}
decc {
supportType = ['html','xml']
}
package com.huawei.cookbook.slice;import com.huawei.cookbook.ScanAbility;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import com.huawei.cookbook.ResourceTable;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Component;
public class MainAbilitySlice extends AbilitySlice { @Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
initViewEvent();
}
private void initViewEvent() {
presentSlice(ResourceTable.Id_codeGeneration,
new CodeGenerationAbilitySlice());
presentSlice(ResourceTable.Id_codeIdentification,
new CodeIdentificationAbilitySlice());
codeScanning();
}
private void codeScanning() {
findComponentById(ResourceTable.Id_codeScanning).setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
Intent intent = new Intent();
Operation operation = new Intent.OperationBuilder()
.withDeviceId("")
.withBundleName(getBundleName())
.withAbilityName(ScanAbility.class)
.build();
intent.setOperation(operation);
startAbility(intent);
}
});
}
private void presentSlice(int resId, AbilitySlice targetSlice) {
findComponentById(resId).setClickedListener(
component -> present(targetSlice, new Intent()));
}
}
package com.huawei.cookbook;import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.AbilitySliceAnimator;
import ohos.aafwk.content.Intent;
import ohos.bundle.IBundleManager;
import ohos.security.SystemPermission;
import com.huawei.cookbook.slice.MainAbilitySlice;
public class MainAbility extends Ability { private static final int REQUEST_CODE = 20210601;
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setMainRoute(MainAbilitySlice.class.getName());
setAbilitySliceAnimator(new AbilitySliceAnimator().setDuration(0));
requestPermission();
}
private void requestPermission() {
if (verifySelfPermission(SystemPermission.CAMERA)
!= IBundleManager.PERMISSION_GRANTED) {
// has no permission
if (canRequestPermission(SystemPermission.CAMERA)) {
// toast
requestPermissionsFromUser(
new String[]{SystemPermission.CAMERA}, REQUEST_CODE);
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:orientation="vertical">
<Button
ohos:id="$+id:codeGeneration"
ohos:height="match_content"
ohos:width="match_parent"
ohos:background_element="$graphic:background_button"
ohos:bottom_padding="10vp"
ohos:layout_alignment="horizontal_center"
ohos:left_margin="60vp"
ohos:left_padding="16vp"
ohos:right_margin="60vp"
ohos:right_padding="16vp"
ohos:text="Code generation"
ohos:text_color="#ffffff"
ohos:text_size="30"
ohos:top_margin="140vp"
ohos:top_padding="10vp"
/>
<Button
ohos:id="$+id:codeIdentification"
ohos:height="match_content"
ohos:width="match_parent"
ohos:background_element="$graphic:background_button"
ohos:bottom_padding="10vp"
ohos:layout_alignment="horizontal_center"
ohos:left_margin="60vp"
ohos:left_padding="16vp"
ohos:right_margin="60vp"
ohos:right_padding="16vp"
ohos:text="Code recognition"
ohos:text_color="#ffffff"
ohos:text_size="30"
ohos:top_margin="20vp"
ohos:top_padding="10vp"
/>
<Button
ohos:id="$+id:codeScanning"
ohos:height="match_content"
ohos:width="match_parent"
ohos:background_element="$graphic:background_button"
ohos:bottom_padding="10vp"
ohos:layout_alignment="horizontal_center"
ohos:left_margin="60vp"
ohos:left_padding="16vp"
ohos:right_margin="60vp"
ohos:right_padding="16vp"
ohos:text="Code scanning"
ohos:text_color="#ffffff"
ohos:text_size="30"
ohos:top_margin="20vp"
ohos:top_padding="10vp"
/>
</DirectionalLayout>

--

--

--

Android Developer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Last chance to support iOS 6!

How to create Share Sheet (UIActivityViewController) in SwiftUI

3 Things I’d Like To See in Things 3 for iOS

Apple Configurator 2 For Windows

Customized Camera in SwiftUI

Apple’s Combine: Intro to the Reactive Framework

iPhone Text Message Privacy: How to Turn off SMS Preview

What do I use to stream iOS App Development

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Manoj Kumar

Manoj Kumar

Android Developer

More from Medium

Database as a Work Queue

CryptoEmpire in 2022

Android Studio & HMS Core Kit Integration

Android Studio

4 Misconceptions about Biometrics