2016-04-26 9 views
4

Ich möchte Eigenschaften jpg-Dateien wie bearbeiten: Kommentare, Titel, Aufnahmedatum, Kamerahersteller usw.Bearbeiten von JPEG-EXIF-Daten in Java

enter image description here

I Bibliotheken haben gefunden, diese Daten zu lesen. Aber ich brauche eine freie Bibliothek mit Beispielen zu bearbeiten sie.

Ich kenne Apache-Bildgebung (Sanselan). Aber ich konnte damit keine Daten bearbeiten. Wenn Sie es zuvor selbst verwendet haben, würde ich das als Antwort nur akzeptieren, wenn Sie einen anderen Beispielcode als den auf ihrer Website angeben. Denn auch wenn ich ihr Beispiel nutze, konnte ich keine andere Eigenschaft als GPS-Daten bearbeiten. Nachdem ich den Code ausgeführt habe, haben Dateieigenschaften-Details immer noch dieselben Werte.

Danke!

Hinweis: Ich habe auch versucht, JHeader (https://sourceforge.net/projects/jheader/), aber es als ein Prozess mit -cl Option noch nicht geänderten Eigenschaften Liste verwendet.

Antwort

7

Apache Commons Imaging funktioniert für mich.

ich die Probe here

So sieht mein Client-Code offensichtlich wie dieser

public static void main(String[] args) throws ImageWriteException, ImageReadException, IOException { 
    new WriteExifMetadataExample().changeExifMetadata(new File("somefilename.jpg"), new File("result_file.jpg")); 
} 

und die erweiterte Methode in WriteExifMetadataExample

public void changeExifMetadata(final File jpegImageFile, final File dst) 
     throws IOException, ImageReadException, ImageWriteException { 
    OutputStream os = null; 
    boolean canThrow = false; 
    try { 
     TiffOutputSet outputSet = null; 

     // note that metadata might be null if no metadata is found. 
     final ImageMetadata metadata = Imaging.getMetadata(jpegImageFile); 
     final JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata; 
     if (null != jpegMetadata) { 
      // note that exif might be null if no Exif metadata is found. 
      final 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. 
      // 
      final TiffOutputDirectory exifDirectory = outputSet 
        .getOrCreateExifDirectory(); 
      // make sure to remove old value if present (this method will 
      // not fail if the tag does not exist). 
      exifDirectory 
        .removeField(ExifTagConstants.EXIF_TAG_APERTURE_VALUE); 
      exifDirectory.add(ExifTagConstants.EXIF_TAG_APERTURE_VALUE, 
        new RationalNumber(3, 10)); 
     } 

     { 
      // Example of how to add/update GPS info to output set. 

      // New York City 
      final double longitude = -74.0; // 74 degrees W (in Degrees East) 
      final double latitude = 40 + 43/60.0; // 40 degrees N (in Degrees 
      // North) 

      outputSet.setGPSInDegrees(longitude, latitude); 
     } 



     final TiffOutputDirectory exifDirectory = outputSet 
       .getOrCreateRootDirectory(); 
     exifDirectory 
       .removeField(ExifTagConstants.EXIF_TAG_SOFTWARE); 
     exifDirectory.add(ExifTagConstants.EXIF_TAG_SOFTWARE, 
       "SomeKind"); 

     os = new FileOutputStream(dst); 
     os = new BufferedOutputStream(os); 

     new ExifRewriter().updateExifMetadataLossless(jpegImageFile, os, 
       outputSet); 

     canThrow = true; 
    } finally { 
     IoUtils.closeQuietly(canThrow, os); 
    } 
} 

Aufmerksamkeit Bitte zahlen Sie nur auf Leitung erweitert, wo Ich füge zusätzliches Tag hinzu

final TiffOutputDirectory exifDirectory = outputSet 
       .getOrCreateRootDirectory(); 
     exifDirectory 
       .removeField(ExifTagConstants.EXIF_TAG_SOFTWARE); 
     exifDirectory.add(ExifTagConstants.EXIF_TAG_SOFTWARE, 
       "SomeKind"); 

als Ergebnis wurde EXIF-Tag hinzugefügt richtig

enter image description here


Um die Kommentare Tag ändern Sie die folgende

 final TiffOutputDirectory exifDirectory = outputSet.getOrCreateRootDirectory(); 
     exifDirectory.removeField(MicrosoftTagConstants.EXIF_TAG_XPCOMMENT); 
     exifDirectory.add(MicrosoftTagConstants.EXIF_TAG_XPCOMMENT, "SomeKind"); 

die vollständige Liste der verfügbaren Konstanten in der tun kann, ist, Paket:

org.apache.commons.imaging.formats.tiff.constants 

enter image description here

+0

Früher habe ich die Sanselan-Bibliothek verwendet, da sie dachte, dass sie funktional mit Imaging identisch wäre (ich konnte das Imaging-Glas nicht herunterladen). Und tatsächlich hatte ich Probleme mit Sanselans Beispiel. Ich werde Imaging by Maven mit Ihrem Code ausprobieren und die Ergebnisse melden. –

+0

Ihr Code ist erfolgreich bei der Bearbeitung von Program Name Tag, aber wie ich sagte, ich musste Tags wie Kommentare, Titel, Aufnahmedatum, Kamerahersteller aktualisieren. Ich konnte keine Dokumentation finden, die erklärt, was ExifTagConstants was zuordnet. Außerdem sollte der Datentyp des Werts für die add-Methode verwendet werden. –

+0

MicrosoftTagConstants funktionierte wie Magie. Danke vielmals. Ich werde auch andere Konstanten suchen. –

2

wäre ein Beispiel wie this work for you?

Ich gehe davon würde Pakete wie org.apache.commons.imaging.util.IoUtils und Import org.apache.commons.imaging.Imaging verwenden würde hier Ihnen eine große Hilfe sein .

+0

I @yurko erraten habe schon die selbe Bibliothek erwähnt. Ich werde es testen und die Ergebnisse sehr danken. –

2

Um die Kommentare Tag ändern können Sie wie folgt vorgehen

 final TiffOutputDirectory exifDirectory = outputSet.getOrCreateRootDirectory(); 
     exifDirectory.removeField(MicrosoftTagConstants.EXIF_TAG_XPCOMMENT); 
     exifDirectory.add(MicrosoftTagConstants.EXIF_TAG_XPCOMMENT, "SomeKind"); 

die vollständige Liste der verfügbaren Konstanten in dem Paket ist:

org.apache.commons.imaging.formats.tiff.constants 

enter image description here