丑小子0000 2014-03-04 16:02 采纳率: 0%
浏览 2672

图片Exif信息修改代码编译时报错

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

  • 写回答

2条回答 默认 最新

  • 迷 笛 2022-04-25 10:53
    关注

    去看下官方的示例 有些图片没有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方法

    评论

报告相同问题?

悬赏问题

  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥20 为什么我写出来的绘图程序是这样的,有没有lao哥改一下
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号