Пример #1
0
  public static void main(String[] args) throws Exception {
    File f1 = new File(PrepImage.class.getResource("IMG2009-11-11.JPG").toURI());
    File f2 = new File(f1.getParent(), "alt.JPG");

    LLJTran llj;
    Exif e;

    llj = new LLJTran(f1);
    llj.read(LLJTran.READ_HEADER, true);
    e = (Exif) llj.getImageInfo();

    IFD mainIFD = e.getIFDs()[0];
    IFD exifIFD = mainIFD.getIFD(Exif.EXIFOFFSET);
    IFD gpsIfd = mainIFD.getIFD(Exif.GPSINFO);

    IFD data = exifIFD.getIFD(Exif.APPLICATIONNOTE);
    if (data == null) {
      data = new IFD(Exif.APPLICATIONNOTE, Exif.LONG);
      exifIFD.addIFD(data);
    }
    Entry entry;

    if (gpsIfd == null) {
      System.out.println("Gps IFD not found adding..");
      gpsIfd = new IFD(Exif.GPSINFO, Exif.LONG);
      mainIFD.addIFD(gpsIfd);
    }
    // Set Latitude
    entry = new Entry(Exif.ASCII);
    entry.setValue(0, 'N');
    gpsIfd.setEntry(new Integer(Exif.GPSLatitudeRef), 0, entry);
    entry = new Entry(Exif.RATIONAL);
    entry.setValue(0, new Rational(45.5f));
    gpsIfd.setEntry(new Integer(Exif.GPSLatitude), 0, entry);

    // Set Longitude
    entry = new Entry(Exif.ASCII);
    entry.setValue(0, 'W');
    gpsIfd.setEntry(new Integer(Exif.GPSLongitudeRef), 0, entry);
    entry = new Entry(Exif.RATIONAL);
    entry.setValue(0, new Rational(16.5f));
    gpsIfd.setEntry(new Integer(Exif.GPSLongitude), 0, entry);

    entry = new Entry(Exif.ASCII);
    entry.setValue(
        0, "Application-specific data of the Nikki GPS/Photo log tool http://www.brazzy.de/nikki");
    data.addEntry(1, entry);

    entry = new Entry(Exif.ASCII);
    entry.setValue(0, "Australia/North");
    data.addEntry(2, entry); // Timezone

    entry = new Entry(Exif.UNDEFINED);
    byte[] title = "Überschrift".getBytes("UTF-8");
    for (int i = title.length - 1; i >= 0; i--) {
      entry.setValue(i, Integer.valueOf(title[i]));
    }
    data.addEntry(3, entry);

    entry = new Entry(Exif.UNDEFINED);
    byte[] comment = "Kommentar\näöüß".getBytes("UTF-8");
    for (int i = comment.length - 1; i >= 0; i--) {
      entry.setValue(i, Integer.valueOf(comment[i]));
    }
    data.addEntry(4, entry);

    entry = new Entry(Exif.BYTE);
    entry.setValue(0, 1);
    data.addEntry(5, entry); // Export

    InputStream fip = new BufferedInputStream(new FileInputStream(f1));
    OutputStream out = new BufferedOutputStream(new FileOutputStream(f2));
    llj.refreshAppx();
    llj.xferInfo(fip, out, LLJTran.REPLACE, LLJTran.RETAIN);
    fip.close();
    out.close();

    llj.freeMemory();
    System.out.println(f2.getPath());

    llj = new LLJTran(f2);
    llj.read(LLJTran.READ_HEADER, true);
    e = (Exif) llj.getImageInfo();
    for (IFD i : e.getIFDs()) {
      print(i, 0);
    }

    Object[] commentValues =
        mainIFD.getIFD(Exif.EXIFOFFSET).getIFD(Exif.APPLICATIONNOTE).getEntry(4, 0).getValues();
    comment = new byte[commentValues.length];
    for (int i = 0; i < commentValues.length; i++) {
      comment[i] = (byte) ((Integer) commentValues[i]).intValue();
    }
    System.out.println("xyzzy");
    System.out.println("found comment " + Exif.USERCOMMENT + ": " + new String(comment, "UTF-8"));
    System.out.println("xyzzy");

    /*
     * System.out.println("found comment "+Exif.USERCOMMENT+": "+e.getTagValue
     * (Exif.USERCOMMENT, true));
     * System.out.println("found description "+Exif
     * .IMAGEDESCRIPTION+": "+e.getTagValue(Exif.IMAGEDESCRIPTION,true));
     * System
     * .out.println("found offset "+0x882a+": "+e.getTagValue(0x882a,true));
     */
  }