本文整理汇总了Java中javax.imageio.stream.ImageOutputStream.flush方法的典型用法代码示例。如果您正苦于以下问题:Java ImageOutputStream.flush方法的具体用法?Java ImageOutputStream.flush怎么用?Java ImageOutputStream.flush使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.imageio.stream.ImageOutputStream
的用法示例。
在下文中一共展示了ImageOutputStream.flush方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: save
import javax.imageio.stream.ImageOutputStream; //导入方法依赖的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);
}
}
示例2: encode
import javax.imageio.stream.ImageOutputStream; //导入方法依赖的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();
}
示例3: writeImageWithHist
import javax.imageio.stream.ImageOutputStream; //导入方法依赖的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;
}
示例4: createTestImage
import javax.imageio.stream.ImageOutputStream; //导入方法依赖的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");
}
示例5: doTest
import javax.imageio.stream.ImageOutputStream; //导入方法依赖的package包/类
public void doTest(String compression, int bi_type) throws IOException {
System.out.println("Test " + compression + " on " + getImageTypeName(bi_type));
BufferedImage src = createTestImage(bi_type);
writer.reset();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageOutputStream ios =
ImageIO.createImageOutputStream(baos);
writer.setOutput(ios);
ImageWriteParam wparam = prepareWriteParam(compression);
writer.write(null, new IIOImage(src, null, null), wparam);
ios.flush();
ios.close();
// read result
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ImageInputStream iis = ImageIO.createImageInputStream(bais);
reader.reset();
reader.setInput(iis);
BufferedImage dst = reader.read(0);
checkResult(dst);
}
示例6: paintToPng
import javax.imageio.stream.ImageOutputStream; //导入方法依赖的package包/类
/**
* Paints this panel to a PNG image with specified resolution and width.
* The image height is computed so that the image has the same aspect
* ratio as this panel.
* @param dpi the image resolution, in dots per inch.
* @param win the image width, in inches.
* @param fileName the name of the file to contain the PNG image.
* @throws IOException when unable to save to file.
*/
public void paintToPng(double dpi, double win, String fileName)
throws IOException
{
BufferedImage image = paintToImage((int)ceil(dpi*win));
// The two lines below are simple, but do not write resolution info to
// the PNG file. We want that info, especially for high-res images.
//File file = new File(fileName);
//ImageIO.write(image,"png",file);
Iterator<ImageWriter> i = ImageIO.getImageWritersBySuffix("png");
if (!i.hasNext())
throw new IOException("cannot get a PNG image writer");
ImageWriter iw = i.next();
FileOutputStream fos = new FileOutputStream(fileName);
ImageOutputStream ios = ImageIO.createImageOutputStream(fos);
iw.setOutput(ios);
ImageWriteParam iwp = iw.getDefaultWriteParam();
ImageTypeSpecifier its = new ImageTypeSpecifier(image);
IIOMetadata imd = iw.getDefaultImageMetadata(its,iwp);
String format = "javax_imageio_png_1.0";
IIOMetadataNode tree = (IIOMetadataNode)imd.getAsTree(format);
IIOMetadataNode node = new IIOMetadataNode("pHYs");
String dpm = Integer.toString((int)ceil(dpi/0.0254));
node.setAttribute("pixelsPerUnitXAxis",dpm);
node.setAttribute("pixelsPerUnitYAxis",dpm);
node.setAttribute("unitSpecifier","meter");
tree.appendChild(node);
imd.setFromTree(format,tree);
iw.write(new IIOImage(image,null,imd));
ios.flush();
ios.close();
fos.flush();
fos.close();
iw.dispose();
}
示例7: doWrite
import javax.imageio.stream.ImageOutputStream; //导入方法依赖的package包/类
/**
* Writes image to output stream using given image writer.
*/
private static boolean doWrite(RenderedImage im, ImageWriter writer,
ImageOutputStream output) throws IOException {
if (writer == null) {
return false;
}
writer.setOutput(output);
try {
writer.write(im);
} finally {
writer.dispose();
output.flush();
}
return true;
}
示例8: writeWithCompression
import javax.imageio.stream.ImageOutputStream; //导入方法依赖的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);
}
示例9: BMPSubsamplingTest
import javax.imageio.stream.ImageOutputStream; //导入方法依赖的package包/类
public BMPSubsamplingTest() throws IOException {
ImageWriter writer =
ImageIO.getImageWritersByFormatName(format).next();
ImageWriteParam wparam = writer.getDefaultWriteParam();
wparam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
String[] types = wparam.getCompressionTypes();
for (int t = 0; t < img_types.length; t++) {
int img_type = img_types[t];
System.out.println("Test for " + getImageTypeName(img_type));
BufferedImage image = getTestImage(img_type);
ImageTypeSpecifier specifier = new ImageTypeSpecifier(image);
if (!writer.getOriginatingProvider().canEncodeImage(specifier)) {
System.out.println("Writer does not support encoding this buffered image type.");
continue;
}
for(int i=0; i<types.length; i++) {
if ("BI_JPEG".equals(types[i])) {
// exclude BI_JPEG from automatic test
// due to color diffusion effect on the borders.
continue;
}
if (canEncodeImage(types[i], specifier, img_type)) {
System.out.println("compression type: " + types[i] +
" Supported for " + getImageTypeName(img_type));
} else {
System.out.println("compression type: " + types[i] +
" NOT Supported for " + getImageTypeName(img_type));
continue;
}
ImageWriteParam imageWriteParam = getImageWriteParam(writer, types[i]);
imageWriteParam.setSourceSubsampling(srcXSubsampling,
srcYSubsampling,
0, 0);
File outputFile = new File("subsampling_test_" +
getImageTypeName(img_type) + "__" +
types[i] + ".bmp");
ImageOutputStream ios =
ImageIO.createImageOutputStream(outputFile);
writer.setOutput(ios);
IIOImage iioImg = new IIOImage(image, null, null);
writer.write(null, iioImg, imageWriteParam);
ios.flush();
ios.close();
BufferedImage outputImage = ImageIO.read(outputFile);
checkTestImage(outputImage);
}
}
}
示例10: test
import javax.imageio.stream.ImageOutputStream; //导入方法依赖的package包/类
private static void test() throws IOException {
int failures = 0;
for (int imageType : typeToCompression.keySet()) {
BufferedImage image = new BufferedImage(WIDTH, HEIGHT, imageType);
System.out.println("Image: " + image.toString());
for (String compression : typeToCompression.get(imageType)) {
System.out.println("Compression: "
+ (compression == null ? "None" : compression));
ByteArrayOutputStream output = new ByteArrayOutputStream();
ImageOutputStream ios = new MemoryCacheImageOutputStream(output);
ImageWriter writer =
ImageIO.getImageWritersByFormatName("TIFF").next();
ImageWriteParam wparam = writer.getDefaultWriteParam();
if (compression == null) {
wparam.setCompressionMode(ImageWriteParam.MODE_DEFAULT);
} else {
wparam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
wparam.setCompressionType(compression);
}
writer.setOutput(ios);
writer.write(null, new IIOImage(image, null, null), wparam);
ios.flush();
ImageReader reader =
ImageIO.getImageReadersByFormatName("TIFF").next();
ByteArrayInputStream input
= new ByteArrayInputStream(output.toByteArray());
ImageInputStream iis = new MemoryCacheImageInputStream(input);
iis.mark();
TIFFImageReadParam rparam = new TIFFImageReadParam();
rparam.removeAllowedTagSet(BaselineTIFFTagSet.getInstance());
reader.setInput(iis, false, false);
BufferedImage resultFalse = reader.read(0, rparam);
if (resultFalse.getWidth() != WIDTH
|| resultFalse.getHeight() != HEIGHT) {
System.err.printf("Read image dimensions != %d x %d%n",
WIDTH, HEIGHT);
failures++;
} else {
System.out.println("ignoreMetadata == false test passed");
}
iis.reset();
reader.setInput(iis, false, true);
BufferedImage resultTrue;
try {
resultTrue = reader.read(0, rparam);
if (resultTrue.getWidth() != WIDTH
|| resultTrue.getHeight() != HEIGHT) {
System.err.printf("Read image dimensions != %d x %d%n",
WIDTH, HEIGHT);
failures++;
} else {
System.out.println("ignoreMetadata == true test passed");
}
} catch (Exception e) {
e.printStackTrace();
failures++;
}
}
}
if (failures != 0) {
throw new RuntimeException("Test failed with "
+ failures + " errors!");
}
}
示例11: test
import javax.imageio.stream.ImageOutputStream; //导入方法依赖的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.");
}
}
}
}
示例12: main
import javax.imageio.stream.ImageOutputStream; //导入方法依赖的package包/类
public static void main(String[] args) throws IOException {
ImageIO.setUseCache(true);
// Create a FileImageOutputStream from a FileOutputStream
File temp1 = File.createTempFile("imageio", ".tmp");
temp1.deleteOnExit();
ImageOutputStream fios = ImageIO.createImageOutputStream(temp1);
// Create a FileCacheImageOutputStream from a BufferedOutputStream
File temp2 = File.createTempFile("imageio", ".tmp");
temp2.deleteOnExit();
FileOutputStream fos2 = new FileOutputStream(temp2);
BufferedOutputStream bos = new BufferedOutputStream(fos2);
ImageOutputStream fcios1 = ImageIO.createImageOutputStream(bos);
// Create a FileCacheImageOutputStream from a ByteArrayOutputStream
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageOutputStream fcios2 = ImageIO.createImageOutputStream(baos);
BufferedImage bi =
new BufferedImage(10, 10, BufferedImage.TYPE_3BYTE_BGR);
ImageIO.write(bi, "jpg", fios); // No bug, check it anyway
ImageIO.write(bi, "png", fcios1); // Bug 4414990
ImageIO.write(bi, "jpg", fcios2); // Bug 4415041
// It should not be necessary to flush any of the streams
// If flushing does make a difference, it indicates a bug
// in the writer or the stream implementation
// Get length of temp1 before and after flushing
long file1NoFlushLength = temp1.length();
fios.flush();
long file1FlushLength = temp1.length();
// Get length of temp2 before and after flushing
long file2NoFlushLength = temp2.length();
fcios1.flush();
bos.flush();
long file2FlushLength = temp2.length();
byte[] b0 = baos.toByteArray();
int cacheNoFlushLength = b0.length;
fcios2.flush();
byte[] b1 = baos.toByteArray();
int cacheFlushLength = b1.length;
if (file1NoFlushLength != file1FlushLength) {
// throw new RuntimeException
System.out.println
("FileImageOutputStream not flushed!");
}
if (file2NoFlushLength != file2FlushLength) {
// throw new RuntimeException
System.out.println
("FileCacheImageOutputStream/BufferedOutputStream not flushed!");
}
if (cacheNoFlushLength != cacheFlushLength) {
// throw new RuntimeException
System.out.println
("FileCacheImageOutputStream/ByteArrayOutputStream not flushed!");
}
}
示例13: doTest
import javax.imageio.stream.ImageOutputStream; //导入方法依赖的package包/类
public void doTest() throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
writer.reset();
ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
writer.setOutput(ios);
ImageWriteParam wparam = prepareWriteParam();
IIOMetadata streamMetadata = prepareStreamMetadata(wparam);
writer.prepareWriteSequence(streamMetadata);
for (int i = 0; i < frames.length; i++) {
BufferedImage src = frames[i].getImage();
IIOMetadata imageMetadata = prepareImageMetadata(i, src, wparam);
IIOImage img = new IIOImage(src, null, imageMetadata);
writer.writeToSequence(img, wparam);
}
writer.endWriteSequence();
ios.flush();
ios.close();
if (doSave) {
File f = File.createTempFile("wr_test_", "." + format, new File("."));
System.out.println("Save to file: " + f.getCanonicalPath());
FileOutputStream fos = new FileOutputStream(f);
fos.write(baos.toByteArray());
fos.flush();
fos.close();
}
// read result
reader.reset();
ByteArrayInputStream bais =
new ByteArrayInputStream(baos.toByteArray());
reader.setInput(ImageIO.createImageInputStream(bais));
int minIndex = reader.getMinIndex();
int numImages = reader.getNumImages(true);
for (int i = 0; i < numImages; i++) {
BufferedImage dst = reader.read(i + minIndex);
frames[i].checkResult(dst);
}
}
示例14: main
import javax.imageio.stream.ImageOutputStream; //导入方法依赖的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.");
}
}
示例15: doTest
import javax.imageio.stream.ImageOutputStream; //导入方法依赖的package包/类
public void doTest() {
try {
System.out.println(this.getDescription());
if (param.getCompressionMode() != ImageWriteParam.MODE_EXPLICIT) {
System.out.println("Warning: compression mode is not MODE_EXPLICIT");
}
// change metadata according to ImageWriteParam
IIOMetadata new_meta = iw.convertImageMetadata(meta, new ImageTypeSpecifier(img), param);
IIOImage iio_img = new IIOImage(img, null, new_meta);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
iw.setOutput(ios);
System.out.print("write image...");
System.out.println("Current compression Type is \""+param.getCompressionType()+"\"");
iw.write(new_meta, iio_img, param);
//iw.write(iio_img);
System.out.println("OK");
System.out.print("read image ... ");
ios.flush();
byte[] ba_image = baos.toByteArray();
System.out.println("Array length=" + ba_image.length);
FileOutputStream fos = new FileOutputStream(new File(param.getCompressionType()+".bmp"));
fos.write(ba_image);
fos.flush();
fos = null;
ByteArrayInputStream bais = new ByteArrayInputStream(ba_image);
ImageReader ir = ImageIO.getImageReader(iw);
ir.setInput(ImageIO.createImageInputStream(bais));
BufferedImage res = ir.read(0);
System.out.println("OK");
if (!param.getCompressionType().equals("BI_JPEG")) {
System.out.print("compare images ... ");
boolean r = compare(img,res);
System.out.println(r?"OK":"FAILED");
if (!r) {
throw new RuntimeException("Compared images are not equals. Test failed.");
}
}
BMPMetadata mdata = (BMPMetadata)ir.getImageMetadata(0);
if (!param.getCompressionType().equals(param.getCompressionTypes()[mdata.compression])) {
throw new RuntimeException("Different compression value");
}
} catch (Exception ex) {
ex.printStackTrace();
throw new RuntimeException("Unexpected exception: " + ex);
}
}