Resource Base
Table of contents

CameraEnhancer - Frame Acquisition

Name Description
fetchImage Fetches the current frame from the camera’s video feed.
getColourChannelUsageType Retrieves the current usage type for color channels in images.
getImage Retrieves a buffered image, of type DSImageData.
getPixelFormat Retrieves the current pixel format used for images fetched from the camera.
getScanRegion Retrieves the current scan region set within the camera’s view.
hasNextImageToFetch Determines whether there are more images available to fetch.
isBufferEmpty Determines whether the buffer is currently empty.
setColourChannelUsageType Sets the usage type for color channels in images.
setPixelFormat Sets the pixel format for the images fetched from the camera.
setScanRegion Sets the scan region within the camera’s view which limits the frame acquisition to a specific area of the video feed.
singleFrameMode Controls the single-frame mode of the CameraEnhancer.
takePhoto Initiates a sequence to capture a single frame/image, halting the video stream temporarily.

fetchImage

Fetches the current frame from the camera’s video feed.

This method is used to obtain the latest image captured by the camera.

fetchImage(): DCEFrame;

Parameters

None.

Return value

A DCEFrame object representing the current frame. The structure and content of this object will depend on the pixel format set by setPixelFormat() and other settings.

Code Snippet

let image = cameraEnhancer.fetchImage();
document.body.appendChild(image.toCanvas());

See also

DCEFrame

getColourChannelUsageType

Retrieves the current usage type for color channels in images.

getColourChannelUsageType(): EnumColourChannelUsageType;

Parameters

None.

Return value

The current color channel usage type.

Code Snippet

cameraEnhancer.getColourChannelUsageType();

See also

EnumColourChannelUsageType

getImage

Retrieves a buffered image, of type DSImageData.

This function retrieves the latest image added to the buffer, and removes it from the buffer in the process.

getImage(): DCEFrame;

Parameters

None.

Return value

A DSImageData object retrieved from the buffer which contains the image data of the frame and related information.

Code Snippet

let image = cameraEnhancer.getImage();
document.body.appendChild(image.toCanvas());

See also

DCEFrame

getPixelFormat

Retrieves the current pixel format used for images fetched from the camera.

getPixelFormat(): EnumImagePixelFormat;

Parameters

None.

Return value

The current pixel format, which could be one of the following: IPF_GRAYSCALED, IPF_ABGR_8888, and IPF_ARGB_8888.

Code Snippet

let pixelFormat = cameraEnhancer.getPixelFormat();
console.log(pixelFormat);

getScanRegion

Retrieves the current scan region set within the camera’s view.

Note: If no scan region has been explicitly set before calling this method, an error may be thrown, indicating the necessity to define a scan region beforehand.

getScanRegion(): Rect | DSRect;

Parameters

None.

Return value

A DSRect or Rect object representing the current scan region.

Code Snippet

let region = cameraEnhancer.getScanRegion();

See also

Rect

DSRect

hasNextImageToFetch

Determines whether there are more images available to fetch.

hasNextImageToFetch(): boolean;

Parameters

None.

Return value

Boolean indicating whether more images can be fetched. false means the image source is closed or exhausted.

Code Snippet

if(!cameraEnhancer.hasNextImageToFetch()) {
    console.log("The image source is exhausted!");
}

isBufferEmpty

Determines whether the buffer is currently empty.

isBufferEmpty(): boolean;

Parameters

None.

Return value

Boolean indicating whether the buffer is empty.

Code Snippet

if(cameraEnhancer.isBufferEmpty()) {
    console.log("There is no image in buffer!");
}

setColourChannelUsageType

Sets the usage type for color channels in images.

setColourChannelUsageType(type: EnumColourChannelUsageType): void;

Parameters

type: one of the types defined in EnumColourChannelUsageType, specifying how color channels should be used.

Return value

None.

Code Snippet

cameraEnhancer.setColourChannelUsageType(Dynamsoft.Core.EnumColourChannelUsageType.CCUT_FULL_CHANNEL);

See also

EnumColourChannelUsageType

setPixelFormat

