use of javax.imageio.ImageWriter in project cxf by apache.
the class ImageDataContentHandler method writeTo.
public void writeTo(Object obj, String mimeType, OutputStream os) throws IOException {
if (obj instanceof Image) {
Iterator<ImageWriter> writers = ImageIO.getImageWritersByMIMEType(mimeType);
if (writers.hasNext()) {
ImageWriter writer = writers.next();
BufferedImage bimg = convertToBufferedImage((Image) obj);
ImageOutputStream out = ImageIO.createImageOutputStream(os);
writer.setOutput(out);
writer.write(bimg);
writer.dispose();
out.flush();
out.close();
return;
}
} else if (obj instanceof byte[]) {
os.write((byte[]) obj);
} else if (obj instanceof InputStream) {
IOUtils.copyAndCloseInput((InputStream) obj, os);
} else if (obj instanceof File) {
InputStream file = Files.newInputStream(((File) obj).toPath());
IOUtils.copyAndCloseInput(file, os);
} else {
throw new IOException("Attachment type not spported " + obj.getClass());
}
}
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 = null;
// 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 jmonkeyengine by jMonkeyEngine.
the class JmeDesktopSystem method writeImageFile.
@Override
public void writeImageFile(OutputStream outStream, String format, ByteBuffer imageData, int width, int height) throws IOException {
BufferedImage awtImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
Screenshots.convertScreenShot2(imageData.asIntBuffer(), awtImage);
ImageWriter writer = ImageIO.getImageWritersByFormatName(format).next();
ImageWriteParam writeParam = writer.getDefaultWriteParam();
if (format.equals("jpg")) {
JPEGImageWriteParam jpegParam = (JPEGImageWriteParam) writeParam;
jpegParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
jpegParam.setCompressionQuality(0.95f);
}
awtImage = verticalFlip(awtImage);
ImageOutputStream imgOut = new MemoryCacheImageOutputStream(outStream);
writer.setOutput(imgOut);
IIOImage outputImage = new IIOImage(awtImage, null, null);
try {
writer.write(null, outputImage, writeParam);
} finally {
imgOut.close();
writer.dispose();
}
}
Aggregations