private void writeExif ( ) throws IOException { IIOMetadata metadata = jpegReader . getImageMetadata ( 0 ) ; // names says which exif tree to get - 0 for jpeg 1 for the default String [ ] names = metadata . getMetadataFormatNames ( ) ; IIOMetadataNode root = ( IIOMetadataNode ) metadata . getAsTree ( names [ 0 ] ) ; // exif is on the app1 node called unknown NodeList nList = root . getElementsByTagName ( "unknown" ) ; IIOMetadataNode app1EXIFNode = ( IIOMetadataNode ) nList . item ( 0 ) ; ArrayList < IIOMetadata > md = readExif ( app1EXIFNode ) ; IIOMetadata exifMetadata = md . get ( 0 ) ; // insert the gps data into the exif exifMetadata = insertGPSCoords ( exifMetadata ) ; // create a new exif node IIOMetadataNode app1NodeNew = createNewExifNode ( exifMetadata , null , null ) ; // copy the user data accross app1EXIFNode . setUserObject ( app1NodeNew . getUserObject ( ) ) ; // write to a new image file FileImageOutputStream out1 = new FileImageOutputStream ( new File ( "GPS_" + imageFile . getName ( ) ) ) ; jpegWriter . setOutput ( out1 ) ; metadata . setFromTree ( names [ 0 ] , root ) ; IIOImage image = new IIOImage ( jpegReader . readAsRenderedImage ( 0 , jpegReader . getDefaultReadParam ( ) ) , null , metadata ) ; // write out the new image jpegWriter . write ( jpegReader . getStreamMetadata ( ) , image , jpegWriter . getDefaultWriteParam ( ) ) ; }
Main method to write the gps data to the exif