Actions
Open Camera

openCamera

OpenCamera action enables users to access their device's camera, capturing images or videos directly from the app, enhancing interactivity and enabling seamless integration of media functionalities within the application. With id property we can bind results of camera. Then use the onComplete properties to execute other actions after capture or selection is done.

Properties

PropertyTypeDescription
idstringGive the camera an ID, allows you to bind to its result. e.g. ${cameraId.files...}
onCompleteactionExecute an Action after completing capturing media
onCloseactionExecute an Action on camera close
onCaptureactionExecute an Action on each capture
optionsobjectdifferent options to chose for openCamera action. see properties
overlayWidgetwidgetCustom overlay Widget to display over camera.
loadingWidgetwidgetCustom widget to show for loading indicator in camera.

properties.options

PropertyTypeDescription
modestringModes of camera. It can be photo only i.e allows to capture just photo. Similarly video or can be both. photo video both
initialCamerastringInitialize either camera, back or front. back front
allowGalleryPickerbooleanAllow users to pick media from gallery. Default (true).
allowCameraRotatebooleanAllow users rotate camera i.e back and front. Default (true).
allowFlashControlbooleanAllow users to control flash options. Default (true).
previewbooleanIf set true, users can view captured/selected media.
maxCountnumberIt used to control number of media that can be captured/selected
maxCountMessagestringCustom message to show when captured/selected media is greater than maxCount
minCountnumberIt used to control number of media that can be captured/selected
minCountMessagestringCustom message to show when captured/selected media is greater than minCount
permissionDeniedMessagestringSet custom message when access to camera is denied
nextButtonLabelstringSet custom label on next button.
cameraRotateIconwidgetSet custom icon for camera rotate button. see here
galleryPickerIconwidgetSet custom icon for gallery picker button. see here
focusIconwidgetSet custom icon for focus node.
assistAngleobjectShow assist message whenever angle goes below minAngle or above minAngle. see properties
assistSpeedobjectShow assist message whenever camera is moving faster than maxSpeed. see properties
autoCaptureIntervalintegerIf set any number n, on each n interval camera will capture
captureOverlaybooleanIf set picture will be cropped according to overlay widget
faceDetectionobjectEnable face detection. see properties
Values for options.assistAngle
PropertyTypeDescription
minAnglenumberMinimum angle
maxAnglenumberMaximum Angle
maxAngleassistAngleMessagenumberCustom message to show when condition is hit.
Values for options.assistSpeed
PropertyTypeDescription
maxSpeednumberMaximum speed in km/hr.
assistSpeedMessagenumberCustom message to show when condition is hit.
Values for options.faceDetection
PropertyTypeDescription
enabledbooleanEnable face detection.
messagestringuse this pass a message above the camera
messageStyleobjectstyle applied to the message widget
showControlsbooleanset false to hide all controls
showCaptureControlbooleanset false to hide capture control icon
showFlashControlbooleanset false to hide flash control control icon
showCameraLensControlbooleanset false to hide camera lens control icon
indicatorShapestringuse this to change the shape of the face indicator circle, square
autoDisableCaptureControlbooleanset true to disable capture control widget when no face is detected
autoCapturebooleanset true to capture image on face detected
imageResolutionstringuse this to set image resolution low, medium, high
defaultFlashModestringuse this to set initial flash mode off, auto, always
performanceModestringUse this to set your preferred performance mode. accurate, fast
accuracyConfigobjectUse this to set accuracy config for face detection. Accuracy config is only supported on web. see properties
Values for options.faceDetection.accuracyConfig

[Note] Accuracy config is only supported on web.

PropertyTypeDescription
detectionThresholdnumberMinimum confidence score required to consider a face detection valid.
intersectionRatioThresholdnumberMinimum allowed overlap ratio between the detected face and expected region.
extraHeightFactornumberAdditional height factor added to the face bounding box.
inputSizenumberSize of the input image used for face detection.
landmarkRationumberMinimum acceptable alignment accuracy for facial landmarks.
frameMarginnumberMargin ratio to ensure face is not too close to frame edges.
tiltAngleThresholdnumberMaximum allowed tilt angle of the detected face (in degrees).
horizontalCenterTolerancenumberAllowed tolerance for how centered the face must be horizontally.
earThresholdnumberMinimum Eye Aspect Ratio (EAR) to detect open eyes.
minFaceWidthRationumberMinimum ratio of face width relative to the frame.
maxFaceWidthRationumberMaximum ratio of face width relative to the frame.
qualityPassThresholdnumberMinimum quality score required for a face to pass detection.
yawLowerThresholdnumberLower bound of acceptable yaw (left-right head rotation) ratio.
yawUpperThresholdnumberUpper bound of acceptable yaw (left-right head rotation) ratio.

