本文整理匯總了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);
}