当前位置: 首页>>代码示例>>Java>>正文


Java ImageWriter.write方法代码示例

本文整理汇总了Java中javax.imageio.ImageWriter.write方法的典型用法代码示例。如果您正苦于以下问题:Java ImageWriter.write方法的具体用法?Java ImageWriter.write怎么用?Java ImageWriter.write使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在javax.imageio.ImageWriter的用法示例。


在下文中一共展示了ImageWriter.write方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: convertPngToJpeg

import javax.imageio.ImageWriter; //导入方法依赖的package包/类
private String convertPngToJpeg(String pngBase64) throws IOException {
	byte[] pngBinary = DatatypeConverter.parseBase64Binary(pngBase64);
	InputStream in = new ByteArrayInputStream(pngBinary);
	BufferedImage pngImage = ImageIO.read(in);

	int width = pngImage.getWidth(), height = pngImage.getHeight();
	BufferedImage jpgImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

	Graphics2D g = jpgImage.createGraphics();
	g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
	g.setBackground(Color.WHITE);
	g.clearRect(0, 0, width, height);
	g.drawImage(pngImage, 0, 0, width, height, null);
	g.dispose();

	final ImageWriter writer = ImageIO.getImageWritersByFormatName("jpeg").next();
	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	writer.setOutput(ImageIO.createImageOutputStream(baos));
	writer.write(null, new IIOImage(jpgImage, null, null), JPEG_PARAMS);

	String jpgBase64 = DatatypeConverter.printBase64Binary(baos.toByteArray());
	return jpgBase64;
}
 
开发者ID:JensN4,项目名称:png_to_jpg_in_svg,代码行数:24,代码来源:PNG_to_JPG_in_SVG.java

示例2: 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);
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:18,代码来源:OutputImageTests.java

示例3: doTest

