下面列出了怎么用javax.imageio.stream.FileImageInputStream的API类实例代码及写法,或者点击链接到github查看源代码。
public static Dimension getImageDimension(File imgFile) throws IOException {
int pos = imgFile.getName().lastIndexOf(".");
if (pos == -1)
throw new IOException("No extension for file: " + imgFile.getAbsolutePath());
String suffix = imgFile.getName().substring(pos + 1);
Iterator<ImageReader> iter = ImageIO.getImageReadersBySuffix(suffix);
while(iter.hasNext()) {
ImageReader reader = iter.next();
try {
ImageInputStream stream = new FileImageInputStream(imgFile);
reader.setInput(stream);
int width = reader.getWidth(reader.getMinIndex());
int height = reader.getHeight(reader.getMinIndex());
return new Dimension(width, height);
} catch (IOException e) {
System.out.println("Error reading: " + imgFile.getAbsolutePath() + " " + e);
} finally {
reader.dispose();
}
}
throw new IOException("Not a known image file: " + imgFile.getAbsolutePath());
}
private static void checkContainerInFile(File file, int nonOptionalIntValue, Integer autoOptionalIntValue)
throws IOException
{
final FileImageInputStream stream = new FileImageInputStream(file);
if (autoOptionalIntValue == null)
{
assertEquals((CONTAINER_BIT_SIZE_WITHOUT_OPTIONAL + 7) / Byte.SIZE, stream.length());
assertEquals(nonOptionalIntValue, (int) stream.readBits(32));
assertEquals(0, stream.readBit());
}
else
{
assertEquals((CONTAINER_BIT_SIZE_WITH_OPTIONAL + 7) / Byte.SIZE, stream.length());
assertEquals(nonOptionalIntValue, (int) stream.readBits(32));
assertEquals(1, stream.readBit());
assertEquals((int) autoOptionalIntValue, (int) stream.readBits(32));
}
stream.close();
}
private static void checkContainerInFile(File file, BasicColor basicColor, short numBlackTones)
throws IOException
{
final FileImageInputStream stream = new FileImageInputStream(file);
if (basicColor == BasicColor.WHITE)
{
assertEquals(1, stream.length());
assertEquals(basicColor.getValue(), stream.readByte());
}
else
{
assertEquals(1 + 1 + 4 * numBlackTones, stream.length());
assertEquals(basicColor.getValue(), stream.readByte());
assertEquals(numBlackTones, stream.readByte());
for (short i = 0; i < numBlackTones; ++i)
assertEquals(i + 1, stream.readInt());
}
stream.close();
}
/**
* Gets image dimensions for given file
*
* @param imgFile image file
* @return dimensions of image
* @throws IOException if the file is not a known image
*/
public static Dimension getImageDimension(File imgFile) throws IOException {
int pos = imgFile.getName().lastIndexOf(".");
if (pos == -1)
throw new IOException("No extension for file: " + imgFile.getAbsolutePath());
String suffix = imgFile.getName().substring(pos + 1);
Iterator<ImageReader> iter = ImageIO.getImageReadersBySuffix(suffix);
while (iter.hasNext()) {
ImageReader reader = iter.next();
try {
ImageInputStream stream = new FileImageInputStream(imgFile);
reader.setInput(stream);
int width = reader.getWidth(reader.getMinIndex());
int height = reader.getHeight(reader.getMinIndex());
return new Dimension(width, height);
} catch (IOException e) {
log.warn("Error reading: " + imgFile.getAbsolutePath(), e);
} finally {
reader.dispose();
}
}
throw new IOException("Not a known image file: " + imgFile.getAbsolutePath());
}
public static void main(String[] args) {
try {
File f = new File("ImageInputStreamFromRAF.tmp");
RandomAccessFile raf = new RandomAccessFile(f, "rw");
ImageInputStream istream = ImageIO.createImageInputStream(raf);
ImageOutputStream ostream = ImageIO.createImageOutputStream(raf);
f.delete();
if (istream == null) {
throw new RuntimeException("ImageIO.createImageInputStream(RandomAccessFile) returned null!");
}
if (ostream == null) {
throw new RuntimeException("ImageIO.createImageOutputStream(RandomAccessFile) returned null!");
}
if (!(istream instanceof FileImageInputStream)) {
throw new RuntimeException("ImageIO.createImageInputStream(RandomAccessFile) did not return a FileImageInputStream!");
}
if (!(ostream instanceof FileImageOutputStream)) {
throw new RuntimeException("ImageIO.createImageOutputStream(RandomAccessFile) did not return a FileImageOutputStream!");
}
} catch (IOException ioe) {
throw new RuntimeException("Unexpected IOException: " + ioe);
}
}
public static void main(String args[]) throws IOException {
String inputWebpPath = "test_pic/test.webp";
String outputJpgPath = "test_pic/test_.jpg";
String outputJpegPath = "test_pic/test_.jpeg";
String outputPngPath = "test_pic/test_.png";
// Obtain a WebP ImageReader instance
ImageReader reader = ImageIO.getImageReadersByMIMEType("image/webp").next();
// Configure decoding parameters
WebPReadParam readParam = new WebPReadParam();
readParam.setBypassFiltering(true);
// Configure the input on the ImageReader
reader.setInput(new FileImageInputStream(new File(inputWebpPath)));
// Decode the image
BufferedImage image = reader.read(0, readParam);
ImageIO.write(image, "png", new File(outputPngPath));
ImageIO.write(image, "jpg", new File(outputJpgPath));
ImageIO.write(image, "jpeg", new File(outputJpegPath));
}
/**
* Gets image dimensions for given file
*
* @param imgFile image file
* @return dimensions of image
* @throws IOException if the file is not a known image
*/
public static Dimension getImageDimension(File imgFile) throws IOException {
int pos = imgFile.getName().lastIndexOf(".");
if (pos == -1)
throw new IOException("No extension for file: " + imgFile.getAbsolutePath());
String suffix = imgFile.getName().substring(pos + 1);
Iterator<ImageReader> iter = ImageIO.getImageReadersBySuffix(suffix);
if (iter.hasNext()) {
ImageReader reader = iter.next();
try {
ImageInputStream stream = new FileImageInputStream(imgFile);
reader.setInput(stream);
int width = reader.getWidth(reader.getMinIndex());
int height = reader.getHeight(reader.getMinIndex());
return new Dimension(width, height);
} finally {
reader.dispose();
}
}
throw new IOException("Not a known image file: " + imgFile.getAbsolutePath());
}
public static void main(String[] args) {
try {
File f = new File("ImageInputStreamFromRAF.tmp");
RandomAccessFile raf = new RandomAccessFile(f, "rw");
ImageInputStream istream = ImageIO.createImageInputStream(raf);
ImageOutputStream ostream = ImageIO.createImageOutputStream(raf);
f.delete();
if (istream == null) {
throw new RuntimeException("ImageIO.createImageInputStream(RandomAccessFile) returned null!");
}
if (ostream == null) {
throw new RuntimeException("ImageIO.createImageOutputStream(RandomAccessFile) returned null!");
}
if (!(istream instanceof FileImageInputStream)) {
throw new RuntimeException("ImageIO.createImageInputStream(RandomAccessFile) did not return a FileImageInputStream!");
}
if (!(ostream instanceof FileImageOutputStream)) {
throw new RuntimeException("ImageIO.createImageOutputStream(RandomAccessFile) did not return a FileImageOutputStream!");
}
} catch (IOException ioe) {
throw new RuntimeException("Unexpected IOException: " + ioe);
}
}
public static boolean doesImageFitBox(FileHandle fileHandle, float width, float height) {
boolean result = false;
String suffix = fileHandle.extension();
Iterator<ImageReader> iter = ImageIO.getImageReadersBySuffix(suffix);
if (iter.hasNext()) {
ImageReader reader = iter.next();
try (var stream = new FileImageInputStream(fileHandle.file())) {
reader.setInput(stream);
int imageWidth = reader.getWidth(reader.getMinIndex());
int imageHeight = reader.getHeight(reader.getMinIndex());
result = imageWidth < width && imageHeight < height;
} catch (IOException e) {
Gdx.app.error(Utils.class.getName(), "error checking image dimensions", e);
} finally {
reader.dispose();
}
} else {
Gdx.app.error(Utils.class.getName(), "No reader available to check image dimensions");
}
return result;
}
private static Dimension getImageDimension(File imgFile) throws IOException {
int pos = imgFile.getName().lastIndexOf(".");
if (pos == -1)
throw new IOException("No extension for file: " + imgFile.getAbsolutePath());
String suffix = imgFile.getName().substring(pos + 1);
Iterator<ImageReader> iter = ImageIO.getImageReadersBySuffix(suffix);
while (iter.hasNext()) {
ImageReader reader = iter.next();
try {
ImageInputStream stream = new FileImageInputStream(imgFile);
reader.setInput(stream);
int width = reader.getWidth(reader.getMinIndex());
int height = reader.getHeight(reader.getMinIndex());
return new Dimension(width, height);
} catch (IOException e) {
LOG.log(Level.WARNING, "Error reading: " + imgFile.getAbsolutePath(), e);
} finally {
reader.dispose();
}
}
throw new IOException("Not a known image file: " + imgFile.getAbsolutePath());
}
private java.awt.Dimension getImageSize(File image) throws IOException {
String filename = image.getName();
int p = filename.lastIndexOf('.');
if (p == -1) {
return null;
}
String suffix = filename.substring(p + 1).toLowerCase();
Iterator<ImageReader> readers = ImageIO.getImageReadersBySuffix(suffix);
if (readers.hasNext()) {
ImageReader reader = readers.next();
try {
try (ImageInputStream in = new FileImageInputStream(image)) {
reader.setInput(in);
int width = reader.getWidth(reader.getMinIndex());
int height = reader.getHeight(reader.getMinIndex());
return new java.awt.Dimension(width, height);
}
} finally {
reader.dispose();
}
} else {
return null;
}
}
public ImageInputStream createInputStreamInstance(Object input,
boolean useCache,
File cacheDir) {
if (input instanceof File) {
try {
return new FileImageInputStream((File)input);
} catch (Exception e) {
return null;
}
} else {
throw new IllegalArgumentException();
}
}
public ImageInputStream createInputStreamInstance(Object input,
boolean useCache,
File cacheDir) {
if (input instanceof RandomAccessFile) {
try {
return new FileImageInputStream((RandomAccessFile)input);
} catch (Exception e) {
return null;
}
} else {
throw new IllegalArgumentException
("input not a RandomAccessFile!");
}
}
public ImageInputStream createInputStreamInstance(Object input,
boolean useCache,
File cacheDir) {
if (input instanceof File) {
try {
return new FileImageInputStream((File)input);
} catch (Exception e) {
return null;
}
} else {
throw new IllegalArgumentException();
}
}
public ImageInputStream createInputStreamInstance(Object input,
boolean useCache,
File cacheDir) {
if (input instanceof RandomAccessFile) {
try {
return new FileImageInputStream((RandomAccessFile)input);
} catch (Exception e) {
return null;
}
} else {
throw new IllegalArgumentException
("input not a RandomAccessFile!");
}
}
public ImageInputStream createInputStreamInstance(Object input,
boolean useCache,
File cacheDir) {
if (input instanceof File) {
try {
return new FileImageInputStream((File)input);
} catch (Exception e) {
return null;
}
} else {
throw new IllegalArgumentException();
}
}
public ImageInputStream createInputStreamInstance(Object input,
boolean useCache,
File cacheDir) {
if (input instanceof RandomAccessFile) {
try {
return new FileImageInputStream((RandomAccessFile)input);
} catch (Exception e) {
return null;
}
} else {
throw new IllegalArgumentException
("input not a RandomAccessFile!");
}
}
public ImageInputStream createInputStreamInstance(Object input,
boolean useCache,
File cacheDir) {
if (input instanceof File) {
try {
return new FileImageInputStream((File)input);
} catch (Exception e) {
return null;
}
} else {
throw new IllegalArgumentException();
}
}
public ImageInputStream createInputStreamInstance(Object input,
boolean useCache,
File cacheDir) {
if (input instanceof RandomAccessFile) {
try {
return new FileImageInputStream((RandomAccessFile)input);
} catch (Exception e) {
return null;
}
} else {
throw new IllegalArgumentException
("input not a RandomAccessFile!");
}
}
public ImageInputStream createInputStreamInstance(Object input,
boolean useCache,
File cacheDir) {
if (input instanceof File) {
try {
return new FileImageInputStream((File)input);
} catch (Exception e) {
return null;
}
} else {
throw new IllegalArgumentException();
}
}
public ImageInputStream createInputStreamInstance(Object input,
boolean useCache,
File cacheDir) {
if (input instanceof RandomAccessFile) {
try {
return new FileImageInputStream((RandomAccessFile)input);
} catch (Exception e) {
return null;
}
} else {
throw new IllegalArgumentException
("input not a RandomAccessFile!");
}
}
private void checkItemInFile(File file, Item item, long version) throws IOException
{
final FileImageInputStream stream = new FileImageInputStream(file);
assertEquals(item.getParam(), stream.readUnsignedShort());
if (version >= HIGHER_VERSION)
assertEquals(item.getExtraParam(), Long.valueOf(stream.readUnsignedInt()));
stream.close();
}
public ImageInputStream createInputStreamInstance(Object input,
boolean useCache,
File cacheDir) {
if (input instanceof File) {
try {
return new FileImageInputStream((File)input);
} catch (Exception e) {
return null;
}
} else {
throw new IllegalArgumentException();
}
}
public ImageInputStream createInputStreamInstance(Object input,
boolean useCache,
File cacheDir) {
if (input instanceof RandomAccessFile) {
try {
return new FileImageInputStream((RandomAccessFile)input);
} catch (Exception e) {
return null;
}
} else {
throw new IllegalArgumentException
("input not a RandomAccessFile!");
}
}
public ImageInputStream createInputStreamInstance(Object input,
boolean useCache,
File cacheDir) {
if (input instanceof File) {
try {
return new FileImageInputStream((File)input);
} catch (Exception e) {
return null;
}
} else {
throw new IllegalArgumentException();
}
}
public ImageInputStream createInputStreamInstance(Object input,
boolean useCache,
File cacheDir) {
if (input instanceof RandomAccessFile) {
try {
return new FileImageInputStream((RandomAccessFile)input);
} catch (Exception e) {
return null;
}
} else {
throw new IllegalArgumentException
("input not a RandomAccessFile!");
}
}
public ImageInputStream createInputStreamInstance(Object input,
boolean useCache,
File cacheDir) {
if (input instanceof File) {
try {
return new FileImageInputStream((File)input);
} catch (Exception e) {
return null;
}
} else {
throw new IllegalArgumentException();
}
}
public ImageInputStream createInputStreamInstance(Object input,
boolean useCache,
File cacheDir) {
if (input instanceof RandomAccessFile) {
try {
return new FileImageInputStream((RandomAccessFile)input);
} catch (Exception e) {
return null;
}
} else {
throw new IllegalArgumentException
("input not a RandomAccessFile!");
}
}
public ImageInputStream createInputStreamInstance(Object input,
boolean useCache,
File cacheDir) {
if (input instanceof File) {
try {
return new FileImageInputStream((File)input);
} catch (Exception e) {
return null;
}
} else {
throw new IllegalArgumentException();
}
}
public ImageInputStream createInputStreamInstance(Object input,
boolean useCache,
File cacheDir) {
if (input instanceof RandomAccessFile) {
try {
return new FileImageInputStream((RandomAccessFile)input);
} catch (Exception e) {
return null;
}
} else {
throw new IllegalArgumentException
("input not a RandomAccessFile!");
}
}