use of javax.imageio.ImageWriteParam in project robolectric by robolectric.
the class ImageUtil method writeToStream.
public static boolean writeToStream(Bitmap realBitmap, CompressFormat format, int quality, OutputStream stream) {
if ((quality < 0) || (quality > 100)) {
throw new IllegalArgumentException("Quality out of bounds!");
}
try {
ImageWriter writer = null;
Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName(getFormatName(format));
if (iter.hasNext()) {
writer = iter.next();
}
if (writer == null) {
return false;
}
try (ImageOutputStream ios = ImageIO.createImageOutputStream(stream)) {
writer.setOutput(ios);
ImageWriteParam iwparam = writer.getDefaultWriteParam();
iwparam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
iwparam.setCompressionQuality((quality / 100f));
int width = realBitmap.getWidth();
int height = realBitmap.getHeight();
boolean needAlphaChannel = needAlphaChannel(format);
BufferedImage bufferedImage = Shadows.shadowOf(realBitmap).getBufferedImage();
if (bufferedImage == null) {
bufferedImage = new BufferedImage(realBitmap.getWidth(), realBitmap.getHeight(), BufferedImage.TYPE_INT_ARGB);
}
int outputImageType = getBufferedImageType(realBitmap.getConfig(), needAlphaChannel);
if (outputImageType != BufferedImage.TYPE_INT_ARGB) {
// re-encode image data with a type that is compatible with the output format.
BufferedImage outputBufferedImage = new BufferedImage(width, height, outputImageType);
Graphics2D g = outputBufferedImage.createGraphics();
g.drawImage(bufferedImage, 0, 0, null);
g.dispose();
bufferedImage = outputBufferedImage;
}
writer.write(null, new IIOImage(bufferedImage, null, null), iwparam);
ios.flush();
writer.dispose();
}
} catch (IOException ignore) {
return false;
}
return true;
}
use of javax.imageio.ImageWriteParam in project java-swing-tips by aterai.
the class MainPanel method createAnimatedGifFile.
private static File createAnimatedGifFile(List<Shape> list, Dimension size) throws IOException {
Iterator<ImageWriter> it = ImageIO.getImageWritersByFormatName("gif");
ImageWriter writer = it.hasNext() ? it.next() : null;
if (Objects.isNull(writer)) {
throw new IOException();
}
File file = File.createTempFile("anime", ".gif");
file.deleteOnExit();
try (ImageOutputStream stream = ImageIO.createImageOutputStream(file)) {
writer.setOutput(stream);
writer.prepareWriteSequence(null);
IIOMetadataNode gce = new IIOMetadataNode("GraphicControlExtension");
gce.setAttribute("disposalMethod", "none");
gce.setAttribute("userInputFlag", "FALSE");
gce.setAttribute("transparentColorFlag", "FALSE");
gce.setAttribute("transparentColorIndex", "0");
gce.setAttribute("delayTime", Objects.toString(DELAY));
IIOMetadataNode ae = new IIOMetadataNode("ApplicationExtension");
ae.setAttribute("applicationID", "NETSCAPE");
ae.setAttribute("authenticationCode", "2.0");
// last two bytes is an unsigned short (little endian) that
// indicates the number of times to loop.
// 0 means loop forever.
ae.setUserObject(new byte[] { 0x1, 0x0, 0x0 });
IIOMetadataNode aes = new IIOMetadataNode("ApplicationExtensions");
aes.appendChild(ae);
// Create animated GIF using imageio | Oracle Community
// https://community.oracle.com/thread/1264385
BufferedImage image = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_ARGB);
ImageWriteParam iwp = writer.getDefaultWriteParam();
IIOMetadata metadata = writer.getDefaultImageMetadata(new ImageTypeSpecifier(image), iwp);
String metaFormat = metadata.getNativeMetadataFormatName();
Node root = metadata.getAsTree(metaFormat);
root.appendChild(gce);
root.appendChild(aes);
metadata.setFromTree(metaFormat, root);
// make frame
for (int i = 0; i < list.size() * DELAY; i++) {
paintFrame(image, list);
Collections.rotate(list, 1);
writeToSequence(writer, image, metadata);
metadata = null;
}
writer.endWriteSequence();
}
return file;
}
use of javax.imageio.ImageWriteParam in project openolat by klemens.
the class ImageHelperImpl method getOptimizedImageWriteParam.
private static ImageWriteParam getOptimizedImageWriteParam(ImageWriter writer, Size scaledSize) {
ImageWriteParam iwp = writer.getDefaultWriteParam();
try {
if (iwp.canWriteCompressed()) {
iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
int maxSize = Math.max(scaledSize.getWidth(), scaledSize.getHeight());
if (maxSize <= 50) {
iwp.setCompressionQuality(0.95f);
} else if (maxSize <= 100) {
iwp.setCompressionQuality(0.90f);
} else if (maxSize <= 200) {
iwp.setCompressionQuality(0.85f);
} else if (maxSize <= 500) {
iwp.setCompressionQuality(0.80f);
} else {
iwp.setCompressionQuality(0.75f);
}
}
} catch (Exception e) {
// bmp can be compressed but don't allow it!!!
return writer.getDefaultWriteParam();
}
return iwp;
}
use of javax.imageio.ImageWriteParam in project openolat by klemens.
the class ImageHelperImpl method writeTo.
/**
* Can change this to choose a better compression level as the default
* @param image
* @param scaledImage
* @return
*/
public static boolean writeTo(BufferedImage image, File scaledImage, Size scaledSize, String outputFormat) {
try {
if (!StringHelper.containsNonWhitespace(outputFormat)) {
outputFormat = OUTPUT_FORMAT;
}
Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName(outputFormat);
if (writers.hasNext()) {
ImageWriter writer = writers.next();
ImageWriteParam iwp = getOptimizedImageWriteParam(writer, scaledSize);
IIOImage iiOImage = new IIOImage(image, null, null);
ImageOutputStream iOut = new FileImageOutputStream(scaledImage);
writer.setOutput(iOut);
writer.write(null, iiOImage, iwp);
writer.dispose();
iOut.flush();
iOut.close();
return true;
} else {
return ImageIO.write(image, outputFormat, scaledImage);
}
} catch (IOException e) {
return false;
}
}
use of javax.imageio.ImageWriteParam in project AozoraEpub3 by hmdev.
the class ImageUtils method _writeImage.
/**
* 画像を出力 マージン指定があればカット
* @param margin カットするピクセル数(left, top, right, bottom)
*/
private static void _writeImage(ZipArchiveOutputStream zos, BufferedImage srcImage, String ext, float jpegQuality) throws IOException {
if ("png".equals(ext)) {
/*//PNGEncoder kindlegenでエラーになるのと色が反映されない
PngEncoder pngEncoder = new PngEncoder();
int pngColorType = PngEncoder.COLOR_TRUECOLOR;
switch (srcImage.getType()) {
case BufferedImage.TYPE_BYTE_BINARY:
pngColorType = PngEncoder.COLOR_INDEXED; break;
case BufferedImage.TYPE_BYTE_INDEXED:
pngColorType = PngEncoder.COLOR_INDEXED; break;
case BufferedImage.TYPE_BYTE_GRAY:
pngColorType = PngEncoder.COLOR_GRAYSCALE; break;
}
pngEncoder.setColorType(pngColorType);
pngEncoder.setCompression(PngEncoder.BEST_COMPRESSION);
pngEncoder.setIndexedColorMode(PngEncoder.INDEXED_COLORS_AUTO);
pngEncoder.encode(srcImage, zos);
*/
// ImageIO.write(srcImage, "PNG", zos);
ImageWriter imageWriter = getPngImageWriter();
imageWriter.setOutput(ImageIO.createImageOutputStream(zos));
imageWriter.write(srcImage);
} else if ("jpeg".equals(ext) || "jpg".equals(ext)) {
ImageWriter imageWriter = getJpegImageWriter();
imageWriter.setOutput(ImageIO.createImageOutputStream(zos));
ImageWriteParam iwp = imageWriter.getDefaultWriteParam();
if (iwp.canWriteCompressed()) {
try {
iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
iwp.setCompressionQuality(jpegQuality);
imageWriter.write(null, new IIOImage(srcImage, null, null), iwp);
} catch (Exception e) {
e.printStackTrace();
}
} else {
imageWriter.write(srcImage);
}
} else {
ImageIO.write(srcImage, ext, zos);
}
zos.flush();
}
Aggregations