0
Follow
0
View

The ExifInterface setAttribute() setting ExposureTime loses precision

d619217605 注册会员
2023-02-28 00:22

you can try to use ExifInterface. SetAttribute(String tag, long value) method to set the exposure time(TAG_EXPOSURE_TIME), which will allow you to specify the value of the label as a long type, avoiding loss of precision when using a double cast to long.

Here is the sample code:


```c
ExifInterface exif = new ExifInterface(filePath);
exif.setAttribute(ExifInterface.TAG_EXPOSURE_TIME, 1L/4000);
exif.saveAttributes();


Note that in this example, 1L/4000 will be treated as a value of type long, and the precision will be retained to set the value of the label correctly.


duhai668 注册会员
2023-02-28 00:22

the reference answer GPT ᴼ ᴾ ᴱ ᴺ ᴬ ᴵ < br / > ExifInterface is provided by the Android API, Its setAttribute method internally converts floating-point values to finite decimals represented by fractions, so it can suffer from precision loss. To avoid precision loss, you can use the third-party library Metadata-extractor for reading and writing Exif information, which supports handling more Exif data types, such as floating-point values. Here is a sample code for writing Exif information using the Metadata-extractor library:

// 导入库
import com.drew.imaging.ImageMetadataReader;
import com.drew.imaging.ImageProcessingException;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.exif.ExifSubIFDDirectory;
import com.drew.metadata.exif.ExifSubIFDDescriptor;
import com.drew.metadata.exif.ExifIFD0Directory;
import com.drew.metadata.exif.ExifIFD0Descriptor;

// 读取Exif信息
File imageFile = new File("path/to/image");
Metadata metadata = ImageMetadataReader.readMetadata(imageFile);

// 获取Exif子标签信息
ExifSubIFDDirectory subIFDDirectory = metadata.getFirstDirectoryOfType(ExifSubIFDDirectory.class);
ExifSubIFDDescriptor subIFDDescriptor = new ExifSubIFDDescriptor(subIFDDirectory);

// 写入曝光时间
subIFDDirectory.setObject(ExifSubIFDDirectory.TAG_EXPOSURE_TIME, new Rational(1, 4000));

// 保存Exif信息到文件
FileOutputStream fos = new FileOutputStream(imageFile);
fos.write(metadata.getByteArray());
fos.close();

The sample code reads the Exif information in an image file using the Metadata-extractor library, then uses the ExifSubIFDDirectory and ExifSubIFDDescriptor objects to get the sublabel information for the Exif information, Finally, Rational objects are used to write the exposure time into the Exif information of the image and save the Exif information to a file. If you need to write other types of Exif data, refer to the documentation for the Metadata-extractor library.

About the Author

Question Info

Publish Time
2023-02-28 00:22
Update Time
2023-02-28 00:22