嗨,您可以像这样将 Exif 保存到 Imageavaliable 上的图像
此图片可用:
@Override
public void onImageAvailable(ImageReader reader) {
try {
if (latitude == null || longitude == null){
imageview.setVisibility(View.GONE);
/*deleteImage(file.getPath());*/
Toast.makeText(ShotActivity_camera2API.this,"Waiting.. try to get location result.",Toast.LENGTH_LONG).show();
//get location
myLocation.getLocation(ShotActivity_camera2API.this,locationResult);
return;
}else {
File dir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), "Image Project");
if (!dir.exists())
dir.mkdir();
file = new File(dir, currentDate + ".jpg");
Image image = reader.acquireNextImage();
ByteBuffer buffer = image.getPlanes()[0].getBuffer();
byte[] bytes = new byte[buffer.remaining()];
buffer.get(bytes);
try {
saveMetaData(file);
} catch (IOException e) {
e.printStackTrace();
}
saveImageFile(bytes);
/*mBackgroundHandler.post(new ImageSaver(reader.acquireLatestImage(),file));*/
//close image to fix crash second time capture
image.close();
}
} catch (Exception e) {
e.printStackTrace();
} /*catch (IOException e) {
e.printStackTrace();
}*/
}
这种保存Exiv的方法:
private void saveMetaData(File file) throws IOException {
ExifInterface exif = new ExifInterface(file.getCanonicalPath());
Log.e(TAG,""+file.getAbsolutePath());
//add Latitude to metadata
exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE, gpsParse.convert(latitude));
exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE_REF, gpsParse.latitudeRef(latitude));
exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE, gpsParse.convert(longitude));
exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF, gpsParse.longitudeRef(longitude));
exif.saveAttributes();
Log.i(TAG, "" + latitude + "," + longitude);
Log.i(TAG, "" + gpsParse.convert(latitude) + "," + gpsParse.longitudeRef(longitude));
Log.i(TAG, "" + gpsParse.latitudeRef(latitude) + "," + gpsParse.longitudeRef(longitude));
}
这是我的 GPS 将纬度经度解析为 Exif :
package com.example.PT107.task107_imagesqilte.Helper;
公共类 gpsParse {
私有静态 StringBuilder sb = new StringBuilder(20);
/**
* returns ref for latitude which is S or N.
* @param latitude
* @return S or N
*/
public static String latitudeRef(double latitude) {
return latitude<0.0d?"S":"N";
}
public static String longitudeRef(double longitude) {
return longitude<0.0d?"W":"E";
}
/**
* convert latitude into DMS (degree minute second) format. For instance<br/>
* -79.948862 becomes<br/>
* 79/1,56/1,55903/1000<br/>
* It works for latitude and longitude<br/>
* @param latitude could be longitude.
* @return
*/
synchronized public static final String convert(double latitude) {
latitude=Math.abs(latitude);
int degree = (int) latitude;
latitude *= 60;
latitude -= (degree * 60.0d);
int minute = (int) latitude;
latitude *= 60;
latitude -= (minute * 60.0d);
int second = (int) (latitude*1000.0d);
sb.setLength(0);
sb.append(degree);
sb.append("/1,");
sb.append(minute);
sb.append("/1,");
sb.append(second);
sb.append("/1000,");
return sb.toString();
}
}
我希望这对某人有帮助:)