import {ImageSourceAdapter, type DSRect} from "../core"; import {NativeModules, Platform} from "react-native"; import {CameraView} from "./CameraView" import {EnumCameraPosition} from "./EnumCameraPosition"; import type {EnumFocusMode} from "./EnumFocusMode"; import type {EnumEnhancedFeatures} from "./EnumEnhancedFeatures"; import type {EnumResolution} from "./EnumResolution"; import type {Rect} from "../core/Rect"; import type {Point} from "../core/Point"; // @ts-ignore Check whether __turboModuleProxy exists, it may not const isTurboModuleEnabled = global.__turboModuleProxy != null; const DynamsoftCameraEnhancerModule = isTurboModuleEnabled ? require("./NativeDynamsoftCameraViewModule").default : NativeModules.DynamsoftCameraView; /** * The singleton instance of CameraEnhancer. * */ let dce: CameraEnhancer | null; /** * The CameraEnhancer class is the primary class of Dynamsoft Camera Enhancer that defines the camera controlling APIs. * It is a subclass of {@link ImageSourceAdapter}. *
* In js end of react-native, CameraEnhancer uses singleton mode. You can only get CaptureVisionRouter instance by {@link CameraEnhancer.getInstance}. * @hideconstructor * */ export class CameraEnhancer extends ImageSourceAdapter { private constructor() { super(); } protected init() { this.isaId = DynamsoftCameraEnhancerModule.createInstance() } /**Trigger to request the camera permission.*/ static async requestCameraPermission() { if (Platform.OS === "android") { DynamsoftCameraEnhancerModule.requestCameraPermission() } } /** * Get the singleton instance of CameraEnhancer. *
* This method ensures that only one instance of CameraEnhancer is created * and reused throughout the application. If an instance already exists, it will * return the existing one. Otherwise, it will create a new instance and return it. * * @returns {CameraEnhancer} The singleton instance of CameraEnhancer. * */ static getInstance(): CameraEnhancer { if (dce) { return dce; } return dce = new CameraEnhancer(); } /** * Destroys the CameraEnhancer instance and releases all associated resources. * After calling this method, the instance will be set to null and cannot be used anymore. * Only call this method when you want to completely dispose the CameraEnhancer instance, otherwise just call {@link CameraEnhancer.close} to stop the camera and release the video stream. */ dispose() { DynamsoftCameraEnhancerModule.destroyInstance(this.isaId); dce = null; } /**Opens the currently selected camera and starts the video stream.*/ open() { DynamsoftCameraEnhancerModule.open(); } /**Closes the currently active camera and stops the video stream.*/ close() { DynamsoftCameraEnhancerModule.close(); } /** * Bind a CameraView instance with this CameraEnhancer instance. * *
Code Snippet:
* *``` * function App(): React.JSX.Element { * const cameraView = useRefCode Snippet:
* *``` * cameraEnhancer.enableEnhancedFeatures(EnumEnhancedFeatures.EF_AUTO_ZOOM | EnumEnhancedFeatures.EF_FRAME_FILTER) * ``` * @param features - An enum value or a bitwise combination of EnumEnhancedFeatures which indicates a series of enhanced features. * @see {@link EnumEnhancedFeatures} * */ enableEnhancedFeatures(features: EnumEnhancedFeatures | number) { DynamsoftCameraEnhancerModule.enableEnhancedFeatures(features); } /** * Disables one or more previously enabled enhanced features. *Code Snippet:
* *``` * cameraEnhancer.disableEnhancedFeatures(EnumEnhancedFeatures.EF_AUTO_ZOOM | EnumEnhancedFeatures.EF_FRAME_FILTER) * ``` * @param features - An enum value or a bitwise combination of EnumEnhancedFeatures indicating the features to be disabled. * @see {@link EnumEnhancedFeatures} * */ disableEnhancedFeatures(features: EnumEnhancedFeatures | number) { DynamsoftCameraEnhancerModule.disableEnhancedFeatures(features); } /** * Sets the scan region within the camera's view which limits the frame acquisition to a specific area of the video feed. *Code Snippet:
* *``` * let scanRegion = { * top: 0.25, * bottom: 0.75, * left: 0.25, * right: 0.75, * measuredInPercentage: true, * } * cameraEnhancer.setScanRegion(scanRegion); * //... * cameraEnhancer.setScanRegion(null); //Cancel the scan region * ``` * * @param region - specifies the scan region. If null or undefined, cancel the scan region. * @remarks The region is always specified relative to the original video size, regardless of any transformations or zoom applied to the video display. * @see {@link DSRect} * */ setScanRegion(region: DSRect | null | undefined) { DynamsoftCameraEnhancerModule.setScanRegion(region); } /** * Retrieves the current scan region. * @return {Promise