本文整理汇总了Java中javax.imageio.ImageReader.getImageTypes方法的典型用法代码示例。如果您正苦于以下问题:Java ImageReader.getImageTypes方法的具体用法?Java ImageReader.getImageTypes怎么用?Java ImageReader.getImageTypes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.imageio.ImageReader
的用法示例。
在下文中一共展示了ImageReader.getImageTypes方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: doTest
import javax.imageio.ImageReader; //导入方法依赖的package包/类
private static void doTest(int type) throws IOException {
BufferedImage src = createTestImage(type);
File f = new File("test.jpg");
if (!ImageIO.write(src, "jpg", f)) {
throw new RuntimeException("Failed to write test image.");
}
ImageInputStream iis = ImageIO.createImageInputStream(f);
ImageReader reader = ImageIO.getImageReaders(iis).next();
reader.setInput(iis);
Iterator<ImageTypeSpecifier> types = reader.getImageTypes(0);
ImageTypeSpecifier srgb = null;
ImageTypeSpecifier gray = null;
// look for gray and srgb types
while ((srgb == null || gray == null) && types.hasNext()) {
ImageTypeSpecifier t = types.next();
if (t.getColorModel().getColorSpace().getType() == TYPE_GRAY) {
gray = t;
}
if (t.getColorModel().getColorSpace() == sRGB) {
srgb = t;
}
}
if (gray == null) {
throw new RuntimeException("No gray type available.");
}
if (srgb == null) {
throw new RuntimeException("No srgb type available.");
}
System.out.println("Read as GRAY...");
testType(reader, gray, src);
System.out.println("Read as sRGB...");
testType(reader, srgb, src);
}
示例2: readImage
import javax.imageio.ImageReader; //导入方法依赖的package包/类
/**
* Description of the Method
*
* @param source Description of the Parameter
* @return Description of the Return Value
* @throws IOException Description of the Exception
*/
public static BufferedImage readImage(Object source) throws IOException {
ImageInputStream stream = ImageIO.createImageInputStream(source);
if (stream == null) {
if (source instanceof java.net.URL) {
return ImageIO.read((java.net.URL) source);
} else {
return null;
}
}
Iterator<?> it = ImageIO.getImageReaders(stream);
if (!it.hasNext()) {
// bug with firefox 2
BufferedImage buf = null;
if (source instanceof File) {
buf = ImageIO.read((File) source);
}
return buf;
}
ImageReader reader = (ImageReader) it.next();
reader.setInput(stream);
ImageReadParam param = reader.getDefaultReadParam();
ImageTypeSpecifier typeToUse = null;
boolean looking = true;
for (Iterator<?> i = reader.getImageTypes(0); i.hasNext() && looking; ) {
ImageTypeSpecifier type = (ImageTypeSpecifier) i.next();
if (type.getColorModel().getColorSpace().getNumComponents() == 1) {
typeToUse = type;
looking = false;
} else if (type.getColorModel().getColorSpace().isCS_sRGB()) {
typeToUse = type;
looking = false;
}
}
if (typeToUse != null) {
param.setDestinationType(typeToUse);
}
BufferedImage b = null;
try {
b = reader.read(0, param);
} catch (Exception e) {
e.printStackTrace();
}
reader.dispose();
stream.close();
return b;
}