exif 修改使用

前几天项目需要修改,读取exif。以前完全没有接触过。于是上网找项目= =。读取很快就搞定了。但是修改换了2个第三方的包都不行。尤其(mediautil-1.0.jar)。安装开源中国里面给的补丁。还是不行。后来想直接修改,但是看了很久,发现几张图片的文件头都不一样。直接改难度比较大。

微笑上面前情,下面正文。

commons-imaging Apache 的项目

官网里面的好像下不了了。在站里资源里面找到了。下面是代码。


 
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
 
import org.apache.commons.imaging.ImageReadException;
import org.apache.commons.imaging.ImageWriteException;
import org.apache.commons.imaging.Imaging;
import org.apache.commons.imaging.common.IImageMetadata;
import org.apache.commons.imaging.common.RationalNumber;
import org.apache.commons.imaging.formats.jpeg.JpegImageMetadata;
import org.apache.commons.imaging.formats.jpeg.exif.ExifRewriter;
import org.apache.commons.imaging.formats.tiff.TiffImageMetadata;
import org.apache.commons.imaging.formats.tiff.constants.ExifTagConstants;
import org.apache.commons.imaging.formats.tiff.write.TiffOutputDirectory;
import org.apache.commons.imaging.formats.tiff.write.TiffOutputSet;
import org.apache.commons.imaging.util.IoUtils;
 
