本文整理汇总了Java中javax.imageio.ImageWriter.dispose方法的典型用法代码示例。如果您正苦于以下问题:Java ImageWriter.dispose方法的具体用法?Java ImageWriter.dispose怎么用?Java ImageWriter.dispose使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.imageio.ImageWriter
的用法示例。
在下文中一共展示了ImageWriter.dispose方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: compress
import javax.imageio.ImageWriter; //导入方法依赖的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: saveImage
import javax.imageio.ImageWriter; //导入方法依赖的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);
}
}
示例3: 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);
}
}
示例4: 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();
}
示例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: 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);
}
}
示例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: resize
import javax.imageio.ImageWriter; //导入方法依赖的package包/类
/**
* Reads the original image, creates a resized copy of it and returns its input stream. largestDimension is the
* largest dimension of the resized image, the other dimension is scaled accordingly.
*
* @param image
* original image
* @param largestDimension
* the largest dimension of the resized image, the other dimension is scaled accordingly
* @return
* @throws IOException
* @throws UploadImageGalleryFileException
*/
public static InputStream resize(BufferedImage image, int largestDimension) throws IOException {
try {
//resize to 150 pixels max
BufferedImage outImage = Scalr.resize(image, Method.QUALITY, largestDimension);
//set jpeg compression quality explicitly
ImageWriter imageWriter = ImageIO.getImageWritersByFormatName("jpeg").next();
ImageWriteParam jpgWriteParam = imageWriter.getDefaultWriteParam();
jpgWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
jpgWriteParam.setCompressionQuality(.95f);
// buffer all data in a circular buffer of infinite sizes
CircularByteBuffer outputBuffer = new CircularByteBuffer(CircularByteBuffer.INFINITE_SIZE);
ImageOutputStream ios = ImageIO.createImageOutputStream(outputBuffer.getOutputStream());
imageWriter.setOutput(ios);
IIOImage outputImage = new IIOImage(outImage, null, null);
imageWriter.write(null, outputImage, jpgWriteParam);
imageWriter.dispose();
outputBuffer.getOutputStream().close();
return outputBuffer.getInputStream();
} catch (IOException e) {
log.error(e.getStackTrace());
return null;
}
}
示例10: 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;
}
示例11: main
import javax.imageio.ImageWriter; //导入方法依赖的package包/类
public static void main(final String[] argv) throws Exception {
IIOMetadata jpegmetadata = null;
ImageWriter jpgWriter = ImageIO.getImageWritersByFormatName("jpg").next();
try {
jpegmetadata = createJPEGMetadata(jpgWriter);
} catch (Exception e) {
throw new RuntimeException(e);
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageOutputStream output = ImageIO.createImageOutputStream(baos);
jpgWriter.setOutput(output);
int w=100, h=100;
BufferedImage bi = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = bi.createGraphics();
g2d.setColor(Color.white);
g2d.fillRect(0, 0, w, h);
IIOImage image = new IIOImage(bi, null, jpegmetadata);
jpgWriter.write(null, image, null);
jpgWriter.dispose();
baos.flush();
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ImageInputStream iis = ImageIO.createImageInputStream(bais);
bi = ImageIO.read(iis);
for (int i=0; i<bi.getWidth(); i++) {
for(int j=0; j<bi.getHeight(); j++) {
if (bi.getRGB(i, j) != Color.white.getRGB()) {
throw new RuntimeException("Wrong color : " + Integer.toHexString(bi.getRGB(i, j)));
}
}
}
}
示例12: 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());
}
}
}
示例13: 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
}
}
}
}
示例14: 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);
}
示例15: test
import javax.imageio.ImageWriter; //导入方法依赖的package包/类
private void test(final ImageWriter writer) throws IOException {
// 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
final File file = File.createTempFile("temp", ".img");
file.deleteOnExit();
final FileOutputStream 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");
}
writer.dispose();
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.");
}
}
}
}