In this tutorial, we will walk through the steps to use the @capgo/camera-preview
package in your Capacitor project. This package allows you to interact with the camera from your JavaScript and HTML code.
To install the @capgo/camera-preview
package, open your terminal and run one of the following commands:
yarn add @capgo/camera-preview
or
npm install @capgo/camera-preview
After the installation is complete, run the following command to sync your Capacitor project:
npx cap sync
If you are using Android, you need to make some additional changes to your project. Open the android/app/src/main/AndroidManifest.xml
file and add the following line above the closing </application>
tag to request the CAMERA permission:
<uses-permission android:name="android.permission.CAMERA" />
For more help, refer to the Capacitor documentation.
If you are using iOS, you need to add two permissions to your Info.plist
file. Follow the Capacitor documentation and add the NSCameraUsageDescription
and NSMicrophoneUsageDescription
permissions. The NSMicrophoneUsageDescription
permission is only required if you will be using audio. If you don't need audio, you can set the disableAudio
option to true
to disable the microphone permission request.
If you are using the web platform with Ionic, add the following line to your entry script in app.module.ts
:
import '@capgo/camera-preview';
This will allow Capacitor to register the web platform from the plugin.
The @capgo/camera-preview
package provides the following API methods:
Starts the camera preview instance.
Stops the camera preview instance.
Captures a picture from the camera.
Captures a sample image.
Gets the supported flash modes.
Gets the horizontal field of view.
Sets the flash mode.
Flips the camera.
Sets the camera opacity.
Stops recording a video.
Starts recording a video.
For more details on the parameters and return values of these methods, refer to the @capgo/camera-preview
package documentation.
In this tutorial, we learned how to install and use the @capgo/camera-preview
package in a Capacitor project. We explored the available API methods and their usage. Now you can integrate camera functionality into your application using this package.