public static int getImageWidth ( String imageLoc ) { // Get the image as an InputStream InputStream is = null ; try { URI imageUri = new URI ( imageLoc ) ; URL imageUrl = imageUri . toURL ( ) ; is = imageUrl . openStream ( ) ; } catch ( Exception e ) { jlogger . log ( Level . SEVERE , "getImageWidth" , e ) ; return - 1 ; } // Determine the image width try { // Create an image input stream on the image ImageInputStream iis = ImageIO . createImageInputStream ( is ) ; // Find all image readers that recognize the image format Iterator iter = ImageIO . getImageReaders ( iis ) ; // No readers found if ( ! iter . hasNext ( ) ) { return - 1 ; } // Use the first reader ImageReader reader = ( ImageReader ) iter . next ( ) ; reader . setInput ( iis , true ) ; int width = reader . getWidth ( 0 ) ; iis . close ( ) ; return width ; } catch ( IOException e ) { jlogger . log ( Level . SEVERE , "getImageWidth" , e ) ; // The image could not be read } return - 1 ; }
Determines the width of the first image in an image file in pixels .