Results 1 to 1 of 1
  1. #1
    unixhead is offline Member
    Join Date
    Jul 2012
    Rep Power

    Default [Sanselan] Writing exif metadata to jpeg

    Hello :)
    I want to edit GPS tag and save new GPS latitude and longitude to jpeg. Ok I found a Sanselan Java library which lets to do this. There is an example how to write metadata org.apache.sanselan.sampleUsage.WriteExifMetadataE xample Source Code | end I used this part
    Java Code:
    package test1.gpstest1;
    import org.apache.sanselan.ImageReadException;
    import org.apache.sanselan.ImageWriteException;
    import org.apache.sanselan.Sanselan;
    import org.apache.sanselan.common.IImageMetadata;
    import org.apache.sanselan.formats.jpeg.JpegImageMetadata;
    import org.apache.sanselan.formats.jpeg.exifRewrite.ExifRewriter;
    import org.apache.sanselan.formats.tiff.TiffImageMetadata;
    import org.apache.sanselan.formats.tiff.constants.TiffConstants;
    import org.apache.sanselan.formats.tiff.write.TiffOutputDirectory;
    import org.apache.sanselan.formats.tiff.write.TiffOutputField;
    import org.apache.sanselan.formats.tiff.write.TiffOutputSet;
    //import org.apache.sanselan.util.IOUtils;
    public class Testgps
    	 * This example illustrates how to add/update EXIF metadata in a JPEG file.
    	 * @param jpegImageFile
    	 *            A source image file.
    	 * @param dst
    	 *            The output file.
    	 * @throws IOException
    	 * @throws ImageReadException
    	 * @throws ImageWriteException
    	public void changeExifMetadata(File jpegImageFile, File dst)
    			throws IOException, ImageReadException, ImageWriteException
    		OutputStream os = null;
    			TiffOutputSet outputSet = null;
    			// note that metadata might be null if no metadata is found.
    			IImageMetadata metadata = Sanselan.getMetadata(jpegImageFile);
    			JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata;
    			if (null != jpegMetadata)
    				// note that exif might be null if no Exif metadata is found.
    				TiffImageMetadata exif = jpegMetadata.getExif();
    				if (null != exif)
    					// TiffImageMetadata class is immutable (read-only).
    					// TiffOutputSet class represents the Exif data to write.
    					// Usually, we want to update existing Exif metadata by
    					// changing
    					// the values of a few fields, or adding a field.
    					// In these cases, it is easiest to use getOutputSet() to
    					// start with a "copy" of the fields read from the image.
    					outputSet = exif.getOutputSet();
    			// if file does not contain any exif metadata, we create an empty
    			// set of exif metadata. Otherwise, we keep all of the other
    			// existing tags.
    			if (null == outputSet)
    				outputSet = new TiffOutputSet();
    				// Example of how to add a field/tag to the output set.
    				// Note that you should first remove the field/tag if it already
    				// exists in this directory, or you may end up with duplicate
    				// tags. See above.
    				// Certain fields/tags are expected in certain Exif directories;
    				// Others can occur in more than one directory (and often have a
    				// different meaning in different directories).
    				// TagInfo constants often contain a description of what
    				// directories are associated with a given tag.
    				// see
    				// org.apache.sanselan.formats.tiff.constants.AllTagConstants
    				TiffOutputField aperture = TiffOutputField.create(
    						outputSet.byteOrder, new Double(0.3));
    				TiffOutputDirectory exifDirectory = outputSet
    				// make sure to remove old value if present (this method will
    				// not fail if the tag does not exist).
    				// Example of how to add/update GPS info to output set.
    				// New York City
    				double longitude = -74.0; // 74 degrees W (in Degrees East)
    				double latitude = 40 + 43 / 60.0; // 40 degrees N (in Degrees
    				// North)
    				outputSet.setGPSInDegrees(longitude, latitude);
    			// printTagValue(jpegMetadata, TiffConstants.TIFF_TAG_DATE_TIME);
    			//os = new FileOutputStream(dst);
    			os = new BufferedOutputStream(os);
    			new ExifRewriter().updateExifMetadataLossless(jpegImageFile, os,
    			os = null;
    		} finally
    			if (os != null)
    				} catch (IOException e)
    public static void main(String [] args) throws ImageReadException, 
    								       IOException {
    		File jpegImageFile = new File("test22.JPG");
    		//String jpegImageFile ="P1020220.JPG";
    		File dst = new File("test23.JPG");
    	     new Testgps().changeExifMetadata(jpegImageFile, dst);
    	     System.out.println("Data has been changed successfull!");
    ok it works but this example creates a new photo with new gps latitude/longitude. I would like to set new gps values on the same photo, how I can do this ? Generally I want to create application similar to Picasa (photo viewer + google maps) .. and in this case when I drag and drop marker on map I want to save new gps exif metadata to photo.. so according with this example it will be create new photo every time? stupid ...I have a little experience with Java .. any sygestion/example is very usefull for me :)
    Last edited by unixhead; 07-13-2012 at 07:30 PM.

Similar Threads

  1. Writing Exif Metadata to JPEG
    By lichtemo in forum Advanced Java
    Replies: 4
    Last Post: 04-04-2014, 11:44 PM
  2. Why Metadata is different in different databases?
    By Arun_N in forum New To Java
    Replies: 7
    Last Post: 10-24-2011, 01:08 PM
  3. Help on reading in EXIF data in Java
    By Eric Chua in forum Advanced Java
    Replies: 3
    Last Post: 07-18-2011, 09:32 PM
  4. jpg metadata
    By dinosoep in forum Java 2D
    Replies: 1
    Last Post: 11-20-2009, 03:11 PM
  5. Media Metadata
    By soulspirit in forum Advanced Java
    Replies: 0
    Last Post: 11-26-2007, 11:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts