本文整理汇总了Java中javax.imageio.ImageWriter.setOutput方法的典型用法代码示例。如果您正苦于以下问题:Java ImageWriter.setOutput方法的具体用法?Java ImageWriter.setOutput怎么用?Java ImageWriter.setOutput使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.imageio.ImageWriter
的用法示例。
在下文中一共展示了ImageWriter.setOutput方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: saveImage
import javax.imageio.ImageWriter; //导入方法依赖的package包/类
private static long saveImage(final BufferedImage image,
final ImageWriter writer,
final ImageWriteParam writerParams,
final String mode,
final String suffix) throws IOException
{
final File imgFile = new File("WriterCompressionTest-"
+ mode + '.' + suffix);
System.out.println("Writing file: " + imgFile.getAbsolutePath());
final ImageOutputStream imgOutStream
= ImageIO.createImageOutputStream(new FileOutputStream(imgFile));
try {
writer.setOutput(imgOutStream);
writer.write(null, new IIOImage(image, null, null), writerParams);
} finally {
imgOutStream.close();
}
return imgFile.length();
}
示例2: createGif
import javax.imageio.ImageWriter; //导入方法依赖的package包/类
/**
* Create the gif and clear down afterwards.
*
* @return the gif byte data
* @throws IOException if the output fails
*/
public byte[] createGif() throws IOException {
try {
ImageOutputStream ios = ImageIO.createImageOutputStream(baos); // N.b. not try with closure because we need to close the stream before returning
ImageWriter iw = ImageIO.getImageWritersByFormatName("gif").next();
iw.setOutput(ios);
iw.prepareWriteSequence(null);
int index = 0;
ImageWriteParam iwp = iw.getDefaultWriteParam();
String frameDelay = String.valueOf(OptionsObject.getInstance().getGifDisplayTimeMillis() / 10L);
log.debug("Frames size: {}, Free memory {}", frames.size(), Runtime.getRuntime().freeMemory());
for (BufferedImage frame : frames) {
IIOMetadata metadata = iw.getDefaultImageMetadata(new ImageTypeSpecifier(frame), iwp);
configureMetaData(metadata, frameDelay, index++);
iw.writeToSequence(new IIOImage(frame, null, metadata), null);
}
iw.endWriteSequence();
ios.close();
return baos.toByteArray();
} finally {
baos = null;
frames = null;
}
}
示例3: runTest
import javax.imageio.ImageWriter; //导入方法依赖的package包/类
public void runTest(Object ctx, int numReps) {
final Context ictx = (Context)ctx;
final ImageWriter writer = ictx.writer;
final BufferedImage image = ictx.image;
do {
try {
ImageOutputStream ios = ictx.createImageOutputStream();
writer.setOutput(ios);
writer.write(image);
writer.reset();
ios.close();
ictx.closeOriginalStream();
} catch (IOException e) {
e.printStackTrace();
}
} while (--numReps >= 0);
}
示例4: compressSave
import javax.imageio.ImageWriter; //导入方法依赖的package包/类
public void compressSave(MultipartFile file, String uploadLocation) throws IOException {
//Compresses JPG file but kills the quality.
BufferedImage image = ImageIO.read(new ByteArrayInputStream(file.getBytes()));
File compressedImageFile = new File(uploadLocation);
OutputStream os = new FileOutputStream(compressedImageFile);
Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("jpg");
ImageWriter writer = (ImageWriter) writers.next();
ImageOutputStream ios = ImageIO.createImageOutputStream(os);
writer.setOutput(ios);
ImageWriteParam param = writer.getDefaultWriteParam();
if(param.canWriteCompressed()) {
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality(0.05f);
}
writer.write(null, new IIOImage(image, null, null), param);
}
示例5: createTestImage
import javax.imageio.ImageWriter; //导入方法依赖的package包/类
private static Image createTestImage() throws IOException {
BufferedImage frame1 = createFrame(new int[] { 0xffff0000, 0xffff0000 });
BufferedImage frame2 = createFrame(new int[] { 0xff0000ff, 0xffff0000 });
ImageWriter writer = ImageIO.getImageWritersByFormatName("GIF").next();
ImageOutputStream ios = ImageIO.createImageOutputStream(new File("lut_test.gif"));
ImageWriteParam param = writer.getDefaultWriteParam();
writer.setOutput(ios);
writer.prepareWriteSequence(null);
writer.writeToSequence(new IIOImage(frame1, null, null), param);
writer.writeToSequence(new IIOImage(frame2, null, null), param);
writer.endWriteSequence();
writer.reset();
writer.dispose();
ios.flush();
ios.close();
return Toolkit.getDefaultToolkit().createImage("lut_test.gif");
}
示例6: test
import javax.imageio.ImageWriter; //导入方法依赖的package包/类
private static void test(final ImageWriter writer) throws Exception {
final File file = File.createTempFile("temp", ".img");
file.deleteOnExit();
final FileOutputStream fos = new FileOutputStream(file);
final ImageOutputStream ios = ImageIO.createImageOutputStream(fos);
writer.setOutput(ios);
final IIOMetadata data = writer.getDefaultStreamMetadata(null);
if (writer.canWriteSequence()) {
writer.prepareWriteSequence(data);
} else {
try {
writer.prepareWriteSequence(data);
throw new RuntimeException(
"UnsupportedOperationException was not thrown");
} catch (final UnsupportedOperationException ignored) {
// expected
}
}
writer.dispose();
ios.close();
}
示例7: setGlitchQuality
import javax.imageio.ImageWriter; //导入方法依赖的package包/类
public static byte[] setGlitchQuality(byte[] inputImageBytes, int quality) throws Exception
{
Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName("png");
ImageWriter writer = (ImageWriter)iter.next();
ImageWriteParam iwp = writer.getDefaultWriteParam();
BufferedImage originalImage = ImageIO.read(new ByteArrayInputStream(inputImageBytes));
IIOImage image = new IIOImage(originalImage, null, null);
ByteArrayOutputStream out=new ByteArrayOutputStream();
ImageOutputStream imageOut=ImageIO.createImageOutputStream(out);
writer.setOutput(imageOut);
writer.write(null, image, iwp);
byte[] qualityImageBytes = out.toByteArray();
return qualityImageBytes;
}
示例8: apply
import javax.imageio.ImageWriter; //导入方法依赖的package包/类
@Override
public BufferedImage apply(BufferedImage img) {
if(compressionLevel == 0f)
return img;
try {
ImageWriter iw = ImageIO.getImageWritersByFormatName("jpeg").next();
JPEGImageWriteParam iwp = (JPEGImageWriteParam) iw.getDefaultWriteParam();
iwp.setOptimizeHuffmanTables(false);
iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
iwp.setProgressiveMode(ImageWriteParam.MODE_DISABLED);
iwp.setCompressionQuality(1f-compressionLevel);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
iw.setOutput(new MemoryCacheImageOutputStream(baos));
IIOImage outputImage = new IIOImage(img, null, null);
iw.write(null, outputImage, iwp);
iw.dispose();
baos.flush();
byte[] returnImage = baos.toByteArray();
baos.close();
BufferedImage img2 = ImageIO.read(new ByteArrayInputStream(returnImage));
if(img2 == null)
throw new Exception();
else
img = img2;
} catch (Exception e) {
e.printStackTrace();
}
return img;
}
示例9: IR2F
import javax.imageio.ImageWriter; //导入方法依赖的package包/类
public static void IR2F(ImageReader input, String formatname, String output) throws IOException {
final Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName(formatname);
final ImageWriter writer = writers.next();
final File f = new File(output);
final ImageOutputStream ios = ImageIO.createImageOutputStream(f);
writer.setOutput(ios);
}
示例10: test
import javax.imageio.ImageWriter; //导入方法依赖的package包/类
private static void test(final ImageWriter writer) throws Exception {
try {
file = File.createTempFile("temp", ".img");
fos = new FileOutputStream(file);
final ImageOutputStream ios = ImageIO.createImageOutputStream(fos);
writer.setOutput(ios);
final IIOMetadata data = writer.getDefaultStreamMetadata(null);
if (writer.canWriteSequence()) {
writer.prepareWriteSequence(data);
} else {
try {
writer.prepareWriteSequence(data);
throw new RuntimeException(
"UnsupportedOperationException was not thrown");
} catch (final UnsupportedOperationException ignored) {
// expected
}
}
} finally {
writer.dispose();
if (file != null) {
if (fos != null) {
fos.close();
}
Files.delete(file.toPath());
}
}
}
示例11: writeTestImage
import javax.imageio.ImageWriter; //导入方法依赖的package包/类
public static void writeTestImage(String fileName) {
BufferedImage bi =
new BufferedImage(TEST_W, TEST_H, BufferedImage.TYPE_INT_RGB);
Graphics g = bi.getGraphics();
g.setColor(new Color(testRGB));
g.fillRect(0, 0, TEST_W, TEST_H);
try {
System.err.printf("Writing %s\n", fileName);
if (lowCompression) {
ImageWriter iw = (ImageWriter)ImageIO.getImageWritersBySuffix("jpeg").next();
if(iw == null) {
throw new RuntimeException("No available image writer for "
+ "jpeg "
+ " Test failed.");
}
File file = new File(fileName);
ImageOutputStream ios = ImageIO.createImageOutputStream(file);
iw.setOutput(ios);
ImageWriteParam param = iw.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality(1);
IIOImage iioImg = new IIOImage(bi, null, null);
iw.write(null, iioImg, param);
} else {
ImageIO.write(bi, "jpeg", new File(fileName));
}
} catch (IOException e) {
System.err.println("Error " + e +
" when writing file: " + fileName);
throw new RuntimeException(e);
}
}
示例12: writeTo
import javax.imageio.ImageWriter; //导入方法依赖的package包/类
private static void writeTo(File f, ITXtTest t) {
BufferedImage src = createBufferedImage();
try {
ImageOutputStream imageOutputStream =
ImageIO.createImageOutputStream(f);
ImageTypeSpecifier imageTypeSpecifier =
new ImageTypeSpecifier(src);
ImageWriter imageWriter =
ImageIO.getImageWritersByFormatName("PNG").next();
imageWriter.setOutput(imageOutputStream);
IIOMetadata m =
imageWriter.getDefaultImageMetadata(imageTypeSpecifier, null);
String format = m.getNativeMetadataFormatName();
Node root = m.getAsTree(format);
IIOMetadataNode iTXt = t.getNode();
root.appendChild(iTXt);
m.setFromTree(format, root);
imageWriter.write(new IIOImage(src, null, m));
imageOutputStream.close();
System.out.println("Writing done.");
} catch (Throwable e) {
throw new RuntimeException("Writing test failed.", e);
}
}
示例13: print
import javax.imageio.ImageWriter; //导入方法依赖的package包/类
public Base64Data print(Image v) {
ByteArrayOutputStreamEx imageData = new ByteArrayOutputStreamEx();
XMLSerializer xs = XMLSerializer.getInstance();
String mimeType = xs.getXMIMEContentType();
if(mimeType==null || mimeType.startsWith("image/*"))
// because PNG is lossless, it's a good default
//
// mime type can be a range, in which case we can't just pass that
// to ImageIO.getImageWritersByMIMEType, so here I'm just assuming
// the default of PNG. Not sure if this is complete.
mimeType = "image/png";
try {
Iterator<ImageWriter> itr = ImageIO.getImageWritersByMIMEType(mimeType);
if(itr.hasNext()) {
ImageWriter w = itr.next();
ImageOutputStream os = ImageIO.createImageOutputStream(imageData);
w.setOutput(os);
w.write(convertToBufferedImage(v));
os.close();
w.dispose();
} else {
// no encoder
xs.handleEvent(new ValidationEventImpl(
ValidationEvent.ERROR,
Messages.NO_IMAGE_WRITER.format(mimeType),
xs.getCurrentLocation(null) ));
// TODO: proper error reporting
throw new RuntimeException("no encoder for MIME type "+mimeType);
}
} catch (IOException e) {
xs.handleError(e);
// TODO: proper error reporting
throw new RuntimeException(e);
}
Base64Data bd = new Base64Data();
imageData.set(bd,mimeType);
return bd;
}
示例14: main
import javax.imageio.ImageWriter; //导入方法依赖的package包/类
public static void main(String[] args) throws IOException {
ImageWriter iw = new PNGImageWriter(null);
File f = File.createTempFile("imageio", "tmp");
ImageOutputStream ios = ImageIO.createImageOutputStream(f);
try {
iw.setOutput(ios);
} catch (NullPointerException npe) {
f.delete();
throw new RuntimeException("Got NullPointerException!");
}
f.delete();
}
示例15: main
import javax.imageio.ImageWriter; //导入方法依赖的package包/类
public static void main(String[] args) throws IOException {
String format = "GIF";
ImageWriter writer =
ImageIO.getImageWritersByFormatName(format).next();
if (writer == null) {
throw new RuntimeException("No available writers for " + format);
}
BufferedImage img = createTestImage(100, 100, BufferedImage.TYPE_BYTE_GRAY);
ImageWriteParam p = writer.getDefaultWriteParam();
ImageTypeSpecifier type =
ImageTypeSpecifier.createFromRenderedImage(img);
IIOMetadata inImageMetadata =
writer.getDefaultImageMetadata(type, p);
IIOMetadata inStreamMetadata = writer.getDefaultStreamMetadata(p);
// write and read image
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
writer.setOutput(ios);
writer.write(inStreamMetadata, new IIOImage(img, null, inImageMetadata), p);
ios.flush();
ios.close();
// read result
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ImageInputStream iis = ImageIO.createImageInputStream(bais);
ImageReader reader = ImageIO.getImageReader(writer);
reader.setInput(iis);
IIOMetadata outStreamMetadata = reader.getStreamMetadata();
GIFStreamMetadata gifStreamMetadata = (GIFStreamMetadata)outStreamMetadata;
if (gifStreamMetadata.logicalScreenWidth != img.getWidth() ||
gifStreamMetadata.logicalScreenHeight != img.getHeight()) {
throw new RuntimeException("Test failed due to wrong logical screen dimension.");
}
}