import javax.imageio.ImageWriter; //导入方法依赖的package包/类
private static void doTest(int mode) {
    String fileFormat = "bmp";
    try {
        ImageWriter iw = (ImageWriter)ImageIO.getImageWritersBySuffix(fileFormat).next();
        if(iw == null) {
            throw new RuntimeException("No available image writer for "
                                       + fileFormat
                                       + " Test failed.");
        }

        File file = new File("image." + fileFormat);
        ImageOutputStream ios = ImageIO.createImageOutputStream(file);
        iw.setOutput(ios);

        BufferedImage bimg = new BufferedImage(100,
                                               100, BufferedImage.TYPE_INT_RGB);
        Graphics g = bimg.getGraphics();
        g.setColor(Color.green);
        g.fillRect(0,0,100,100);

        ImageWriteParam param = iw.getDefaultWriteParam();

        param.setCompressionMode(mode);

        IIOMetadata meta = iw.getDefaultImageMetadata(new ImageTypeSpecifier(bimg),
                                                      param);

        IIOImage iioImg = new IIOImage(bimg, null, meta);
        iw.write(null, iioImg, param);
    } catch(Exception e) {
        e.printStackTrace();
        throw new RuntimeException("Test failed.");
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:35,代码来源:CompressionModeTest.java

示例4: doTest

import javax.imageio.ImageWriter; //导入方法依赖的package包/类
public void doTest(int type) throws IOException {
    BufferedImage src = getTestImage(type);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageOutputStream ios = ImageIO.createImageOutputStream(baos);

    ImageWriter writer = ImageIO.getImageWritersByFormatName("BMP").next();
    writer.setOutput(ios);

    ImageWriteParam wparam = writer.getDefaultWriteParam();
    wparam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    switch(type) {
        case TEST_RLE8:
            wparam.setCompressionType("BI_RLE8");
            break;
        case TEST_RLE4:
            wparam.setCompressionType("BI_RLE4");
            break;
        default:
            throw new IllegalArgumentException("Wrong test type: " + type);
    }

    writer.write(null, new IIOImage(src, null, null), wparam);

    ios.close();
    baos.close();

    // read result
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());

    BufferedImage dst = ImageIO.read(bais);

    checkResult(src, dst);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:35,代码来源:RLECompressionTest.java

示例5: writeWithCompression

import javax.imageio.ImageWriter; //导入方法依赖的package包/类
private static void writeWithCompression(BufferedImage src,
                                         String compression) throws IOException
{
    System.out.println("Compression: " + compression);
    ImageWriter writer = ImageIO.getImageWritersByFormatName("BMP").next();
    if (writer == null) {
        throw new RuntimeException("Test failed: no bmp writer available");
    }
    File fout = File.createTempFile(compression + "_", ".bmp",
                                    new File("."));

    ImageOutputStream ios = ImageIO.createImageOutputStream(fout);
    writer.setOutput(ios);

    BMPImageWriteParam param = (BMPImageWriteParam)
            writer.getDefaultWriteParam();
    param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    param.setCompressionType(compression);
    param.setTopDown(true);
    writer.write(null, new IIOImage(src, null, null), param);
    writer.dispose();
    ios.flush();
    ios.close();

    BufferedImage dst = ImageIO.read(fout);

    verify(dst);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:29,代码来源:TopDownTest.java

示例6: 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;
}
 
开发者ID:CalebKussmaul,项目名称:GIFKR,代码行数:41,代码来源:JPEGFilter.java

示例7: doTest

import javax.imageio.ImageWriter; //导入方法依赖的package包/类
protected void doTest() throws IOException {
    int w = src.getWidth();
    int h = src.getHeight();

    System.out.println("Write image...");
    try {
        ImageWriter writer =
            ImageIO.getImageWritersByFormatName("GIF").next();
        writer.setOutput(ImageIO.createImageOutputStream(new File(fname)));
        writer.write(src);
    } catch (Exception e) {
        throw new RuntimeException("Test failed.", e);
    }
    System.out.println("Read image....");
    dst = ImageIO.read(new File(fname));

    BufferedImage tmp = new BufferedImage(w, 2 * h,
                                          BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = tmp.createGraphics();
    g.setColor(Color.pink);
    g.fillRect(0, 0, tmp.getWidth(), tmp.getHeight());

    g.drawImage(src, 0, 0, null);
    g.drawImage(dst, 0, h, null);

    int width = w / 8;
    int x = 5 * width + width / 2;
    for (int y = 0; y < h; y++) {
        int argb = tmp.getRGB(x, y);
        if (Color.pink.getRGB() != argb) {
            throw new RuntimeException("Bad color at " + x + "," + y +
                                       " - " + Integer.toHexString(argb));
        }
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:36,代码来源:TransparencyTest.java

示例8: 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;
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:41,代码来源:RuntimeBuiltinLeafInfoImpl.java

示例9: 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);
    }
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:31,代码来源:ITXtTest.java

示例10: writeImage

import javax.imageio.ImageWriter; //导入方法依赖的package包/类
/**
 * Write buffered image as required format
 *
 * @param bi
 * @param mimeType
 * @return
 * @throws IOException
 */
public byte[] writeImage(BufferedImage bi, String mimeType) throws IOException {
    Iterator<ImageWriter> writers = ImageIO.getImageWritersByMIMEType(mimeType);
    if (writers.hasNext()) {
        ImageWriter writer = writers.next();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        writer.setOutput(ImageIO.createImageOutputStream(baos));
        writer.write(bi);
        return baos.toByteArray();
    }
    throw new IOException("Cannot write to this mimetype");
}
 
开发者ID:openaudible,项目名称:openaudible,代码行数:20,代码来源:StandardImageHandler.java

示例11: 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.");
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:45,代码来源:LogicalScreenDimensionTest.java

示例12: test

import javax.imageio.ImageWriter; //导入方法依赖的package包/类
public static void test(String mimeType, boolean useStreamMeta,
                        String metaXml, String... boolXpaths)
    throws Exception
{
    BufferedImage img =
        new BufferedImage(16, 16, BufferedImage.TYPE_INT_RGB);
    ImageWriter iw = ImageIO.getImageWritersByMIMEType(mimeType).next();
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    ImageOutputStream ios = new MemoryCacheImageOutputStream(os);
    iw.setOutput(ios);
    ImageWriteParam param = null;
    IIOMetadata streamMeta = iw.getDefaultStreamMetadata(param);
    IIOMetadata imageMeta =
        iw.getDefaultImageMetadata(new ImageTypeSpecifier(img), param);
    IIOMetadata meta = useStreamMeta ? streamMeta : imageMeta;
    Source src = new StreamSource(new StringReader(metaXml));
    DOMResult dst = new DOMResult();
    transform(src, dst);
    Document doc = (Document)dst.getNode();
    Element node = doc.getDocumentElement();
    String metaFormat = node.getNodeName();

    // Verify that the default metadata gets formatted correctly.
    verify(meta.getAsTree(metaFormat), boolXpaths, false);

    meta.mergeTree(metaFormat, node);

    // Verify that the merged metadata gets formatte correctly.
    verify(meta.getAsTree(metaFormat), boolXpaths, true);

    iw.write(streamMeta, new IIOImage(img, null, imageMeta), param);
    iw.dispose();
    ios.close();
    ImageReader ir = ImageIO.getImageReader(iw);
    byte[] bytes = os.toByteArray();
    if (bytes.length == 0)
        throw new AssertionError("Zero length image file");
    ByteArrayInputStream is = new ByteArrayInputStream(bytes);
    ImageInputStream iis = new MemoryCacheImageInputStream(is);
    ir.setInput(iis);
    if (useStreamMeta) meta = ir.getStreamMetadata();
    else meta = ir.getImageMetadata(0);

    // Verify again after writing and re-reading the image
    verify(meta.getAsTree(metaFormat), boolXpaths, true);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:47,代码来源:BooleanAttributes.java

示例13: test

import javax.imageio.ImageWriter; //导入方法依赖的package包/类
private void test(final ImageWriter writer) throws IOException {
    try {
        // Image initialization
        final BufferedImage imageWrite =
                new BufferedImage(WIDTH, HEIGHT, TYPE_BYTE_BINARY);
        final Graphics2D g = imageWrite.createGraphics();
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, WIDTH, HEIGHT);
        g.dispose();

        // File initialization
        file = File.createTempFile("temp", ".img");
        fos = new SkipWriteOnAbortOutputStream(file);
        final ImageOutputStream ios = ImageIO.createImageOutputStream(fos);
        writer.setOutput(ios);
        writer.addIIOWriteProgressListener(this);

        // This write will be aborted, and file will not be touched
        writer.write(imageWrite);
        if (!isStartedCalled) {
            throw new RuntimeException("Started should be called");
        }
        if (!isProgressCalled) {
            throw new RuntimeException("Progress should be called");
        }
        if (!isAbortCalled) {
            throw new RuntimeException("Abort should be called");
        }
        if (isCompleteCalled) {
            throw new RuntimeException("Complete should not be called");
        }
        // Flush aborted data
        ios.flush();

        /*
         * This write should be completed successfully and the file should
         * contain correct image data.
         */
        abortFlag = false;
        isAbortCalled = false;
        isCompleteCalled = false;
        isProgressCalled = false;
        isStartedCalled = false;
        writer.write(imageWrite);

        if (!isStartedCalled) {
            throw new RuntimeException("Started should be called");
        }
        if (!isProgressCalled) {
            throw new RuntimeException("Progress should be called");
        }
        if (isAbortCalled) {
            throw new RuntimeException("Abort should not be called");
        }
        if (!isCompleteCalled) {
            throw new RuntimeException("Complete should be called");
        }
        ios.close();

        // Validates content of the file.
        final BufferedImage imageRead = ImageIO.read(file);
        for (int x = 0; x < WIDTH; ++x) {
            for (int y = 0; y < HEIGHT; ++y) {
                if (imageRead.getRGB(x, y) != imageWrite.getRGB(x, y)) {
                    throw new RuntimeException("Test failed.");
                }
            }
        }
    } finally {
        writer.dispose();
        if (file != null) {
            if (fos != null) {
                fos.close();
            }
            Files.delete(file.toPath());
        }
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:79,代码来源:WriteAfterAbort.java

示例14: write

import javax.imageio.ImageWriter; //导入方法依赖的package包/类
@Override
public void write(BufferedImage image, MediaType contentType, HttpOutputMessage outputMessage)
		throws IOException, HttpMessageNotWritableException {

	if (contentType == null || contentType.isWildcardType() || contentType.isWildcardSubtype()) {
		contentType = getDefaultContentType();
	}
	Assert.notNull(contentType,
			"Count not determine Content-Type, set one using the 'defaultContentType' property");
	outputMessage.getHeaders().setContentType(contentType);
	ImageOutputStream imageOutputStream = null;
	ImageWriter imageWriter = null;
	try {
		imageOutputStream = createImageOutputStream(outputMessage.getBody());
		Iterator<ImageWriter> imageWriters = ImageIO.getImageWritersByMIMEType(contentType.toString());
		if (imageWriters.hasNext()) {
			imageWriter = imageWriters.next();
			ImageWriteParam iwp = imageWriter.getDefaultWriteParam();
			process(iwp);
			imageWriter.setOutput(imageOutputStream);
			imageWriter.write(null, new IIOImage(image, null, null), iwp);
		}
		else {
			throw new HttpMessageNotWritableException(
					"Could not find javax.imageio.ImageWriter for Content-Type [" + contentType + "]");
		}
	}
	finally {
		if (imageWriter != null) {
			imageWriter.dispose();
		}
		if (imageOutputStream != null) {
			try {
				imageOutputStream.close();
			}
			catch (IOException ex) {
				// ignore
			}
		}
	}
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:42,代码来源:BufferedImageHttpMessageConverter.java

示例15: doTest

import javax.imageio.ImageWriter; //导入方法依赖的package包/类
public static void doTest(boolean writeSequence) throws IOException {
    String format = "GIF";
    ImageWriter writer =
            ImageIO.getImageWritersByFormatName(format).next();
    if (writer == null) {
        throw new RuntimeException("No writer available for " + format);
    }

    BufferedImage img = createTestImage();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
    writer.setOutput(ios);

    WriterReuseTest t = new WriterReuseTest();
    writer.addIIOWriteProgressListener(t);

    ImageWriteParam param = writer.getDefaultWriteParam();
    IIOMetadata streamMetadata = writer.getDefaultStreamMetadata(param);
    IIOImage iioImg = new IIOImage(img, null, null);
    if (writeSequence) {
        writer.prepareWriteSequence(streamMetadata);
        writer.writeToSequence(iioImg, param);
    } else {
        writer.write(img);
    }

    if (!t.isWritingAborted || t.isWritingCompleted) {
        throw new RuntimeException("Test failed.");
    }
    t.reset();

    // next attempt after abort
    ImageOutputStream ios2 =
         ImageIO.createImageOutputStream(new ByteArrayOutputStream());
    writer.setOutput(ios2);
    if (writeSequence) {
        writer.writeToSequence(iioImg, param);
    } else {
        writer.write(img);
    }

    if (t.isWritingAborted || !t.isWritingCompleted) {
        throw new RuntimeException("Test failed.");
    }
    System.out.println("Test passed.");
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:47,代码来源:WriterReuseTest.java


注:本文中的javax.imageio.ImageWriter.write方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。