public class WriteExifMetadataExample {
    public void removeExifMetadata(final File jpegImageFile, final File dst)
            throws IOException, ImageReadException, ImageWriteException {
        OutputStream os = null;
        try {
            os = new FileOutputStream(dst);
            os = new BufferedOutputStream(os);
 
            new ExifRewriter().removeExifMetadata(jpegImageFile, os);
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (final IOException e) {
 
                }
            }
        }
    }
    public static void main(String[] args) {
    	File old=new File("E://dd.jpg");
    	File news=new File("E://ddi.jpg");
    	WriteExifMetadataExample we=new WriteExifMetadataExample();
    	try {
			we.changeExifMetadata(old,news);
		} catch (ImageReadException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ImageWriteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
    /**
     * This example illustrates how to add/update EXIF metadata in a JPEG file.
     *
     * @param jpegImageFile
     *            A source image file.
     * @param dst
     *            The output file.
     * @throws IOException
     * @throws ImageReadException
     * @throws ImageWriteException
     */
    public void changeExifMetadata(final File jpegImageFile, final File dst)
            throws IOException, ImageReadException, ImageWriteException {
        OutputStream os = null;
        try {
            TiffOutputSet outputSet = null;
 
            // note that metadata might be null if no metadata is found.
            final IImageMetadata 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.
                //
                // see
                // org.apache.commons.sanselan.formats.tiff.constants.AllTagConstants
                //
                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,
                        RationalNumber.factoryMethod(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);
            }
 
            // printTagValue(jpegMetadata, TiffConstants.TIFF_TAG_DATE_TIME);
 
            os = new FileOutputStream(dst);
            os = new BufferedOutputStream(os);
 
            new ExifRewriter().updateExifMetadataLossless(jpegImageFile, os,
                    outputSet);
 
            os.close();
            os = null;
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (final IOException e) {
 
                }
            }
        }
    }
 
    /**
     * This example illustrates how to remove a tag (if present) from EXIF
     * metadata in a JPEG file.
     *
     * In this case, we remove the "aperture" tag from the EXIF metadata if
     * present.
     *
     * @param jpegImageFile
     *            A source image file.
     * @param dst
     *            The output file.
     * @throws IOException
     * @throws ImageReadException
     * @throws ImageWriteException
     */
    public void removeExifTag(final File jpegImageFile, final File dst) throws IOException,
            ImageReadException, ImageWriteException {
        OutputStream os = null;
        try {
            TiffOutputSet outputSet = null;
 
            // note that metadata might be null if no metadata is found.
            final IImageMetadata 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 (null == outputSet) {
                // file does not contain any exif metadata. We don't need to
                // update the file; just copy it.
                IoUtils.copyFileNio(jpegImageFile, dst);
                return;
            }
 
            {
                // Example of how to remove a single tag/field.
                // There are two ways to do this.
 
                // Option 1: brute force
                // Note that this approach is crude: Exif data is organized in
                // directories. The same tag/field may appear in more than one
                // directory, and have different meanings in each.
                outputSet.removeField(ExifTagConstants.EXIF_TAG_APERTURE_VALUE);
 
                // Option 2: precision
                // We know the exact directory the tag should appear in, in this
                // case the "exif" directory.
                // One complicating factor is that in some cases, manufacturers
                // will place the same tag in different directories.
                // To learn which directory a tag appears in, either refer to
                // the constants in ExifTagConstants.java or go to Phil Harvey's
                // EXIF website.
                final TiffOutputDirectory exifDirectory = outputSet
                        .getExifDirectory();
                if (null != exifDirectory) {
                    exifDirectory
                            .removeField(ExifTagConstants.EXIF_TAG_APERTURE_VALUE);
                }
            }
 
            os = new FileOutputStream(dst);
            os = new BufferedOutputStream(os);
 
            new ExifRewriter().updateExifMetadataLossless(jpegImageFile, os,
                    outputSet);
 
            os.close();
            os = null;
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (final IOException e) {
 
                }
            }
        }
    }
 
    /**
     * This example illustrates how to set the GPS values in JPEG EXIF metadata.
     *
     * @param jpegImageFile
     *            A source image file.
     * @param dst
     *            The output file.
     * @throws IOException
     * @throws ImageReadException
     * @throws ImageWriteException
     */
    public void setExifGPSTag(final File jpegImageFile, final File dst) throws IOException,
            ImageReadException, ImageWriteException {
        OutputStream os = null;
        try {
            TiffOutputSet outputSet = null;
 
            // note that metadata might be null if no metadata is found.
            final IImageMetadata 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/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);
            }
 
            os = new FileOutputStream(dst);
            os = new BufferedOutputStream(os);
 
            new ExifRewriter().updateExifMetadataLossless(jpegImageFile, os,
                    outputSet);
 
            os.close();
            os = null;
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (final IOException e) {
 
                }
            }
        }
    }
 
}





 
Photo Exif Editor 是一款功能强大的照片 EXIF 编辑软件,可以用于修改照片的元数据信息。使用这款软件非常简单,下面我来详细介绍一下使用方法。 首先,打开 Photo Exif Editor 软件。你可以从官方网站下载并安装它,然后在计算机上打开软件。 其次,导入要编辑的照片。你可以选择从计算机上的文件夹中直接拖拽照片到软件界面,或者点击软件界面上的 "导入" 按钮,然后选择要编辑的照片。 接下来,选择要编辑的照片的 EXIF 信息。软件界面会显示照片的元数据信息,如拍摄日期、照相机型号、光圈、曝光时间等等。你可以根据需要选择要编辑的特定信息。 然后,进行编辑。点击你选择要编辑的信息旁边的 "编辑" 按钮,然后输入你想要修改为的新值。例如,你可以编辑拍摄日期,将其修改为你想要的日期。同样地,你还可以编辑其他的元数据信息。 最后,保存所做的编辑。点击软件界面上的 "保存" 按钮,然后选择保存照片的路径和文件名。软件会将修改后的照片保存到指定的位置。 需要注意的是,修改照片的 EXIF 信息可能会导致一些问题,比如照片的日期与实际拍摄日期不一致等。因此,在使用 Photo Exif Editor 进行编辑之前,最好提前备份原始照片,以防不必要的麻烦。 综上所述,Photo Exif Editor 是一款非常方便易用的照片 EXIF 编辑软件。通过简单的几个步骤,你可以轻松地修改照片的元数据信息,实现你想要的效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值