在Java中将EXIF数据添加到文件中

时间:2018-04-21 15:51:56

标签: java android file exif

想要在保存时将GPS位置数据添加到图像,据我所知,这需要在outputStream关闭后完成。我编写了代码并实现了它,但没有写入GPS位置数据。据我所知,我已准备好EXIF数据所需的所有相关数据。

GPS转换代码

    public String getLon(Location location) {

    if (location == null) return "0/1,0/1,0/1000";
    String[] degMinSec = Location.convert(location.getLongitude(), Location.FORMAT_SECONDS).split(":");
    return degMinSec[0] + "/1," + degMinSec[1] + "/1," + degMinSec[2] + "/1000";

}
public String getLat(Location location) {

    if (location == null) return "0/1,0/1,0/1000";
    String[] degMinSec = Location.convert(location.getLatitude(), Location.FORMAT_SECONDS).split(":");
    return degMinSec[0] + "/1," + degMinSec[1] + "/1," + degMinSec[2] + "/1000";
}

保存图片的代码

            file = new File(Environment.getExternalStorageDirectory() + "/DCIM/GeoVista/" + UUID.randomUUID().toString() + ".jpg");
        ImageReader.OnImageAvailableListener readerListener = new ImageReader.OnImageAvailableListener() {
            @Override
            public void onImageAvailable(ImageReader imageReader) {
                Image image = null;
                try {
                    image = reader.acquireLatestImage();
                    ByteBuffer buffer = image.getPlanes()[0].getBuffer();
                    byte[] bytes = new byte[buffer.capacity()];
                    buffer.get(bytes);
                    save(bytes);
                    storeGeoCoordsToImage(bytes);
                    save(bytes);

                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    {
                        if (image != null)
                            image.close();
                    }
                }
            }


            private void save(byte[] bytes) throws IOException {
                OutputStream outputStream = null;

                try {
                    outputStream = new FileOutputStream(file);
                    outputStream.write(bytes);

                } finally {
                    if (outputStream != null)
                        outputStream.close();
                }
            }
        };

EXIF DATA CODE

                public boolean storeGeoCoordsToImage(File file, Location location) {

                // Avoid NullPointer
                if (file == null || location == null) return false;

                try {
                    ExifInterface exif = new ExifInterface(file.getAbsolutePath());
                    exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE, getLat(location));
                    exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE_REF, location.getLatitude() < 0 ? "S" : "N");
                    exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE, getLon(location));
                    exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF, location.getLongitude() < 0 ? "W" : "E");
                    exif.saveAttributes();
                } catch (IOException e) {
                    // do something
                    return false;
                }

                // Data was likely written. For sure no NullPointer. 
                return true;
            }

非常感谢任何输入。

0 个答案:

没有答案