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