Я пытаюсь преобразовать изображение с метаданными (широта, долгота, дата создания, производитель) в поток байтов. что я могу сделать. Но при построении обратного изображения из Bytestream метаданные (широта, долгота, дата создания, производитель) не присутствуют в новом изображении, которое создается bytestream. введите описание изображения здесь
enter code here
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.Base64;
import javax.imageio.ImageIO;
import org.apache.sanselan.ImageReadException;
import org.apache.sanselan.Sanselan;
import org.apache.sanselan.common.IImageMetadata;
import org.apache.sanselan.common.RationalNumber;
import org.apache.sanselan.formats.jpeg.JpegImageMetadata;
import org.apache.sanselan.formats.tiff.TiffField;
import org.apache.sanselan.formats.tiff.TiffImageMetadata;
import org.apache.sanselan.formats.tiff.constants.ExifTagConstants;
import org.apache.sanselan.formats.tiff.constants.GPSTagConstants;
import org.apache.sanselan.formats.tiff.constants.TagInfo;
import org.apache.sanselan.formats.tiff.constants.TiffTagConstants;
public class SimpleConvertImage {
public static String decoder(String bytestream){
System.out.println("Image Successfully created from Bytestream");
String dirName="C:\\Users\\username\\Downloads\\";
try
{
byte[] bytearray = Base64.getDecoder().decode(bytestream);
BufferedImage imag=ImageIO.read(new ByteArrayInputStream(bytearray));
ImageIO.write(imag, "jpg", new File(dirName,"img.jpg"));
}
catch(Exception e){
}
String path=dirName+"img.jpg";
return path;
}
public static void metadataExample(final File file) throws ImageReadException,IOException {
final IImageMetadata metadata = Sanselan.getMetadata(file);
if (metadata instanceof JpegImageMetadata) {
final JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata;
System.out.println("file: " + file.getPath());
System.out.println();
String manufacturer = jpegMetadata.findEXIFValueWithExactMatch(TiffTagConstants.TIFF_TAG_MAKE).getStringValue();
String model = jpegMetadata.findEXIFValueWithExactMatch(TiffTagConstants.TIFF_TAG_MODEL).getStringValue();
System.out.println("Manufacturer: "+ manufacturer+", Model: " + model);
System.out.println();
printTagValue(jpegMetadata,ExifTagConstants.EXIF_TAG_DATE_TIME_ORIGINAL);
printTagValue(jpegMetadata,GPSTagConstants.GPS_TAG_GPS_LATITUDE_REF);
printTagValue(jpegMetadata, GPSTagConstants.GPS_TAG_GPS_LATITUDE);
printTagValue(jpegMetadata,GPSTagConstants.GPS_TAG_GPS_LONGITUDE_REF);
printTagValue(jpegMetadata, GPSTagConstants.GPS_TAG_GPS_LONGITUDE);
System.out.println();
final TiffImageMetadata exifMetadata = jpegMetadata.getExif();
if(exifMetadata !=null) {
final TiffImageMetadata.GPSInfo gpsInfo = exifMetadata.getGPS();
if (gpsInfo!=null) {
final String gpsDescription = gpsInfo.toString();
final double longitude = gpsInfo.getLongitudeAsDegreesEast();
final double latitude = gpsInfo.getLatitudeAsDegreesNorth();
System.out.println("GPS Description: "+ gpsDescription);
System.out.println("GPS Longitude (Degrees East): " + longitude);
System.out.println("GPS Latitude (Degrees North): " + latitude);
}
}
final TiffField gpsLatitudeRefField = jpegMetadata.findEXIFValueWithExactMatch(GPSTagConstants.GPS_TAG_GPS_LATITUDE_REF);
final TiffField gpsLatitudeField = jpegMetadata.findEXIFValueWithExactMatch(GPSTagConstants.GPS_TAG_GPS_LATITUDE);
final TiffField gpsLongitudeRefField = jpegMetadata.findEXIFValueWithExactMatch(GPSTagConstants.GPS_TAG_GPS_LONGITUDE_REF);
final TiffField gpsLongitudeField = jpegMetadata.findEXIFValueWithExactMatch(GPSTagConstants.GPS_TAG_GPS_LONGITUDE);
if (gpsLatitudeRefField != null && gpsLatitudeField != null && gpsLongitudeRefField != null && gpsLongitudeField != null)
{
final String gpsLatitudeRef = (String) gpsLatitudeRefField.getValue();
final RationalNumber gpsLatitude[] = (RationalNumber[]) (gpsLatitudeField.getValue());
final String gpsLongitudeRef = (String) gpsLongitudeRefField.getValue();
final RationalNumber gpsLongitude[] = (RationalNumber[]) gpsLongitudeField.getValue();
final RationalNumber gpsLatitudeDegrees = gpsLatitude[0];
final RationalNumber gpsLatitudeMinutes = gpsLatitude[1];
final RationalNumber gpsLatitudeSeconds = gpsLatitude[2];
final RationalNumber gpsLongitudeDegrees = gpsLongitude[0];
final RationalNumber gpsLongitudeMinutes = gpsLongitude[1];
final RationalNumber gpsLongitudeSeconds = gpsLongitude[2];
System.out.println(" " + "GPS Latitude: "
+ gpsLatitudeDegrees.toDisplayString() + " degrees, "
+ gpsLatitudeMinutes.toDisplayString() + " minutes, "
+ gpsLatitudeSeconds.toDisplayString() + " seconds "
+ gpsLatitudeRef);
System.out.println(" " + "GPS Longitude: "
+ gpsLongitudeDegrees.toDisplayString() + " degrees, "
+ gpsLongitudeMinutes.toDisplayString() + " minutes, "
+ gpsLongitudeSeconds.toDisplayString() + " seconds "
+ gpsLongitudeRef);
}
System.out.println();
}
}
private static void printTagValue(final JpegImageMetadata jpegMetadata,final TagInfo tagInfo) {
final TiffField field = jpegMetadata.findEXIFValueWithExactMatch(tagInfo);
if (field == null)
System.out.println(tagInfo.name + ": " + "Not Found.");
else
System.out.println(tagInfo.name + ": "+ field.getValueDescription());
}
public static void main(String[] args) throws IOException{
try{
String bytestream="Enter STRING BYTESTREAM HERE";
String path=decoder(bytestream);
metadataExample(new File(path.replace("\\", "\\\\")));
}
catch(Exception e)
{
System.out.println("EXCEPTION");
}
}}