import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import com.drew.imaging.ImageProcessingException;
import com.drew .imaging .jpeg.JpegMetadataReader;
import com.drew.metadata .Directory;
import com.drew .metadata.Metadata;
import com.drew.metadata .Tag;
import com.drew.metadata.exif.*;
import mediautil.image.jpeg.*;
import java.io.FileOutputStream;
public static void main(String[] args) throws Exception
{
LLJTran llj = new LLJTran(new File("E:\2012_07_17\IMG_0665.JPG"));
// readImage(llj, true, LLJTran.READ_INFO, 0, 0);
llj.read(LLJTran.READ_INFO,true);
mediautil.image.jpeg.Exif exifq=(Exif)llj.getImageInfo();
Entry entry = exifq.getTagValue(Exif.USERCOMMENT, true);
entry.setValue(0,"xxxxxxxxxxx");
llj.refreshAppx();
FileOutputStream nhop = new FileOutputStream("E:\\2012_07_17\\IMG_0665.JPG");
llj.xferInfo(null, nhop, LLJTran.REPLACE, LLJTran.REPLACE);
nhop.close();
}
总是报这个错。mediautil.image.jpeg.JPEG cannot be cast to mediautil.image.jpeg.Exif
去看下官方的示例 有些图片没有Exif头 需要虚拟一个Exif头
// 3. If the Image does not have an Exif Header create a dummy Exif
// Header
if(!(imageInfo instanceof Exif))
{
System.out.println("Adding a Dummy Exif Header");
llj.addAppx(LLJTran.dummyExifHeader, 0,
LLJTran.dummyExifHeader.length, true);
imageInfo = llj.getImageInfo(); // This would have changed
Exif exif = (Exif) imageInfo;
// Changed Date/Time and dimensions in Dummy Exif
Entry entry = exif.getTagValue(Exif.DATETIME, true);
if(entry != null)
entry.setValue(0, "1998:08:18 11:15:00");
entry = exif.getTagValue(Exif.DATETIMEORIGINAL, true);
if(entry != null)
entry.setValue(0, "1998:08:18 11:15:00");
entry = exif.getTagValue(Exif.DATETIMEDIGITIZED, true);
if(entry != null)
entry.setValue(0, "1998:08:18 11:15:00");
int imageWidth = llj.getWidth();
int imageHeight = llj.getHeight();
if(imageWidth > 0 && imageHeight > 0)
{
entry = exif.getTagValue(Exif.EXIFIMAGEWIDTH, true);
if(entry != null)
entry.setValue(0, new Integer(imageWidth));
entry = exif.getTagValue(Exif.EXIFIMAGELENGTH, true);
if(entry != null)
entry.setValue(0, new Integer(imageHeight));
}
}
附上链接 main3方法
表示一样问题,现在还没解决.
Exception in thread "main" java.lang.ClassCastException: mediautil.image.jpeg.JPEG