Exif example for Visual J++

Note: Also works with Access 95 and 97.

This example does the following:

1. Reads all of the comments in an existing Exif file, updating the Comment array.

2. Loads and modifies the file's image.

3. Updates some of the comments in the comment array.

4. Saves the file.

5. Reads and displays the updated comments.

Note: Comments for GIF, TIFF, and DICOM files are simpler than the ones in this Exif example. With those file types, all of the comments are strings that you can get and set with simple assignments.

// Specify the file that we will update.
String strFilePath = "c:\\lead\\images\\exif1.tif";

// Get all of the current comments from an Exif file.
// Temporarily disable method errors so that we do not fail when comments are missing.
LEAD1.setEnableMethodErrors( false );
for( short i = 0 ; i <= LTOCXU.CommentConstants.CMNT_LAST ; i++ )
{
   LEAD1.setComment( i, new Variant() );
   LEAD1.setComment( i, LEAD1.ReadComment ( strFilePath, (short) 0, i ) );
}
LEAD1.setEnableMethodErrors( true );

// Load and modify the image.
LEAD1.Load( strFilePath, (short) 0, (short) 0, (short) 1 );
LEAD1.Reverse();

// Update the date when the file is generated.
String strThisDate = new Time().toString();
LEAD1.setComment( (short) LTOCXU.CommentConstants.CMNT_SZDATETIMEDIGITIZED, new Variant( strThisDate ) );
LEAD1.setComment( (short) LTOCXU.CommentConstants.CMNT_SZSUBSECTIMEDIGITIZED, new Variant() );

// Change the subject distance to 4 1/3 meters.
SafeArray aSubjectDistance = new SafeArray( Variant.VariantInt, 2 );
aSubjectDistance.setInt( 0, 13 );
aSubjectDistance.setInt( 1, 3 );
LEAD1.setComment( (short) LTOCXU.CommentConstants.CMNT_SUBJECTDISTANCE, new Variant( aSubjectDistance, true ) ); 

// Specify the user comment string.
String strMyCommentText = "This is my new comment.";
int nMyCommentSize = 8 + strMyCommentText.length();

// Define the array for the user comment.
char cMyComment[] = new char[ nMyCommentSize + 1 ];

// Fill in the ASCII prefix.
cMyComment[0] = 'A';
cMyComment[1] = 'S';
cMyComment[2] = 'C';
cMyComment[3] = 'I';
cMyComment[4] = 'I';
cMyComment[5] = 0;
cMyComment[6] = 0;
cMyComment[7] = 0;

// Fill in the rest of the comment, starting at the end.
cMyComment[ nMyCommentSize ] = 0;
for( int i = ( nMyCommentSize - 1 ) ; i >= 8 ; i-- )
   cMyComment[ i ] = strMyCommentText.charAt( nMyCommentSize - i - 1 );

SafeArray aMyComment = new SafeArray( Variant.VariantByte, nMyCommentSize + 1 );
aMyComment.fromCharArray( cMyComment );

// Update the user comment.
LEAD1.setComment( (short) LTOCXU.CommentConstants.CMNT_USERCOMMENT, new Variant() );
LEAD1.setComment( (short) LTOCXU.CommentConstants.CMNT_USERCOMMENT, new Variant( aMyComment, true ) );

// Save the file and read the comments that we saved.
LEAD1.Save( strFilePath, (short) LTOCXU.FileConstants.FILE_EXIF, (short) 24, (short) 0, (short) LTOCXU.SaveModifyConstants.SAVE_OVERWRITE );

String strNewDate = LEAD1.ReadComment( strFilePath, (short) 0, (short) LTOCXU.CommentConstants.CMNT_SZDATETIMEDIGITIZED ).toString();
SafeArray aNewSubjectDistance = LEAD1.ReadComment( strFilePath, (short) 0, (short) LTOCXU.CommentConstants.CMNT_SUBJECTDISTANCE ).toSafeArray();
String strNewComment = LEAD1.ReadComment( strFilePath, (short) 0, (short) LTOCXU.CommentConstants.CMNT_USERCOMMENT ).toString();

// Interpret the Subject-Distance RATIONAL.
// Initialize the string for the message box.
String strMyMsg = "Subject Distance = " + aNewSubjectDistance.getInt( 0 ) / aNewSubjectDistance.getInt( 1 ) + "\nDate/Time = " + strNewDate;

// Add the ASCII comment if it is there
if( strNewComment.charAt( 0 ) == 'A' ) 
{
   int nArraySize = strNewComment.length();
   String strMyNewComment = "";
   for( int i = 8 ; i <= nArraySize ; i ++ )
      strMyNewComment += strNewComment.charAt( i );
   strMyMsg += "\n\n" + strMyNewComment;
}

// Display the message
MessageBox.show( strMyMsg );

// Clear the comments from memory
for( int i = 0 ; i <= LTOCXU.CommentConstants.CMNT_LAST ; i++ )
   LEAD1.setComment( (short) i, new Variant() );