On this page

Camera

Requires:
@ray-js/ray≥0.6.9

Description

System camera component for previewing and capturing.

Props

PropertyTypeRequiredDefaultDescription
mode"normal"NonormalMode; only effective during initialization, cannot be changed dynamically
resolution"low" | "medium" | "high"NomediumResolution, cannot be changed dynamically
devicePosition"front" | "back"NobackCamera facing
flash"auto" | "on" | "off" | "torch"NoautoFlash mode
borderWidthnumberNo0Border width, in px
borderStyle"solid" | "dashed"NosolidBorder style
borderColorstringNo#ffffffBorder color; must be hex format
borderRadiusnumberNo0Border radius, in px
borderRadiusTopLeftnumberNo-Top-left border radius, in px
borderRadiusTopRightnumberNo-Top-right border radius, in px
borderRadiusBottomLeftnumberNo-Bottom-left border radius, in px
borderRadiusBottomRightnumberNo-Bottom-right border radius, in px
backgroundColorstringNo#ffffffBackground color; must be hex format
No-Triggered when the camera terminates abnormally, e.g., when the app goes to the background
No-Triggered when camera access is denied by the user
No-Triggered when the camera is initialized
updateLayoutunknownNo-Used for cross-layer components to notify the container layer to update styles

Referenced Types

interfaceCameraStopEvent
export interface CameraStopEvent extends BaseEvent {
  /** 事件类型 */
  type: 'stop';
}
interfaceBaseEvent
PropertyTypeRequiredDescription
typestringYesEvent type
timeStampnumberYesTime in milliseconds since the page opened
YesSource component of the event
YesCollection of property values for the current component
markanyNoEvent mark data
interfaceTarget
PropertyTypeRequiredDescription
idstringYesid of the event source component
YesCollection of custom attributes from the `dataset` on the event source component

Valid Values

resolution
ValueDescriptionSince
lowLow resolution-
mediumMedium resolution-
highHigh resolution-
devicePosition
ValueDescriptionSince
frontFront camera-
backRear camera-
flash
ValueDescriptionSince
autoAuto flash-
onFlash on-
offFlash off-
torchTorch mode-
borderStyle
ValueDescriptionSince
solidSolid border-
dashedDashed border-

Examples

Basic usage

import React from 'react';
import { Camera, View, Text } from '@ray-js/ray';

export default function () {
  return (
    <View style={{ padding: '20px' }}>
      <Text style={{ marginBottom: '10px' }}>Camera preview</Text>
      <Camera
        style={{ width: '100%', height: '300px' }}
        devicePosition="back"
        flash="auto"
        resolution="high"
        onInitdone={(e) => console.log('Initialized, max zoom:', e.detail.maxZoom)}
        onError={(e) => console.log('Camera error:', e.detail.errMsg)}
        onBindstop={() => console.log('Camera terminated unexpectedly')}
      />
    </View>
  );
}

Custom border

import React, { useState } from 'react';
import { Camera, View, Button } from '@ray-js/ray';

export default function () {
  const [position, setPosition] = useState<'back' | 'front'>('back');

  return (
    <View style={{ padding: '20px' }}>
      <Camera
        style={{ width: '100%', height: '300px' }}
        devicePosition={position}
        flash="off"
        resolution="medium"
        borderWidth={2}
        borderStyle="dashed"
        borderColor="#1890ff"
        borderRadius={16}
        backgroundColor="#000000"
        onInitdone={(e) => console.log('Initialized:', e.detail)}
      />
      <Button
        style={{ marginTop: '10px' }}
        onClick={() => setPosition(position === 'back' ? 'front' : 'back')}
      >
        Switch camera (current: {position === 'back' ? 'Rear' : 'Front'})
      </Button>
    </View>
  );
}

Related API: createCameraContext. This is a native component based on off-screen rendering. Please note the native component usage restrictions.

Scan with SmartLife App to Try
Download Sample Code

FAQ

  1. Only one camera component can be inserted on the same page.
  2. Not supported in the Tuya MiniApp IDE.
  3. For related principles, refer to Native components based on off-screen rendering.
  4. Please note the native component usage restrictions.