Sets the pixel format for the images fetched from the camera, which determines the format of the images added to the buffer when the fetchImage() or startFetching() method is called.

setPixelFormat(pixelFormat: EnumImagePixelFormat.IPF_GRAYSCALED
                | EnumImagePixelFormat.IPF_ABGR_8888
                | EnumImagePixelFormat.IPF_ARGB_8888): void;

Parameters

pixelFormat: the desired pixel format for the images. Supported formats include IPF_GRAYSCALED, IPF_ABGR_8888, and IPF_ARGB_8888.

Return value

None.

Code Snippet

cameraEnhancer.setPixelFormat(Dynamsoft.Core.EnumImagePixelFormat.IPF_ARGB_8888);
let image = cameraEnhancer.getImage();
document.body.appendChild(image.toCanvas());

See also

EnumImagePixelFormat

setScanRegion

Sets the scan region within the camera’s view which limits the frame acquisition to a specific area of the video feed.

Note: The region is always specified relative to the original video size, regardless of any transformations or zoom applied to the video display.

setScanRegion(region?: Rect | DSRect): void;

Parameters

region: specifies the scan region.

Return value

None.

Code Snippet

let scanRegion = {
    x: 25,
    y: 25,
    width: 50,
    height: 50,
    isMeasuredInPercentage: true
};
cameraEnhancer.setScanRegion(scanRegion); 
//...
cameraEnhancer.setScanRegion(null); //Cancel the scan region.

See also

Rect

DSRect

singleFrameMode

Controls the single-frame mode of the CameraEnhancer.

This mode is designed for scenarios where live camera feed is either not required, not supported by the browser, or when processing static images is preferred. Depending on the setting, it can alter the behavior of how images are sourced for processing.

  • “disabled”: The CameraEnhancer operates in its default mode, attempting to stream live video from the camera. This is the standard behavior in environments where camera access is supported and permitted.
  • “image”: The user is prompted to select a local image file for processing. This mode is useful for applications that need to process a single image instead of a continuous video stream. It can be used in any environment, regardless of camera support.
  • “camera”: The behavior varies based on the device type:
    • On desktop platforms, it prompts the user to select a local image, similar to the “image” mode.
    • On mobile devices, it invokes the system camera application, allowing the user to capture a new photo for processing. This leverages the device’s camera in environments where direct camera streaming through the browser might not be available or preferred.

This property provides flexibility in handling different operational contexts, making it easier to adapt to varying application requirements and user environments.

singleFrameMode: "disabled" | "camera" | "image";

Code Snippet

(async () => {
    let cameraEnhancer = await Dynamsoft.DCE.CameraEnhancer.createInstance();
    cameraEnhancer.on('singleFrameAcquired', frameData => {
        document.body.appendChild(frameData.toCanvas());
    });
    cameraEnhancer.singleFrameMode = "image";
    await cameraEnhancer.open();
})();

As shown in the code snippet above, singleFrameMode should be set before calling open().

takePhoto

Initiates a sequence to capture a single frame/image, halting the video stream temporarily.

This method prompts the user to either select a local image or capture a new one using the system camera, similar to the behavior in singleFrameMode but without changing the mode.

Note: This method is intended for use cases where capturing a single, user-obtained image is necessary while the application is otherwise utilizing a live video stream.

Steps performed by takePhoto:

  1. Stops the video stream and releases the camera, if it was in use.
  2. Prompts the user to select an image from their gallery or disk, or to take a new image with the system camera. This behavior mirrors that of singleFrameMode.
  3. Places the obtained image into the buffer, differing from singleFrameMode which would display the image in the view.
  4. Attempts to resume the video stream after the image has been obtained.
takePhoto(listener: (dceFrame: DCEFrame) => void): void;

Parameter

listener: A callback function that is invoked with a DCEFrame object containing the obtained image.

Return Value

None.

Code Snippet

function handleCapturedPhoto(dceFrame) {
  console.log('Captured Photo Data:', dceFrame);
  // Process the captured photo data as needed.
}
// Capture a photo and invoke the listener with the captured data.
cameraEnhancer.takePhoto(handleCapturedPhoto);

This page is compatible for: