use of javax.imageio.IIOImage in project nutz by nutzam.
the class Images method writeJpeg.
/**
* 写入一个 JPG 图像
*
* @param im
* 图像对象
* @param targetJpg
* 目标输出 JPG 图像文件
* @param quality
* 质量 0.1f ~ 1.0f
*/
public static void writeJpeg(RenderedImage im, Object targetJpg, float quality) {
ImageWriter writer = null;
try {
writer = ImageIO.getImageWritersBySuffix("jpg").next();
ImageWriteParam param = writer.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality(quality);
ImageOutputStream os = ImageIO.createImageOutputStream(targetJpg);
writer.setOutput(os);
writer.write((IIOMetadata) null, new IIOImage(im, null, null), param);
os.flush();
os.close();
} catch (IOException e) {
throw Lang.wrapThrow(e);
} finally {
if (writer != null) {
try {
writer.dispose();
} catch (Throwable e) {
}
}
}
}
use of javax.imageio.IIOImage 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.IIOImage in project java-swing-tips by aterai.
the class MainPanel method loadFromStream.
// // https://community.oracle.com/thread/1271862 Reading gif animation frame rates and such?
// private List<BufferedImage> loadFromStream(ImageInputStream imageStream) throws IOException {
// String format = "gif";
// String meta = "javax_imageio_gif_image_1.0";
// ImageReader reader = null;
// Iterator<ImageReader> readers = ImageIO.getImageReaders(imageStream);
// while (readers.hasNext()) {
// reader = readers.next();
// String metaFormat = reader.getOriginatingProvider().getNativeImageMetadataFormatName();
// if (format.equalsIgnoreCase(reader.getFormatName()) && meta.equals(metaFormat)) {
// break;
// }
// }
// ImageReader imageReader = Objects.requireNonNull(reader, "Can not read image format!");
// boolean isGif = format.equalsIgnoreCase(imageReader.getFormatName());
// imageReader.setInput(imageStream, false, !isGif);
// List<BufferedImage> list = new ArrayList<>();
// for (int i = 0; i < imageReader.getNumImages(true); i++) {
// IIOImage frame = imageReader.readAll(i, null);
// list.add((BufferedImage) frame.getRenderedImage());
// }
// imageReader.dispose();
// return list;
// }
private static List<BufferedImage> loadFromStream(ImageInputStream imageStream) throws IOException {
Iterable<ImageReader> it = () -> ImageIO.getImageReaders(imageStream);
ImageReader reader = StreamSupport.stream(it.spliterator(), false).filter(MainPanel::checkGifFormat).findFirst().orElseThrow(() -> new IOException("Can not read image format!"));
reader.setInput(imageStream, false, false);
List<BufferedImage> list = new ArrayList<>();
for (int i = 0; i < reader.getNumImages(true); i++) {
IIOImage frame = reader.readAll(i, null);
list.add((BufferedImage) frame.getRenderedImage());
}
reader.dispose();
return list;
}
use of javax.imageio.IIOImage 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.IIOImage 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