Skip to content

FileStorage

Core and matricesclassOpenCV 5.0.0
import { FileStorage } from '@banou/opencv-wasm'

Use after await initOpenCV(). See the initialization and named imports guide.

ARGUMENTSConstructor or factory
CLASSFileStorage
RETURN TYPEOwned native handle
Call structure. A void return can still write to destination arguments. The parameter descriptions define inputs, outputs and ownership.

Native object: release it with using or delete(). Factories can return null; check before calling methods.

XML/YAML/JSON file storage class that encapsulates all the information necessary for writing or reading data to/from a file.

Constructors and members

static new

The constructors.

 The full constructor opens the file. Alternatively you can use the default constructor and then
 call FileStorage::open.
new(filename: EmbindString, flags: number, encoding: EmbindString): FileStorage;
3 available overloads
new(): FileStorage;
new(filename: EmbindString, flags: number): FileStorage;
new(filename: EmbindString, flags: number, encoding: EmbindString): FileStorage;
filename

Name of the file to open or the text string to read the data from. Extension of the file (.xml, .yml/.yaml or .json) determines its format (XML, YAML or JSON respectively). Also you can append .gz to work with compressed files, for example myHugeMatrix.xml.gz. You can also specify a compression level from 0 to 9 by appending it to the extension (e.g. ".gz0" for no compression, ".gz9" for high compression). The last digit will be truncated internally to write/read. (e.g. If "a.xml.gz9" is specified, "a.xml.gz" is used for the actual file name.) If both FileStorage::WRITE and FileStorage::MEMORY flags are specified, source is used just to specify the output file format (e.g. mydata.xml, .yml etc.). A file name can also contain parameters. You can use this format, "*?base64" (e.g. "file.json?base64" (case sensitive)), as an alternative to FileStorage::BASE64 flag.

flags

Mode of operation. One of FileStorage::Mode

encoding

Encoding of the file. Note that UTF-16 XML encoding is not supported currently and you should use 8-bit encoding instead of it.()

Returns

The FileStorage result.

clone

Create another handle to the same native object. This retains the object without copying its pixels or algorithm state; dispose both handles separately.

clone(): this;
Returns

The this result.

open

Opens a file.

 See description of parameters in FileStorage::FileStorage. The method calls FileStorage::release
 before opening the file.
open(filename: EmbindString, flags: number, encoding: EmbindString): boolean;
2 available overloads
open(filename: EmbindString, flags: number): boolean;
open(filename: EmbindString, flags: number, encoding: EmbindString): boolean;
filename

Name of the file to open or the text string to read the data from. Extension of the file (.xml, .yml/.yaml or .json) determines its format (XML, YAML or JSON respectively). Also you can append .gz to work with compressed files, for example myHugeMatrix.xml.gz. You can also specify a compression level from 0 to 9 by appending it to the extension (e.g. ".gz0" for no compression, ".gz9" for high compression). The last digit will be truncated internally to write/read. (e.g. If "a.xml.gz9" is specified, "a.xml.gz" is used for the actual file name.) If both FileStorage::WRITE and FileStorage::MEMORY flags are specified, source is used just to specify the output file format (e.g. mydata.xml, .yml etc.). A file name can also contain parameters. You can use this format, "*?base64" (e.g. "file.json?base64" (case sensitive)), as an alternative to FileStorage::BASE64 flag.

flags

Mode of operation. One of FileStorage::Mode

encoding

Encoding of the file. Note that UTF-16 XML encoding is not supported currently and you should use 8-bit encoding instead of it.

Returns

The boolean result.

isOpened

Checks whether the file is opened.

isOpened(): boolean;
Returns

true if the object is associated with the current file and false otherwise. It is a good practice to call this method after you tried to open a file.

release

Closes the file and releases all the memory buffers.

 Call this method after all I/O operations with the storage are finished.
release(): void;

releaseAndGetString

Closes the file and releases all the memory buffers.

 Call this method after all I/O operations with the storage are finished. If the storage was
 opened for writing data and FileStorage::WRITE was specified
releaseAndGetString(): string;
Returns

The string result.

write

Simplified writing API to use with bindings.

write(name: EmbindString, val: number): void;
name

Name of the written object. When writing to sequences (a.k.a. "arrays"), pass an empty string.

val

Value of the written object.

write1

Simplified writing API to use with bindings.

write1(name: EmbindString, val: boolean): void;
name

Name of the written object. When writing to sequences (a.k.a. "arrays"), pass an empty string.

val

Value of the written object.

write2

Simplified writing API to use with bindings.

write2(name: EmbindString, val: bigint): void;
name

Name of the written object. When writing to sequences (a.k.a. "arrays"), pass an empty string.

val

Value of the written object.

write3

Simplified writing API to use with bindings.

write3(name: EmbindString, val: number): void;
name

Name of the written object. When writing to sequences (a.k.a. "arrays"), pass an empty string.

val

Value of the written object.

write4

Simplified writing API to use with bindings.

write4(name: EmbindString, val: EmbindString): void;
name

Name of the written object. When writing to sequences (a.k.a. "arrays"), pass an empty string.

val

Value of the written object.

write5

Simplified writing API to use with bindings.

write5(name: EmbindString, val: Mat): void;
name

Name of the written object. When writing to sequences (a.k.a. "arrays"), pass an empty string.

val

Value of the written object.

write6

Simplified writing API to use with bindings.

write6(name: EmbindString, val: StringVector): void;
name

Name of the written object. When writing to sequences (a.k.a. "arrays"), pass an empty string.

val

Value of the written object.

writeComment

Writes a comment.

 The function writes a comment into file storage. The comments are skipped when the storage is read.
writeComment(comment: EmbindString, append: boolean): void;
2 available overloads
writeComment(comment: EmbindString): void;
writeComment(comment: EmbindString, append: boolean): void;
comment

The written comment, single-line or multi-line

append

If true, the function tries to put the comment at the end of current line. Else if the comment is multi-line, or if it does not fit at the end of the current line, the comment starts a new line.

startWriteStruct

Starts to write a nested structure (sequence or a mapping).

startWriteStruct(name: EmbindString, flags: number, typeName: EmbindString): void;
2 available overloads
startWriteStruct(name: EmbindString, flags: number): void;
startWriteStruct(name: EmbindString, flags: number, typeName: EmbindString): void;
name

name of the structure. When writing to sequences (a.k.a. "arrays"), pass an empty string.

flags

type of the structure (FileNode::MAP or FileNode::SEQ (both with optional FileNode::FLOW)).

typeName

optional name of the type you store. The effect of setting this depends on the storage format. I.e. if the format has a specification for storing type information, this parameter is used.

endWriteStruct

Finishes writing nested structure (should pair startWriteStruct())

endWriteStruct(): void;

getFormat

Returns the current format.

getFormat(): number;
Returns

The current format, see FileStorage::Mode

getFirstTopLevelNode

Returns the first element of the top-level mapping.

getFirstTopLevelNode(): FileNode;
Returns

The first element of the top-level mapping.

root

Returns the top-level mapping

root(streamidx: number): FileNode;
2 available overloads
root(): FileNode;
root(streamidx: number): FileNode;
streamidx

Zero-based index of the stream. In most cases there is only one stream in the file. However, YAML supports multiple streams and so there can be several.

Returns

The top-level mapping.

getNode

Returns the specified element of the top-level mapping.

getNode(nodename: EmbindString): FileNode;
nodename

Name of the file node.

Returns

Node with the given name.

These signatures describe this package. Upstream documentation can mention optional backends that are absent from this build. Check runtime compatibility before choosing a backend or file format.