Usage Examples

View:
  header:
    title: "Action: openCamera"
  styles:
    scrollableView: true
  body:
    Column:
      styles: { gap: 16, padding: 24 }
      children:
        - Button:
            label: Open Camera
            onTap:
              openCamera:
                id: cameraId
 
                options:
                  mode: photo
 
        - Conditional:
            conditions:
              - if: ${cameraId.files.length > 0}
                Carousel:
                  item-template:
                    data: ${cameraId.files}
                    name: file
                    template:
                      Image:
                        source: ${file.path}

openCamera also comes with options

- Button:
    label: Camera with options
    onTap:
      openCamera:
        id: cameraId1
        options:
          allowGalleryPicker: true
          allowCameraRotate: true
          allowFlashControl: true
          preview: true
          maxCount: 2
          mode: photo
 
- Conditional:
    conditions:
      - if: ${cameraId1.files.length > 0}
        Carousel:
          item-template:
            data: ${cameraId1.files}
            name: file
            template:
              Image:
                source: ${file.path}

openCamera also comes with advance options

  • assistAngle, show message when phone angle goes below min angle or goes beyond max angle.
  • assistSpeed, show message when phone goes beyond max speed.
  • minCount/maxCount, type is a number and can also be set to minCount: ${ensemble.storage.xyz} to get a dynamic value
- Button:
    label: Camera with advance options.
    onTap:
      openCamera:
        options:
          allowGalleryPicker: true
          allowCameraRotate: true
          allowFlashControl: true
          preview: true
 
          assistAngle:
            minAngle: 80
            maxAngle: 100
            assistAngleMessage: Please try to keep angle approx. 90 degree.
 
          assistSpeed:
            maxSpeed: 10
            assistSpeedMessage: Please try to speed below 10 km/hr.

openCamera also comes with custom overlay widget

  • captureOverlay, crop image according to overlay widget.
  • height/width, required to specify the crop area of widget.
- Button:
    label: Camera with overlay widget.
    onTap:
      openCamera:
        options:
          captureOverlay: true
        
        loadingWidget:
          Progress:
            display: circular
            id: loading
            visible: false 
        
        overlayWidget:
          Column:
            styles:
              height: 170
              width: 260
            children:
              - Image:
                  source: https://i.imgur.com/rEYx444.png

You can capture and upload to specified API

        - Button:
            label: Capture and upload
            onTap:
              openCamera:
                id: captureMedia
                onComplete:
                  uploadFiles:
                    id: uploader
                    files: ${captureMedia.files}
                    uploadApi: fileUploadApi
                    fieldName: files
                    inputs:
                      url: <specify your url>
 
 
        - Markdown:
            text: ${uploader.body}
 
API:
  fileUploadApi:
    inputs:
      - url
    uri: ${url}
    method: POST

You can clear previous camera results while recapturing using cameraId.clear()

- Button:
    label: Clear Previous Result and Capture
    onTap:
      executeCode:
        body: |
          captureLatest.clear()
        onComplete:
          openCamera:
            id: captureLatest

Capture image on face detected

- Button:
    label: Open Camera
    onTap:
      openCamera:
        id: cameraWithFaceDetection
        options:
          initialCamera: front
          faceDetection:
            enabled: true
            autoCapture: false
            performanceMode: accurate
            accuracyConfig: # accuracyConfig is only supported on web
              detectionThreshold: 0.5
              intersectionRatioThreshold: 0.9
              extraHeightFactor: 0.6
              inputSize: 224
              landmarkRatio: 0.95
              frameMargin: 0.05
              tiltAngleThreshold: 6
              horizontalCenterTolerance: 0.08
              earThreshold: 0.25
              minFaceWidthRatio: 0.18
              maxFaceWidthRatio: 0.82
              qualityPassThreshold: 0.8
              yawLowerThreshold: 0.85
              yawUpperThreshold: 1.15
            message: "Align your face in the square"
            messageStyle:
              color: "#FF0000"
              fontSize: 20
        onCapture:
          uploadFiles:
            id: uploader
            files: ${cameraWithFaceDetection.files[0]}
            uploadApi: fileUploadApi
            fieldName: file
            onComplete:
              showDialog:
                body:
                  Column:
                    children:
                      - TextInput:
                          value: ${cameraWithFaceDetection.files[0]}
                      - Image:
                          source: ${cameraWithFaceDetection.files[0].path}

To learn more about openCamera functionalities, test it out here in Ensemble Kitchen Sink (opens in a new tab) app.