use of javax.imageio.ImageReader in project yyl_example by Relucent.
the class GetImageTypeTest method getFormatName.
/**
* 读取图片类型
* @param input 图片数据流
* @return 图片类型
*/
private static String getFormatName(InputStream input) {
ImageInputStream is = null;
try {
// Create an image input stream on the image
is = ImageIO.createImageInputStream(input);
// Find all image readers that recognize the image format
Iterator<ImageReader> it = ImageIO.getImageReaders(is);
if (!it.hasNext()) {
return null;
}
// Use the first reader
ImageReader reader = it.next();
// Return the format name
return reader.getFormatName();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
// The image could not be read
return null;
}
use of javax.imageio.ImageReader in project jdk8u_jdk by JetBrains.
the class GetNumImages method main.
public static void main(String[] args) throws Exception {
IIORegistry registry = IIORegistry.getDefaultInstance();
// test ImageReader.getNumImages() for all available ImageReaders,
// with no source set
Iterator readerspis = registry.getServiceProviders(ImageReaderSpi.class, false);
while (readerspis.hasNext()) {
boolean caughtEx = false;
ImageReaderSpi readerspi = (ImageReaderSpi) readerspis.next();
ImageReader reader = readerspi.createReaderInstance();
try {
reader.getNumImages(false);
} catch (IllegalStateException ise) {
// caught exception, everything's okay
caughtEx = true;
}
if (!caughtEx) {
throw new RuntimeException("Test failed: exception was not " + "thrown for null input: " + reader);
}
}
// test ImageReader.getNumImages() for all available ImageReaders,
// with source set, seekForwardOnly and allowSearch both true
readerspis = registry.getServiceProviders(ImageReaderSpi.class, false);
while (readerspis.hasNext()) {
boolean caughtEx = false;
ImageReaderSpi readerspi = (ImageReaderSpi) readerspis.next();
ImageReader reader = readerspi.createReaderInstance();
byte[] barr = new byte[100];
ByteArrayInputStream bais = new ByteArrayInputStream(barr);
ImageInputStream iis = ImageIO.createImageInputStream(bais);
try {
reader.setInput(iis, true);
reader.getNumImages(true);
} catch (IllegalStateException ise) {
// caught exception, everything's okay
caughtEx = true;
}
if (!caughtEx) {
throw new RuntimeException("Test failed: exception was not " + "thrown when allowSearch and " + "seekForwardOnly are both true: " + reader);
}
}
}
use of javax.imageio.ImageReader in project jdk8u_jdk by JetBrains.
the class AllowSearch method main.
public static void main(String[] args) throws IOException {
ImageReader gifReader = new GIFImageReader(null);
ImageReader jpegReader = new JPEGImageReader(null);
ImageReader pngReader = new PNGImageReader(null);
test(gifReader, "GIF");
test(jpegReader, "JPEG");
test(pngReader, "PNG");
}
use of javax.imageio.ImageReader in project jdk8u_jdk by JetBrains.
the class BmpBigDestinationTest method main.
public static void main(String[] args) {
try {
BufferedImage src = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
Graphics2D g = src.createGraphics();
g.setColor(Color.red);
g.fillRect(0, 0, 100, 100);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageWriter iw = (ImageWriter) ImageIO.getImageWritersByFormatName(format).next();
if (iw == null) {
throw new RuntimeException("No writer available. Test failed.");
}
iw.setOutput(ImageIO.createImageOutputStream(baos));
iw.write(src);
byte[] data = baos.toByteArray();
ImageReader ir = (ImageReader) ImageIO.getImageReadersByFormatName(format).next();
ir.setInput(ImageIO.createImageInputStream(new ByteArrayInputStream(data)));
Iterator specifiers = ir.getImageTypes(0);
ImageTypeSpecifier typeSpecifier = null;
if (specifiers.hasNext()) {
typeSpecifier = (ImageTypeSpecifier) specifiers.next();
}
ImageReadParam param = new ImageReadParam();
BufferedImage dst = typeSpecifier.createBufferedImage(200, 200);
param.setDestination(dst);
ir.read(0, param);
checkResults(src, dst);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("Unexpected exception. Test failed.");
}
}
use of javax.imageio.ImageReader in project jdk8u_jdk by JetBrains.
the class AnimationTest method checkAnimation.
protected void checkAnimation() throws IOException {
ImageReader r = ImageIO.getImageReadersByFormatName("GIF").next();
r.setInput(ImageIO.createImageInputStream(new File(fname)));
int n = r.getNumImages(true);
for (int i = 0; i < n; i++) {
BufferedImage f = r.read(i);
checkFrame(i, f);
}
System.out.println("Test passed.");
}
Aggregations