如何使用Java获取图像的高度和宽度?

除了使用ImageIO.read来获取图像的高度和宽度外,还有其他方法吗?


因为我遇到了锁定线程的问题。


at com.sun.medialib.codec.jpeg.Decoder.njpeg_decode(Native Method)      

at com.sun.medialib.codec.jpeg.Decoder.decode(Decoder.java:87)      

at com.sun.media.imageioimpl.plugins.jpeg.CLibJPEGImageReader.decode(CLibJPEGImageReader.java:73)     

 - locked <0xd96fb668> (a com.sun.media.imageioimpl.plugins.jpeg.CLibJPEGImageReader)      

at com.sun.media.imageioimpl.plugins.clib.CLibImageReader.getImage(CLibImageReader.java:320)    

 - locked <0xd96fb668> (a com.sun.media.imageioimpl.plugins.jpeg.CLibJPEGImageReader)     

 at com.sun.media.imageioimpl.plugins.clib.CLibImageReader.read(CLibImageReader.java:384)   

 - locked <0xd96fb668> (a com.sun.media.imageioimpl.plugins.jpeg.CLibJPEGImageReader)      

at javax.imageio.ImageIO.read(ImageIO.java:1400)      

at javax.imageio.ImageIO.read(ImageIO.java:1322)

此错误仅在Sun应用服务器上发生,因此我怀疑这是Sun错误。


牛魔王的故事
浏览 1324回答 3
3回答

杨魅力

这是@Kay对伟大帖子的重写,它引发IOException并提供了较早的退出:/**&nbsp;* Gets image dimensions for given file&nbsp;&nbsp;* @param imgFile image file&nbsp;* @return dimensions of image&nbsp;* @throws IOException if the file is not a known image&nbsp;*/public static Dimension getImageDimension(File imgFile) throws IOException {&nbsp; int pos = imgFile.getName().lastIndexOf(".");&nbsp; if (pos == -1)&nbsp; &nbsp; throw new IOException("No extension for file: " + imgFile.getAbsolutePath());&nbsp; String suffix = imgFile.getName().substring(pos + 1);&nbsp; Iterator<ImageReader> iter = ImageIO.getImageReadersBySuffix(suffix);&nbsp; while(iter.hasNext()) {&nbsp; &nbsp; ImageReader reader = iter.next();&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; ImageInputStream stream = new FileImageInputStream(imgFile);&nbsp; &nbsp; &nbsp; reader.setInput(stream);&nbsp; &nbsp; &nbsp; int width = reader.getWidth(reader.getMinIndex());&nbsp; &nbsp; &nbsp; int height = reader.getHeight(reader.getMinIndex());&nbsp; &nbsp; &nbsp; return new Dimension(width, height);&nbsp; &nbsp; } catch (IOException e) {&nbsp; &nbsp; &nbsp; log.warn("Error reading: " + imgFile.getAbsolutePath(), e);&nbsp; &nbsp; } finally {&nbsp; &nbsp; &nbsp; reader.dispose();&nbsp; &nbsp; }&nbsp; }&nbsp; throw new IOException("Not a known image file: " + imgFile.getAbsolutePath());}我想我的代表不够高,因此我的意见不值得作为回应。

绝地无双

我发现了另一种读取图像大小的方法(更通用)。您可以将ImageIO类与ImageReaders一起使用。这是示例代码:private Dimension getImageDim(final String path) {&nbsp; &nbsp; Dimension result = null;&nbsp; &nbsp; String suffix = this.getFileSuffix(path);&nbsp; &nbsp; Iterator<ImageReader> iter = ImageIO.getImageReadersBySuffix(suffix);&nbsp; &nbsp; if (iter.hasNext()) {&nbsp; &nbsp; &nbsp; &nbsp; ImageReader reader = iter.next();&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ImageInputStream stream = new FileImageInputStream(new File(path));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; reader.setInput(stream);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int width = reader.getWidth(reader.getMinIndex());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int height = reader.getHeight(reader.getMinIndex());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result = new Dimension(width, height);&nbsp; &nbsp; &nbsp; &nbsp; } catch (IOException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log(e.getMessage());&nbsp; &nbsp; &nbsp; &nbsp; } finally {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; reader.dispose();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; log("No reader found for given format: " + suffix));&nbsp; &nbsp; }&nbsp; &nbsp; return result;}请注意,getFileSuffix是返回路径扩展名而不包含“。”的方法。因此例如:png,jpg等。示例实现为:private String getFileSuffix(final String path) {&nbsp; &nbsp; String result = null;&nbsp; &nbsp; if (path != null) {&nbsp; &nbsp; &nbsp; &nbsp; result = "";&nbsp; &nbsp; &nbsp; &nbsp; if (path.lastIndexOf('.') != -1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result = path.substring(path.lastIndexOf('.'));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (result.startsWith(".")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result = result.substring(1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return result;}该解决方案非常快,因为仅从文件读取图像大小,而不是从整个图像读取。我对其进行了测试,无法与ImageIO.read的性能进行比较。我希望有人会觉得有用。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java