package com.simplewatermark;

import org.apache.sanselan.Sanselan;
import org.apache.sanselan.ImageReadException;
import org.apache.sanselan.common.IImageMetadata;
import org.apache.sanselan.formats.jpeg.JpegImageMetadata;
import org.apache.sanselan.formats.tiff.TiffField;
import org.apache.sanselan.formats.tiff.constants.ExifTagConstants;
import org.apache.sanselan.formats.tiff.constants.TiffTagConstants;

import org.apache.sanselan.formats.tiff.TiffImageMetadata;
import org.apache.sanselan.formats.jpeg.exifRewrite.ExifRewriter;
import org.apache.sanselan.formats.tiff.write.TiffOutputSet;
import org.apache.sanselan.formats.tiff.write.TiffOutputDirectory;
import org.apache.sanselan.ImageWriteException;
import java.io.File;
import java.io.FileOutputStream;
import java.io.BufferedOutputStream;

import java.io.InputStream;
import java.io.BufferedInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class Exif {
  final JpegImageMetadata metadata;

  private Exif(JpegImageMetadata metadata) {
    super();
    this.metadata = metadata;
  }

  public static Exif createInstance(final InputStream is) {
    JpegImageMetadata jpegMetadata = null;
    try {
      IImageMetadata metadata = Sanselan.getMetadata(is, null);
      is.close();
      if (metadata instanceof JpegImageMetadata) {
        jpegMetadata = (JpegImageMetadata) metadata;
      }
    } catch (ImageReadException e) {
    } catch (IOException e) {
    }
    return new Exif(jpegMetadata);
  }

  public int getOrientation() {
    int orientation = 0;

    if (metadata == null) {
      return 0;
    }

    TiffField field = metadata.findEXIFValue(ExifTagConstants.EXIF_TAG_ORIENTATION);
    if (field == null) {
      return 0;
    }

    try {
      orientation = field.getIntValue();
    } catch (ImageReadException e) {
    }

    return orientation;
  }

  private TiffOutputSet getExif() {
    if (metadata == null) {
      return new TiffOutputSet();
    }

    final TiffImageMetadata exif = metadata.getExif();
    if (exif == null) {
      return new TiffOutputSet();
    }

    TiffOutputSet outputSet;

    try {
      outputSet = exif.getOutputSet();
      outputSet.removeField(ExifTagConstants.EXIF_TAG_ORIENTATION);
      // outputSet.removeField(TiffTagConstants.TIFF_TAG_ORIENTATION);
    } catch (ImageWriteException e) {
      return new TiffOutputSet();
    }

    // try {
    //   final TiffOutputDirectory exifDirectory = outputSet.getOrCreateExifDirectory();
    //   exifDirectory.removeField(ExifTagConstants.EXIF_TAG_ORIENTATION);
    //   exifDirectory.removeField(TiffTagConstants.TIFF_TAG_ORIENTATION);
    // } catch (ImageWriteException e) {
    // }
    return outputSet;
  }

  public void copyTo(final String filename) {
    final String from = filename;
    final String to = from+".exif";
    TiffOutputSet outputSet = getExif();
    File f;
    FileOutputStream fos;

    try {
      f = new File(from);
      fos = new FileOutputStream(to);
    } catch (FileNotFoundException e) {
      return;
    }

    BufferedOutputStream bos = new BufferedOutputStream(fos);
    try {
      new ExifRewriter().updateExifMetadataLossless(f, bos, outputSet);

      f.delete();
      bos.close();
      File file = new File(to);
      file.renameTo(f);
    } catch (ImageReadException e) {
    } catch (ImageWriteException e) {
    } catch (IOException e) {
    }
  }
}

@@ -379,34 +379,76 @@
 iff.
-constants.TiffTagConstants
+TiffImageData;%0Aimport org.apache.sanselan.formats.tiff.JpegImageData
 ;%0A%0Ai
@@ -871,44 +871,8 @@
 am;%0A
-import java.io.BufferedInputStream;%0A
 impo
@@ -2246,264 +2246,341 @@
 N);%0A
+%0A
       
-// outputSet.removeField(TiffTagConstants.TIFF_TAG_ORIENTATION);%0A    %7D catch (ImageWriteException e) %7B%0A      return new TiffOutputSet(
+for (Object obj : outputSet.getDirectories()) %7B%0A        TiffOutputDirectory directory = (TiffOutputDirectory) obj;%0A        JpegImageData jpeg = directory.getRawJpegImageData();%0A        if (jpeg != null) %7B%0A          directory.setJpegImageData(null
 );%0A    
-%7D%0A%0A
     
-// try %7B
+%7D%0A
 %0A    
-//
    
-final TiffOutputDirectory exifDirectory = outputSet.getOrCreateExifDirectory
+ TiffImageData tiff = directory.getRawTiffImageData
 ();%0A    
 //  
@@ -2579,93 +2579,43 @@
     
-//   exifDirectory.removeField(ExifTagConstants.EXIF_TAG_ORIENTATION);%0A
+    if (tiff != null) %7B%0A   
     
-//
    
-exifD
+d
 irec
@@ -2623,66 +2623,53 @@
 ory.
-removeField(TiffTagConstants.TIFF_TAG_ORIENTATION);%0A    //
+setTiffImageData(null);%0A        %7D%0A      %7D%0A   
  %7D c
@@ -2707,13 +2707,45 @@
     
-//
+  return new TiffOutputSet();%0A   
  %7D%0A
+%0A
     

Remove thumbnail exif