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


Java ImageWriter类代码示例

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


ImageWriter类属于javax.imageio包,在下文中一共展示了ImageWriter类的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: save

import javax.imageio.ImageWriter; //导入依赖的package包/类
void save(File file) throws IOException {
    ImageWriter iw = null;
    String name = file.getName();
    int i = name.lastIndexOf('.');
    if (i >= 0) {
        String extension = name.substring(i + 1);
        Iterator<ImageWriter> imageWritersBySuffix = ImageIO.getImageWritersBySuffix(extension);
        if (imageWritersBySuffix.hasNext()) {
            iw = imageWritersBySuffix.next();
        }
    }
    if (iw != null) {
        file.delete();
        ImageOutputStream ios = ImageIO.createImageOutputStream(file);
        iw.setOutput(ios);
        try {
            iw.write((BufferedImage) image);
        } finally {
            iw.dispose();
            ios.flush();
            ios.close();
        }
    } else {
        ImageIO.write((BufferedImage) image, "PNG", file);
    }
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:27,代码来源:ScreenshotComponent.java

示例3: testEncode

import javax.imageio.ImageWriter; //导入依赖的package包/类
@Test
public void testEncode() throws IOException {
  ImageWriter writer = Streams.stream(ImageIO.getImageWritersByFormatName("jpeg"))
      .filter(TurboJpegImageWriter.class::isInstance)
      .findFirst().get();
  ImageWriteParam param = writer.getDefaultWriteParam();
  param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
  param.setCompressionQuality(0.85f);
  BufferedImage in = ImageIO.read(ClassLoader.getSystemResource("crop_aligned.jpg"));
  ByteArrayOutputStream os = new ByteArrayOutputStream();
  try (ImageOutputStream ios = ImageIO.createImageOutputStream(os)) {
    writer.setOutput(ios);
    writer.write(null, new IIOImage(in, null, null), param);
  }
  os.flush();
  assertThat(os.toByteArray()).isNotEmpty();
}
 
开发者ID:dbmdz,项目名称:imageio-jnr,代码行数:18,代码来源:TurboJpegImageWriterTest.java

示例4: 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;
	}
}
 
开发者ID:KodeMunkie,项目名称:imagetozxspec,代码行数:30,代码来源:GifConverter.java

示例5: 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

示例6: main

import javax.imageio.ImageWriter; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
    String format = "javax_imageio_1.0";
    BufferedImage img =
        new BufferedImage(16, 16, BufferedImage.TYPE_INT_RGB);
    ImageWriter iw = ImageIO.getImageWritersByMIMEType("image/png").next();
    IIOMetadata meta =
        iw.getDefaultImageMetadata(new ImageTypeSpecifier(img), null);
    DOMImplementationRegistry registry;
    registry = DOMImplementationRegistry.newInstance();
    DOMImplementation impl = registry.getDOMImplementation("XML 3.0");
    Document doc = impl.createDocument(null, format, null);
    Element root, text, entry;
    root = doc.getDocumentElement();
    root.appendChild(text = doc.createElement("Text"));
    text.appendChild(entry = doc.createElement("TextEntry"));
    // keyword isn't #REQUIRED by the standard metadata format.
    // However, it is required by the PNG format, so we include it here.
    entry.setAttribute("keyword", "Comment");
    entry.setAttribute("value", "Some demo comment");
    meta.mergeTree(format, root);
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:22,代码来源:MergeStdCommentTest.java

示例7: main

import javax.imageio.ImageWriter; //导入依赖的package包/类
public static void main(String[] args) {
    ImageWriter w = ImageIO.getImageWritersByFormatName(format).next();
    if (w == null) {
        throw new RuntimeException("No available writers for format " + format);
    }
    ImageWriteParam p = w.getDefaultWriteParam();

    ImageTypeSpecifier t =
            ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_RGB);

    IIOMetadata m = w.getDefaultImageMetadata(t, p);
    System.out.println("Default image metadata is " + m);
    testWritableMetadata(m);

    IIOMetadata sm = w.getDefaultStreamMetadata(p);
    System.out.println("Default stream metadata is " + sm);
    testWritableMetadata(sm);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:19,代码来源:WriteMetadataTest.java

示例8: encode

import javax.imageio.ImageWriter; //导入依赖的package包/类
/**
 * Encodes an image in JPEG format and writes it to an output stream.
 *
 * @param bufferedImage  the image to be encoded (<code>null</code> not 
 *     permitted).
 * @param outputStream  the OutputStream to write the encoded image to 
 *     (<code>null</code> not permitted).
 * 
 * @throws IOException if there is an I/O problem.
 * @throws NullPointerException if <code>bufferedImage</code> is 
 *     <code>null</code>.
 */
public void encode(BufferedImage bufferedImage, OutputStream outputStream) 
        throws IOException {
    if (bufferedImage == null) {
        throw new IllegalArgumentException("Null 'image' argument.");
    }
    if (outputStream == null) {
        throw new IllegalArgumentException("Null 'outputStream' argument.");
    }
    Iterator iterator = ImageIO.getImageWritersByFormatName("jpeg");
    ImageWriter writer = (ImageWriter) iterator.next();
    ImageWriteParam p = writer.getDefaultWriteParam();
    p.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    p.setCompressionQuality(this.quality);
    ImageOutputStream ios = ImageIO.createImageOutputStream(outputStream);
    writer.setOutput(ios);
    writer.write(null, new IIOImage(bufferedImage, null, null), p);
    ios.flush();
    writer.dispose();
    ios.close();
}
 
开发者ID:parabuild-ci,项目名称:parabuild-ci,代码行数:33,代码来源:SunJPEGEncoderAdapter.java

