Beginner: Integration of Camera service in Harmony OS

Introduction

Huawei provides various services for developers to make ease of development and provides best user experience to end users. In this article, we will cover Camera device with Java in Harmony OS.

The HarmonyOS camera module enables your application to provide camera functions. You can access and operate the camera device and develop new functions through the open APIs. Common operations include preview, photographing, burst photographing, and video recording.

Basic Concepts

  • Static camera capability

A series of parameters used to describe inherent capabilities of a camera, such as orientation and supported resolution

  • Physical camera

A physical camera is an independent camera device. The physical camera ID is a string that uniquely identifies a physical camera.

  • Logical camera

A logical camera is the abstracted capability of many physical cameras and centrally controls these physical cameras to implement camera functions, such as wide aperture and zooming. A logical camera ID is a unique character string that identifies the abstraction capability of multiple physical cameras.

  • Frame capture

All actions of capturing frames after a camera is started, including single-frame capture, multi-frame capture and looping-frame capture.

  • Single-frame capture

This function captures one frame in the frame data stream transmitted after the camera is started. It is frequently used for photographing.

  • Multi-frame capture

This function repeatedly captures multiple frames in the frame data stream transmitted after the camera is started. It is frequently used for burst photographing.

Development Overview

You need to install DevEcho studio IDE and I assume that you have prior knowledge about the Harmony OS and java.

Hardware Requirements

  • A computer (desktop or laptop) running Windows 10.

Software Requirements

  • Java JDK installation package.

Follows the steps.

  1. Create Harmony OS Project.
  • Open DevEcho studio.
  • Enter Project Name and Package Name and click on Finish.

2. Once you have created the project, DevEco Studio will automatically sync it with Gradle files. Find the below image after synchronization is successful.

3. Add the below maven URL in build.gradle(Project level) file under the repositories of buildscript, dependencies, for more information refer Add Configuration.

4. Update Permission and app version in config.json file as per your requirement, otherwise retain the default values.

5. Create New > Ability, as follows.

6. Development Procedure.

Create MainAbility.java ability and add the below code.

Create TakePhotoAbility.java ability and add the below code.

Create VideoRecordAbility.java ability and add the below code.

Create ability_main.xml layout and add the below code.

Create main_camera_slice.xml in graphic folder and add the below code

7. To build apk and run in device, choose Build > Generate Key and CSR Build for Hap(s)\ APP(s) or Build and Run into connected device, follow the steps.

Result

  1. Run Application on connected device, we can see below result.

2. Click on button, one by one see result as per below screen.

Tips and Tricks

  • Always use the latest version of DevEcho Studio.

Conclusion

In this article, we have learnt Camera service in Harmony OS. We can access and operate the camera device and develop new functions through the open APIs. Common operations include preview, photographing, burst photographing, and video recording.

Thanks for reading the article, please do like and comment your queries or suggestions.

References

Harmony OS: https://www.harmonyos.com/en/develop/?ha_source=hms1

Camera Overview: https://developer.harmonyos.com/en/docs/documentation/doc-guides/media-camera-overview-0000000000031783?ha_source=hms1