# ImageIO

The `ImageIO` class handles image reading and writing (from/to files and memory).

## Definition

*Package:* com.dynamsoft.utility

```java
public class ImageIO
```

## Methods

| Method               | Description |
|----------------------|-------------|
| [`ImageIO`](#imageio) | Initializes a new instance of the `ImageIO` class. |
| [`readFromFile`](#readfromfile) | Reads an image from a file. |
| [`readFromMemory`](#readfrommemory) | Reads an image from a file in memory. |
| [`readFromBase64String`](#readfrombase64string) | Reads an image from a Base64 string. |
| [`saveToFile`](#savetofile) | Saves an image to a file. |
| [`saveToMemory`](#savetomemory) | Saves an image to a file in memory. |
| [`saveToBase64String`](#savetobase64string) | Saves an image to a Base64 string. |

### ImageIO

Initializes a new instance of the `ImageIO` class.

```java
ImageIO()
```

### readFromFile

Reads an image from a file.

```java
ImageData readFromFile(String filePath) throws UtilityException
```

**Parameters**

`filePath` The path of the image file.

**Return value**

Returns an `ImageData` object representing the image.

**Exception**

[UtilityException](https://www.dynamsoft.com/capture-vision/docs/server/programming/java/api-reference/utility/utility-exception.md)

**Remarks**

If the file format is gif, pdf or tiff, we read the first page of the image file.

**See Also**

[ImageData](https://www.dynamsoft.com/capture-vision/docs/server/programming/java/api-reference/core/basic-classes/image-data.md)

### readFromMemory

Reads an image from a file in memory.

```java
ImageData readFromMemory(byte[] fileBytes) throws UtilityException
```

**Parameters**

`fileBytes` A byte array representing the image file in memory.

**Return value**

Returns an `ImageData` object representing the image.

**Exception**

[UtilityException](https://www.dynamsoft.com/capture-vision/docs/server/programming/java/api-reference/utility/utility-exception.md)

**Remarks**

If the file format is gif, pdf or tiff, we read the first page of the image file.

**See Also**

[ImageData](https://www.dynamsoft.com/capture-vision/docs/server/programming/java/api-reference/core/basic-classes/image-data.md)

### readFromBase64String

Reads an image from a Base64 string.

```java
ImageData readFromBase64String(String base64String) throws UtilityException
```

**Parameters**

`base64String` A Base64 encoded string representing the image.

**Return value**

Returns an `ImageData` object representing the image.

**Exception**

[UtilityException](https://www.dynamsoft.com/capture-vision/docs/server/programming/java/api-reference/utility/utility-exception.md)

**See Also**

[ImageData](https://www.dynamsoft.com/capture-vision/docs/server/programming/java/api-reference/core/basic-classes/image-data.md)

### saveToFile

Saves an image to a file in JPEG, PNG, BMP or PDF format.

```java
void saveToFile(ImageData imageData, String path) throws UtilityException
void saveToFile(ImageData imageData, String path, boolean overwrite) throws UtilityException
```

**Parameters**

`imageData` The image data to be saved.

`path` The targeting file path with the file name and extension name.

`overwrite` A flag indicating whether to overwrite the file if it already exists. Defaults to true.

**Exception**

[UtilityException](https://www.dynamsoft.com/capture-vision/docs/server/programming/java/api-reference/utility/utility-exception.md)

**See Also**

[ImageData](https://www.dynamsoft.com/capture-vision/docs/server/programming/java/api-reference/core/basic-classes/image-data.md)

### saveToMemory

Saves an image to a file in memory.

```java
byte[] saveToMemory(ImageData imageData, @EnumImageFileFormat int imageFormat) throws UtilityException
```

**Parameters**

`imageData` The image data to be saved.

`imageFormat` The image file format to be saved.

**Return value**

Returns the byte array representing the saved image file.

**Exception**

[UtilityException](https://www.dynamsoft.com/capture-vision/docs/server/programming/java/api-reference/utility/utility-exception.md)

**See Also**

[ImageData](https://www.dynamsoft.com/capture-vision/docs/server/programming/java/api-reference/core/basic-classes/image-data.md)

[EnumImageFileFormat](https://www.dynamsoft.com/capture-vision/docs/server/programming/java/api-reference/core/enum-image-file-format.md)

### saveToBase64String

Saves an image to a Base64 string.

```java
String saveToBase64String(ImageData imageData, @EnumImageFileFormat int imageFormat) throws UtilityException
```

**Parameters**

`imageData` The image data to be saved.

`imageFormat` The image file format to be saved.

**Return value**

Returns a Base64 encoded string representing the saved image.

**Exception**

[UtilityException](https://www.dynamsoft.com/capture-vision/docs/server/programming/java/api-reference/utility/utility-exception.md)

**See Also**

[ImageData](https://www.dynamsoft.com/capture-vision/docs/server/programming/java/api-reference/core/basic-classes/image-data.md)

[EnumImageFileFormat](https://www.dynamsoft.com/capture-vision/docs/server/programming/java/api-reference/core/enum-image-file-format.md)