示例9: main

import javax.imageio.ImageWriter; //导入依赖的package包/类
public static void main(String[] args) throws IOException {
    Iterator witer = ImageIO.getImageWritersByFormatName("png");
    ImageWriter w = (ImageWriter)witer.next();

    File f = File.createTempFile("WriteProgressive", ".png");
    ImageOutputStream ios = ImageIO.createImageOutputStream(f);
    w.setOutput(ios);

    BufferedImage bi = new BufferedImage(100, 100,
                                         BufferedImage.TYPE_3BYTE_BGR);
    Graphics2D g = bi.createGraphics();
    Random r = new Random(10);
    for (int i = 0; i < 10000; i++) {
        Color c =
            new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256));
        g.setColor(c);
        g.fillRect(r.nextInt(100), r.nextInt(100), 1, 1);
    }

    IIOImage iioimage = new IIOImage(bi, null, null);

    ImageWriteParam param = w.getDefaultWriteParam();
    param.setProgressiveMode(ImageWriteParam.MODE_DEFAULT);

    try {
        w.write(null, iioimage, param);
    } catch (NullPointerException npe) {
        throw new RuntimeException("Got NPE during write!");
    }

    ios.close();

    BufferedImage bi2 = ImageIO.read(f);
    f.delete();

    ImageCompare.compare(bi, bi2);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:38,代码来源:WriteProgressive.java

示例10: 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

示例11: compress

import javax.imageio.ImageWriter; //导入依赖的package包/类
/**
 * 根据质量压缩图片,不改变图片的尺寸
 * 
 * @param imgContent
 *            图片内容
 * @param quality
 *            缩放比例
 * @param extName
 *            后缀名
 * @return
 */
public byte[] compress(byte[] imgContent, float quality, String extName) {
	if (quality > 1 || quality <= 0 || imgContent == null || WakaUtils.string.isEmpty(extName)) {
		throw new ZhhrUtilException("压缩文件失败!入参为空");
	}
	try (InputStream is = new ByteArrayInputStream(imgContent);
			ByteArrayOutputStream os = new ByteArrayOutputStream();
			ImageOutputStream ios = ImageIO.createImageOutputStream(os);) {
		BufferedImage image = ImageIO.read(is);

		Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName(extName);
		ImageWriter writer = writers.next();
		writer.setOutput(ios);

		ImageWriteParam param = writer.getDefaultWriteParam();
		param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
		param.setCompressionQuality(quality);

		writer.write(null, new IIOImage(image, null, null), param);
		writer.dispose();

		return os.toByteArray();
	} catch (IOException e) {
		throw new ZhhrUtilException(e.getMessage(), e);
	}
}
 
开发者ID:wooui,项目名称:springboot-training,代码行数:37,代码来源:ImageUtil.java

示例12: 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);
}
 
开发者ID:Exercon,项目名称:AntiSocial-Platform,代码行数:18,代码来源:ImageServiceImpl.java

示例13: main

import javax.imageio.ImageWriter; //导入依赖的package包/类
public static void main(String[] args) throws IOException {
    String format = "GIF";
    ImageWriter writer = ImageIO.getImageWritersByFormatName(format).next();

    ImageWriteParam param = writer.getDefaultWriteParam();

    IIOMetadata streamMetadata = writer.getDefaultStreamMetadata(param);

    boolean gotException = false;
    try {
        writer.prepareWriteSequence(streamMetadata);
    } catch (IllegalStateException e) {
        gotException = true;
        System.out.println("Test passed.");
        e.printStackTrace(System.out);
    }

    if (!gotException) {
        throw new RuntimeException("Test failed.");
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:22,代码来源:PrepareWriteSequenceTest.java

示例14: createAnimatedImage

import javax.imageio.ImageWriter; //导入依赖的package包/类
public void createAnimatedImage(BufferedImage bi, IIOMetadata metadata,
        ImageWriter writer, File outputFile) {
    try {

        fos = new FileOutputStream(outputFile);
        ImageOutputStream ios = ImageIO.createImageOutputStream(fos);
        System.out.println(ios);
        writer.setOutput(ios);

        ImageWriteParam param = writer.getDefaultWriteParam();
        IIOMetadata streamData = writer.getDefaultStreamMetadata(param);

        writer.prepareWriteSequence(streamData);
        ImageTypeSpecifier specify = new ImageTypeSpecifier(bi);
        IIOMetadata imgData = writer.convertImageMetadata(
                (IIOMetadata) metadata, specify, param);
        GIFImageMetadata gifData = setAnimationProperties(imgData);
        IIOImage iim = new IIOImage(bi, null, gifData);
        param.setProgressiveMode(param.MODE_DISABLED);
        writer.writeToSequence(iim, param);
        writer.endWriteSequence();
        ios.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:27,代码来源:OpaquePNGToGIFTest.java

示例15: doTest

import javax.imageio.ImageWriter; //导入依赖的package包/类
public void doTest() {
    File pwd = new File(".");
    try {
        File f = File.createTempFile("transparency_test_", ".gif", pwd);
        System.out.println("file: " + f.getCanonicalPath());

        ImageWriter w = ImageIO.getImageWritersByFormatName("GIF").next();

        ImageWriterSpi spi = w.getOriginatingProvider();

        boolean succeed_write = ImageIO.write(src, "gif", f);

        if (!succeed_write) {
            throw new RuntimeException("Test failed: failed to write src.");
        }

        dst = ImageIO.read(f);

        checkResult(src, dst);

    } catch (IOException e) {
        throw new RuntimeException("Test failed.", e);
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:25,代码来源:GifTransparencyTest.java


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