use of javax.imageio.ImageWriter in project cxf by apache.
the class SwAOutInterceptor method processAttachments.
protected void processAttachments(SoapMessage message, SoapBodyInfo sbi) {
Collection<Attachment> atts = setupAttachmentOutput(message);
List<Object> outObjects = CastUtils.cast(message.getContent(List.class));
for (MessagePartInfo mpi : sbi.getAttachments()) {
String partName = mpi.getConcreteName().getLocalPart();
String ct = (String) mpi.getProperty(Message.CONTENT_TYPE);
String id = new StringBuilder().append(partName).append('=').append(UUID.randomUUID()).append("@apache.org").toString();
// this assumes things are in order...
int idx = mpi.getIndex();
Object o = outObjects.get(idx);
if (o == null) {
continue;
}
outObjects.set(idx, null);
DataHandler dh;
// This code could probably be refactored out somewhere...
if (o instanceof Source) {
dh = new DataHandler(createDataSource((Source) o, ct));
} else if (o instanceof Image) {
final Image img = (Image) o;
final String contentType = ct;
dh = new DataHandler(o, ct) {
@Override
public InputStream getInputStream() throws IOException {
LoadingByteArrayOutputStream bout = new LoadingByteArrayOutputStream();
writeTo(bout);
return bout.createInputStream();
}
@Override
public void writeTo(OutputStream out) throws IOException {
ImageWriter writer = null;
Iterator<ImageWriter> writers = ImageIO.getImageWritersByMIMEType(contentType);
if (writers.hasNext()) {
writer = writers.next();
}
if (writer != null) {
BufferedImage bimg = convertToBufferedImage(img);
ImageOutputStream iout = ImageIO.createImageOutputStream(out);
writer.setOutput(iout);
writer.write(bimg);
writer.dispose();
iout.flush();
out.flush();
}
}
};
} else if (o instanceof DataHandler) {
dh = (DataHandler) o;
ct = dh.getContentType();
try {
if ("text/xml".equals(ct) && dh.getContent() instanceof Source) {
dh = new DataHandler(createDataSource((Source) dh.getContent(), ct));
}
} catch (IOException e) {
// ignore, use same dh
}
} else if (o instanceof byte[]) {
if (ct == null) {
ct = "application/octet-stream";
}
dh = new DataHandler(new ByteDataSource((byte[]) o, ct));
} else if (o instanceof String) {
if (ct == null) {
ct = "text/plain; charset=\'UTF-8\'";
}
dh = new DataHandler(new ByteDataSource(((String) o).getBytes(StandardCharsets.UTF_8), ct));
} else {
throw new Fault(new org.apache.cxf.common.i18n.Message("ATTACHMENT_NOT_SUPPORTED", LOG, o.getClass()));
}
AttachmentImpl att = new AttachmentImpl(id);
att.setDataHandler(dh);
att.setHeader("Content-Type", ct);
att.setHeader("Content-ID", "<" + id + ">");
atts.add(att);
}
}
use of javax.imageio.ImageWriter in project cxf by apache.
the class ImageHelper method getImageBytes.
public static byte[] getImageBytes(Image image, String type) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
BufferedImage bufImage = convertToBufferedImage(image);
ImageWriter writer = null;
Iterator<ImageWriter> i = ImageIO.getImageWritersByMIMEType(type);
if (i.hasNext()) {
writer = i.next();
}
if (writer != null) {
ImageOutputStream stream = null;
stream = ImageIO.createImageOutputStream(baos);
writer.setOutput(stream);
writer.write(bufImage);
stream.close();
return baos.toByteArray();
}
return null;
}
use of javax.imageio.ImageWriter in project vcell by virtualcell.
the class FormatSpecificSpecs method encodeJPEG.
public static VideoMediaSampleJPEG encodeJPEG(BufferedImage bufferedImage, float compressionQuality, int width, int height, int sampleDuration, int bitsPerPixel, boolean isGrayscale) throws Exception {
ImageWriter imageWriter = ImageIO.getImageWritersBySuffix("jpeg").next();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ImageOutputStream imageOutputStream = ImageIO.createImageOutputStream(byteArrayOutputStream);
imageWriter.setOutput(imageOutputStream);
ImageWriteParam imageWriteParam = imageWriter.getDefaultWriteParam();
imageWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
// quality 0(very compressed, lossy) -> 1.0(less compressed,loss-less)
imageWriteParam.setCompressionQuality(compressionQuality);
IIOImage iioImage = new IIOImage(bufferedImage, null, null);
imageWriter.write(null, iioImage, imageWriteParam);
imageOutputStream.close();
imageWriter.dispose();
return new VideoMediaSampleJPEG(width, height, sampleDuration, byteArrayOutputStream.toByteArray(), bitsPerPixel, isGrayscale);
}
use of javax.imageio.ImageWriter in project hutool by looly.
the class ImgUtil method getWriter.
/**
* 根据给定的Image对象和格式获取对应的{@link ImageWriter},如果未找到合适的Writer,返回null
*
* @param img {@link Image}
* @param formatName 图片格式,例如"jpg"、"png"
* @return {@link ImageWriter}
* @since 4.3.2
*/
public static ImageWriter getWriter(Image img, String formatName) {
final ImageTypeSpecifier type = ImageTypeSpecifier.createFromRenderedImage(toBufferedImage(img, formatName));
final Iterator<ImageWriter> iter = ImageIO.getImageWriters(type, formatName);
return iter.hasNext() ? iter.next() : null;
}
use of javax.imageio.ImageWriter in project propane by ruby-processing.
the class PImageAWT method saveImageIO.
/**
* Use ImageIO functions from Java 1.4 and later to handle image save.Various formats are supported, typically jpeg, png, bmp, and wbmp.To get a list of the supported formats for writing, use: <BR>
* <code>println(javax.imageio.ImageIO.getReaderFormatNames())</code>
*
* @param path
* @return
* @throws java.io.IOException
* @path The path to which the file should be written.
*/
protected boolean saveImageIO(String path) throws IOException {
try {
int outputFormat = (format == ARGB) ? BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB;
String extension = path.substring(path.lastIndexOf('.') + 1).toLowerCase();
// http://code.google.com/p/processing/issues/detail?id=415
if (extension.equals("bmp") || extension.equals("jpg") || extension.equals("jpeg")) {
outputFormat = BufferedImage.TYPE_INT_RGB;
}
BufferedImage bimage = new BufferedImage(pixelWidth, pixelHeight, outputFormat);
bimage.setRGB(0, 0, pixelWidth, pixelHeight, pixels, 0, pixelWidth);
File file = new File(path);
ImageWriter writer = null;
ImageWriteParam param = null;
IIOMetadata metadata = null;
if (extension.equals("jpg") || extension.equals("jpeg")) {
if ((writer = imageioWriter("jpeg")) != null) {
// Set JPEG quality to 90% with baseline optimization. Setting this
// to 1 was a huge jump (about triple the size), so this seems good.
// Oddly, a smaller file size than Photoshop at 90%, but I suppose
// it's a completely different algorithm.
param = writer.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality(0.9f);
}
}
if (extension.equals("png")) {
if ((writer = imageioWriter("png")) != null) {
param = writer.getDefaultWriteParam();
if (false) {
metadata = imageioDPI(writer, param, 100);
}
}
}
if (writer != null) {
try (BufferedOutputStream output = new BufferedOutputStream(PApplet.createOutput(file))) {
writer.setOutput(ImageIO.createImageOutputStream(output));
// writer.write(null, new IIOImage(bimage, null, null), param);
writer.write(metadata, new IIOImage(bimage, null, metadata), param);
writer.dispose();
output.flush();
}
return true;
}
// If iter.hasNext() somehow fails up top, it falls through to here
return javax.imageio.ImageIO.write(bimage, extension, file);
} catch (IOException e) {
throw new IOException("image save failed.");
}
}
Aggregations