![JAR search and dependency download from the Maven repository](/logo.png)
com.github.sarxos.webcam.util.ImageUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of webcam-capture Show documentation
Show all versions of webcam-capture Show documentation
This library allows you to use your PC webcam, IP or network cameras directly from Java. It's compatible with most operating systems (Windows, Linux, MacOS).
package com.github.sarxos.webcam.util;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import com.github.sarxos.webcam.WebcamException;
public class ImageUtils {
/**
* Graphics Interchange Format.
*/
public static final String FORMAT_GIF = "GIF";
/**
* Portable Network Graphic format.
*/
public static final String FORMAT_PNG = "PNG";
/**
* Joint Photographic Experts Group format.
*/
public static final String FORMAT_JPG = "JPG";
/**
* Bitmap image format.
*/
public static final String FORMAT_BMP = "BMP";
/**
* Wireless Application Protocol Bitmap image format.
*/
public static final String FORMAT_WBMP = "WBMP";
/**
* Convert {@link BufferedImage} to byte array.
*
* @param image the image to be converted
* @param format the output image format
* @return New array of bytes
*/
public static byte[] toByteArray(BufferedImage image, String format) {
byte[] bytes = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
ImageIO.write(image, format, baos);
bytes = baos.toByteArray();
} catch (IOException e) {
throw new WebcamException(e);
} finally {
try {
baos.close();
} catch (IOException e) {
throw new WebcamException(e);
}
}
return bytes;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy