Capture Media
This documentation provides guidelines on capturing images and recording videos using the TruVideo Camera SDK. The SDK offers simple methods to integrate camera functionalities into your application.
Prerequisites
Ensure you have integrated the TruVideo Core SDK into your project and completed with the authentication process
Capturing an Image
To capture an image, follow these steps:
Step 1: Create new configuration to give to the camera
const cameraConfiguration : CameraConfiguration = {
lensFacing: LensFacing?.Front,
//lensFacing: LensFacing?.Back,
flashMode: FlashMode?.Off,
//flashMode: FlashMode?.On
orientation: Orientation?.Portrait,
//orientation: Orientation?.LandscapeLeft,
//orientation: Orientation?.LandscapeRight,
//orientation: Orientation?.Portrai
outputPath: "",
frontResolutions: [],
frontResolution: null,
backResolutions: [],
backResolution: null,
mode: CameraMode.videoAndImage()
//mode: CameraMode.image()
//mode: CameraMode.video()
imageFormat : ImageFormat.JPEG
}
Step 2 : Present the camera with the configuration
For Type Checked Camera Response new method introduces initCameraScreenTS
// For JSON value
try {
const response = await initCameraScreen(cameraConfiguration);
const resultData = response.value;
} catch (error) {
console.error("Camera error:", error);
}
// For Type checked value
try {
const response: CameraResult[] = await initCameraScreenTS(cameraConfiguration);
} catch (error) {
console.error("Camera error:", error);
}
Step 4 : Format the response
let mediaItems: any[] = [];
if (typeof resultData === "string") {
try {
const parsed = JSON.parse(resultData);
// get result in parsed form
mediaItems = Array.isArray(parsed) ? parsed : [];
} catch (error) {
console.error("[openCamera] Failed to parse resultData:", error);
return;
}
}
if (!Array.isArray(mediaItems)) {
// exception case in which result is not proper
console.error("[openCamera] Error: mediaItems is not an array:", mediaItems);
return;
}
Finalize :
After completing this steps the function should look like
import { LensFacing,
FlashMode,
Orientation,
CameraMode,
initCameraScreen,
type CameraConfiguration
} from '@trunpm/truvideo-capacitor-camera-sdk';
const cameraConfiguration : CameraConfiguration = {
lensFacing: LensFacing?.Front || "front",
//lensFacing: LensFacing?.Back,
flashMode: FlashMode?.Off || "off",
//flashMode: FlashMode?.On
orientation: Orientation?.Portrait || "portrait",
//orientation: Orientation?.LandscapeLeft,
//orientation: Orientation?.LandscapeRight,
//orientation: Orientation?.Portrai
outputPath: "",
frontResolutions: [],
frontResolution: null,
backResolutions: [],
backResolution: null,
mode: CameraMode.videoAndImage()
//mode: CameraMode.image()
//mode: CameraMode.video()
};
async function openCamera() {
try {
// cameraConfiguration will be the formattedcameraConfiguration from above
const response = await initCameraScreen(cameraConfiguration);
const resultData = response.value;
// getting result
let mediaItems = [];
if (typeof resultData === "string") {
try {
const parsed = JSON.parse(resultData);
// get result in parsed form
mediaItems = Array.isArray(parsed) ? parsed : [];
} catch (error) {
console.error("Failed to parse camera response:", error);
}
} else if (Array.isArray(resultData)) {
mediaItems = resultData;
}
if (!Array.isArray(mediaItems)) {
console.error("Camera Upload Failed: mediaItems is not an array.");
return;
}
} catch (error) {
console.error("Camera error:", error);
}
}
Media Item Object
The result contains an array of MediaItem
objects, where each MediaItem
represents a media file (e.g., video) captured or stored by the application. Below is a description of the properties included in each MediaItem
object.
id
Number
A unique identifier for the media.
filePath
String
The file path where the media item is stored on the device.
duration
Number
The duration of the media item in milliseconds (0 for images).
orientation
String
The orientation of the media item (PORTRAIT or LANDSCAPE).
lensFacing
String
The camera lens used to capture the media (BACK or FRONT).
resolution
Object
An object containing the dimensions of the media item (width and height).
createdAt
Number
The timestamp (in milliseconds) when the media item was created.
What’s Next?
Now that you can capture media, you’re ready to:
By following this guide, you’ve enabled rich media capture in your iOS app with just a few lines of code. Let’s keep going!
Last updated
Was this helpful?