Android:JPEG EXIF信息添加

ExifInterface:https://developer.android.com/reference/android/media/ExifInterface
添加信息:

   ExifInterface exif = new ExifInterface(imagePath);
   // 写入经度信息
   exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE,gpsInfoConvert(lng));
   exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF,lng > 0 ? "E" : "W");
   exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE,gpsInfoConvert(lat));
   exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE_REF,lat > 0 ? "N" : "S");
   exif.setAttribute(ExifInterface.TAG_DATETIME,getNowTime("YYYY:MM:DD HH:mm:SS"));
   if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
       //exif.setAttribute(ExifInterface.TAG_USER_COMMENT,ToPinyin(address));
       exif.setAttribute(ExifInterface.TAG_DATETIME_ORIGINAL,getNowTime("YYYY:MM:DD HH:mm:SS"));
   }
   exif.saveAttributes();

说明:
1、此处的经纬度是度分秒格式,不同于地图api定位后拿到的经纬度。
2、DATETIME 格式固定的(官方没看到具体的格式)YYYY:MM:DD HH:mm:SS
3、TAG_DATETIME_ORIGINAL AndroidM之后才支持
4、Android官方TAG_USER_COMMENT 不支持UTF-8

private static final Charset ASCII = Charset.forName("US-ASCII");
public static ExifAttribute createString(String value) {
   final byte[] ascii = (value + '\0').getBytes(ASCII);
    return new ExifAttribute(IFD_FORMAT_STRING, ascii.length, ascii);
}
//就是说用ExifInterface 写进去的string都会被ASCII编码,反过来,读取的时候可以重写getAttribute,读取string时重新编码。
//可以读取汉字数据,写不进去(需要用第三方库写,如下)

怎么添加TAG_USER_COMMENT 中文?

JpegInfo inJpegInfo = new JpegInfo(file);
try {
    inJpegInfo.set(ExifTagConstants.EXIF_TAG_USER_COMMENT, address);
    rewriteInplace(inJpegInfo, file);
} catch (ImageWriteException e) {
    e.printStackTrace();
}  catch (ImageReadException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
//此处参考:https://www.jianshu.com/p/f3c01883cc7e

猜你喜欢

转载自blog.csdn.net/weixin_34077371/article/details/87507313