All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.softicar.platform.common.io.resource.in.memory.InMemoryImageResource Maven / Gradle / Ivy

Go to download

The SoftiCAR Platform is a lightweight, Java-based library to create interactive business web applications.

There is a newer version: 50.0.0
Show newest version
package com.softicar.platform.common.io.resource.in.memory;

import com.softicar.platform.common.core.exceptions.SofticarIOException;
import com.softicar.platform.common.io.mime.IMimeType;
import com.softicar.platform.common.io.resource.IResource;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Objects;
import javax.imageio.ImageIO;

/**
 * An {@link InMemoryResource} for images.
 *
 * @author Oliver Richers
 */
public class InMemoryImageResource extends InMemoryResource {

	/**
	 * Constructs this {@link InMemoryImageResource} from a
	 * {@link RenderedImage}.
	 *
	 * @param image
	 *            the {@link BufferedImage} (never null)
	 * @param imageFormat
	 *            the image format as given to {@link ImageIO#write} (never
	 *            null)
	 * @param mimeType
	 *            the {@link IMimeType} of this {@link IResource} (never
	 *            null)
	 */
	public InMemoryImageResource(RenderedImage image, String imageFormat, IMimeType mimeType) {

		super(convertImageToByteArray(image, imageFormat), mimeType);
	}

	private static byte[] convertImageToByteArray(RenderedImage image, String imageFormat) {

		Objects.requireNonNull(image);
		Objects.requireNonNull(imageFormat);

		try {
			var buffer = new ByteArrayOutputStream();
			ImageIO.write(image, imageFormat, buffer);
			return buffer.toByteArray();
		} catch (IOException exception) {
			throw new SofticarIOException(exception);
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy