本文整理汇总了Java中javax.imageio.IIOImage类的典型用法代码示例。如果您正苦于以下问题:Java IIOImage类的具体用法?Java IIOImage怎么用?Java IIOImage使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
IIOImage类属于javax.imageio包,在下文中一共展示了IIOImage类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: compress
import javax.imageio.IIOImage; //导入依赖的package包/类
public static byte[] compress(BufferedImage image, float scale) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("jpg");
ImageWriter writer = writers.next();
ImageWriteParam param = writer.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality(scale);
ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
writer.setOutput(ios);
writer.write(null, new IIOImage(image, null, null), param);
byte[] data = baos.toByteArray();
writer.dispose();
return data;
}
示例2: writeTiff
import javax.imageio.IIOImage; //导入依赖的package包/类
/**
* Writes a buffered image to a TIFF output stream.
* @param image buffered image to write out as a TIFF
* @param output output stream to write to
* @throws IOException If there is an error writing to
* <code>output</code>.
*/
private void writeTiff(BufferedImage image, OutputStream output)
throws IOException {
try (ImageOutputStream ios =
ImageIO.createImageOutputStream(output)) {
IIORegistry registry = IIORegistry.getDefaultInstance();
registry.registerServiceProviders(
ServiceRegistry.lookupProviders(
TIFFImageWriterSpi.class));
TIFFImageWriterSpi spi = registry.getServiceProviderByClass(
TIFFImageWriterSpi.class);
TIFFImageWriter writer = new TIFFImageWriter(spi);
writer.setOutput(ios);
writer.write(null, new IIOImage(image, null, null), null);
}
}
示例3: write
import javax.imageio.IIOImage; //导入依赖的package包/类
@Override
public void write(IIOMetadata streamMetadata, IIOImage image, ImageWriteParam param) throws IOException {
RenderedImage img = image.getRenderedImage();
if (stream == null) {
throw new IOException("Set an output first!");
}
if (param == null) {
param = getDefaultWriteParam();
}
Rectangle sourceRegion = new Rectangle(0, 0, img.getWidth(), img.getHeight());
if (param.getSourceRegion() != null) {
sourceRegion = sourceRegion.intersection(param.getSourceRegion());
}
Raster raster = img.getData(sourceRegion);
int quality = 85;
if (param.getCompressionMode() == ImageWriteParam.MODE_EXPLICIT) {
quality = (int) (param.getCompressionQuality() * 100);
}
try {
stream.write(lib.encode(raster, quality).array());
} catch (TurboJpegException e) {
throw new IOException(e);
}
}
示例4: convertPngToJpeg
import javax.imageio.IIOImage; //导入依赖的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;
}
示例5: encode
import javax.imageio.IIOImage; //导入依赖的package包/类
/**
* Encodes an image in JPEG format and writes it to an output stream.
*
* @param bufferedImage the image to be encoded ({@code null} not
* permitted).
* @param outputStream the OutputStream to write the encoded image to
* ({@code null} not permitted).
*
* @throws IOException if there is an I/O problem.
* @throws NullPointerException if {@code bufferedImage} is {@code null}.
*/
@Override
public void encode(BufferedImage bufferedImage, OutputStream outputStream)
throws IOException {
Args.nullNotPermitted(bufferedImage, "bufferedImage");
Args.nullNotPermitted(outputStream, "outputStream");
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();
}
示例6: saveImage
import javax.imageio.IIOImage; //导入依赖的package包/类
public static void saveImage(final String fileName, final BufferedImage image, ImageFormat imageFormat) {
try {
final File file = new File(fileName);
final String extension = FileUtilities.getExtension(fileName);
Iterator<ImageWriter> iter = null;
if (canWriteFormat(extension)) {
iter = ImageIO.getImageWritersByFormatName(extension);
} else {
iter = ImageIO.getImageWritersByFormatName(imageFormat.toString());
}
final ImageWriter writer = iter.next();
final ImageWriteParam iwp = writer.getDefaultWriteParam();
file.getParentFile().mkdirs();
try (final FileImageOutputStream output = new FileImageOutputStream(file.getAbsoluteFile())) {
writer.setOutput(output);
final IIOImage outimage = new IIOImage(image, null, null);
writer.write(null, outimage, iwp);
writer.dispose();
}
} catch (final IOException e) {
log.log(Level.SEVERE, e.getMessage(), e);
}
}
示例7: writeJPG
import javax.imageio.IIOImage; //导入依赖的package包/类
public static void writeJPG( BufferedImage rendered, float quality, File f ) {
try {
JPEGImageWriteParam jpegParams = new JPEGImageWriteParam( null );
jpegParams.setCompressionMode( ImageWriteParam.MODE_EXPLICIT );
jpegParams.setCompressionQuality( 1f );
ImageWriter writer = ImageIO.getImageWritersByFormatName( "jpeg" ).next();
ImageWriteParam param = writer.getDefaultWriteParam();
param.setCompressionMode( ImageWriteParam.MODE_EXPLICIT ); // Needed see javadoc
param.setCompressionQuality( quality ); // Highest quality
writer.setOutput( new FileImageOutputStream( f ) );
writer.write( null, new IIOImage( rendered, null, null ), jpegParams );
} catch ( Throwable th ) {
th.printStackTrace();
}
}
示例8: createGif
import javax.imageio.IIOImage; //导入依赖的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;
}
}
示例9: write
import javax.imageio.IIOImage; //导入依赖的package包/类
public void write(IIOMetadata sm,
IIOImage iioimage,
ImageWriteParam p) throws IOException {
if (stream == null) {
throw new IllegalStateException("output == null!");
}
if (iioimage == null) {
throw new IllegalArgumentException("iioimage == null!");
}
if (iioimage.hasRaster()) {
throw new UnsupportedOperationException("canWriteRasters() == false!");
}
resetLocal();
GIFWritableStreamMetadata streamMetadata;
if (sm == null) {
streamMetadata =
(GIFWritableStreamMetadata)getDefaultStreamMetadata(p);
} else {
streamMetadata =
(GIFWritableStreamMetadata)convertStreamMetadata(sm, p);
}
write(true, true, streamMetadata, iioimage, p);
}
示例10: writeToSequence
import javax.imageio.IIOImage; //导入依赖的package包/类
public void writeToSequence(IIOImage image, ImageWriteParam param)
throws IOException {
if (stream == null) {
throw new IllegalStateException("output == null!");
}
if (image == null) {
throw new IllegalArgumentException("image == null!");
}
if (image.hasRaster()) {
throw new UnsupportedOperationException("canWriteRasters() == false!");
}
if (!isWritingSequence) {
throw new IllegalStateException("prepareWriteSequence() was not invoked!");
}
write(!wroteSequenceHeader, false, theStreamMetadata,
image, param);
if (!wroteSequenceHeader) {
wroteSequenceHeader = true;
}
this.imageIndex++;
}
示例11: encode
import javax.imageio.IIOImage; //导入依赖的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();
}
示例12: createTestImage
import javax.imageio.IIOImage; //导入依赖的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");
}
示例13: readFrom
import javax.imageio.IIOImage; //导入依赖的package包/类
private static ITXtTest readFrom(File f) {
try {
ImageInputStream iis = ImageIO.createImageInputStream(f);
ImageReader r = ImageIO.getImageReaders(iis).next();
r.setInput(iis);
IIOImage dst = r.readAll(0, null);
// look for iTXt node
IIOMetadata m = dst.getMetadata();
Node root = m.getAsTree(m.getNativeMetadataFormatName());
Node n = root.getFirstChild();
while (n != null && !"iTXt".equals(n.getNodeName())) {
n = n.getNextSibling();
}
if (n == null) {
throw new RuntimeException("No iTXt node!");
}
ITXtTest t = ITXtTest.getFromNode((IIOMetadataNode)n);
return t;
} catch (Throwable e) {
throw new RuntimeException("Reading test failed.", e);
}
}
示例14: writeImageWithHist
import javax.imageio.IIOImage; //导入依赖的package包/类
protected File writeImageWithHist(BufferedImage bi) throws IOException {
File f = File.createTempFile("hist_", ".png", new File("."));
ImageWriter writer = ImageIO.getImageWritersByFormatName("PNG").next();
ImageOutputStream ios = ImageIO.createImageOutputStream(f);
writer.setOutput(ios);
ImageWriteParam param = writer.getDefaultWriteParam();
ImageTypeSpecifier type = new ImageTypeSpecifier(bi);
IIOMetadata imgMetadata = writer.getDefaultImageMetadata(type, param);
/* add hIST node to image metadata */
imgMetadata = upgradeMetadata(imgMetadata, bi);
IIOImage iio_img = new IIOImage(bi,
null, // no thumbnails
imgMetadata);
writer.write(iio_img);
ios.flush();
ios.close();
return f;
}
示例15: prepareInsertEmpty
import javax.imageio.IIOImage; //导入依赖的package包/类
public void prepareInsertEmpty(int imageIndex,
ImageTypeSpecifier imageType,
int width,
int height,
IIOMetadata imageMetadata,
List<? extends BufferedImage> thumbnails,
ImageWriteParam param) throws IOException {
checkParamsEmpty(imageType, width, height, thumbnails);
this.isInsertingEmpty = true;
SampleModel emptySM = imageType.getSampleModel();
RenderedImage emptyImage =
new EmptyImage(0, 0, width, height,
0, 0, emptySM.getWidth(), emptySM.getHeight(),
emptySM, imageType.getColorModel());
insert(imageIndex, new IIOImage(emptyImage, null, imageMetadata),
param, false);
}