Skip to content

Commit

Permalink
Write Bearing information to image
Browse files Browse the repository at this point in the history
Writing bearing to the image
  • Loading branch information
james2432 committed Mar 16, 2017
1 parent fa4797b commit d21c3f1
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 2 deletions.
19 changes: 19 additions & 0 deletions OSVUploadr/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,26 @@
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-imaging</artifactId>
<version>1.0-SNAPSHOT</version>
<type>jar</type>
</dependency>
</dependencies>
<repositories>
<repository>
<id>apache.snapshots</id>
<name>Apache Development Snapshot Repository</name>
<url>https://repository.apache.org/content/repositories/snapshots/</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
Expand Down
17 changes: 15 additions & 2 deletions OSVUploadr/src/main/java/ca/osmcanada/osvuploadr/JPMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@
import java.nio.charset.Charset;
import java.util.Locale;
import java.util.ResourceBundle;
import org.apache.commons.imaging.ImageReadException;
import org.apache.commons.imaging.ImageWriteException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.CookieStore;
import org.apache.http.cookie.Cookie;
Expand Down Expand Up @@ -157,12 +159,18 @@ public int compare(File f1, File f2)
}});
System.out.println("End sorting");

Double last_bearing;
Double last_bearing=0.0;
ImageProperties imTO = null;
ImageProperties imFROM = null;
for(int i=file_list.length-1;i>=0;i--){
if(i==0){
//TODO: set last bearing
try{
Helper.setBearing(file_list[i], last_bearing);
}
catch(IOException|ImageReadException|ImageWriteException ex){
Logger.getLogger(JPMain.class.getName()).log(Level.SEVERE, "Error writing exif data", ex);
}
continue;
}
if(imTO==null){
Expand All @@ -176,7 +184,12 @@ public int compare(File f1, File f2)
}
last_bearing = (Helper.calc_bearing(imFROM.getLatitude(), imFROM.getLongitude(), imTO.getLatitude(), imTO.getLongitude())+ Offset) % 360.00;
System.out.println("Calculated bearing (with offset) at: " + last_bearing);

try{
Helper.setBearing(file_list[i], last_bearing);
}
catch(IOException|ImageReadException|ImageWriteException ex){
Logger.getLogger(JPMain.class.getName()).log(Level.SEVERE, "Error writing exif data", ex);
}
}
}

Expand Down
59 changes: 59 additions & 0 deletions OSVUploadr/src/main/java/ca/osmcanada/osvuploadr/Utils/Helper.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
import com.drew.metadata.exif.ExifSubIFDDirectory;
import com.drew.metadata.exif.GpsDirectory;
import java.awt.Desktop;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.net.URI;
import java.util.Date;
import java.util.logging.Level;
Expand All @@ -23,13 +25,30 @@
import java.util.List;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Locale;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.commons.imaging.formats.jpeg.exif.ExifRewriter;
import org.apache.commons.imaging.ImageReadException;
import org.apache.commons.imaging.ImageWriteException;
import org.apache.commons.imaging.Imaging;
import org.apache.commons.imaging.common.ImageMetadata;
import org.apache.commons.imaging.common.RationalNumber;
import org.apache.commons.imaging.formats.jpeg.JpegImageMetadata;
import org.apache.commons.imaging.formats.tiff.TiffImageMetadata;
import org.apache.commons.imaging.formats.tiff.constants.ExifTagConstants;
import org.apache.commons.imaging.formats.tiff.constants.GpsTagConstants;
import org.apache.commons.imaging.formats.tiff.write.TiffOutputDirectory;
import org.apache.commons.imaging.formats.tiff.write.TiffOutputSet;

/**
*
Expand Down Expand Up @@ -76,10 +95,49 @@ public static long getFileTime(File f){
return 0;
}

public static void setBearing(File f, Double bearing)
throws IOException, ImageReadException, ImageWriteException{

try (FileOutputStream fos = new FileOutputStream(f.getParent()+ File.separator+"SUPERTMPDUMP12324231.jpg",false);
OutputStream os = new BufferedOutputStream(fos);) {

TiffOutputSet outputSet = null;

final ImageMetadata metadata = Imaging.getMetadata(new File(f.getAbsolutePath()));
final JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata;
if (null != jpegMetadata) {
final TiffImageMetadata exif = jpegMetadata.getExif();

if (null != exif) {
outputSet = exif.getOutputSet();
}
}

if (null == outputSet) {
outputSet = new TiffOutputSet();
}

Rational r = new Rational(bearing);
final TiffOutputDirectory exifDirectory = outputSet.getOrCreateGPSDirectory();

exifDirectory.removeField(GpsTagConstants.GPS_TAG_GPS_IMG_DIRECTION );
exifDirectory.add(GpsTagConstants.GPS_TAG_GPS_IMG_DIRECTION, new RationalNumber(r.getNumerator(), r.getDenominator()));


new ExifRewriter().updateExifMetadataLossless(f, os,
outputSet);
//Replace file with new meta data
Files.move(Paths.get(f.getPath()+ File.separator+"SUPERTMPDUMP12324231.jpg"), Paths.get(f.getAbsolutePath()), StandardCopyOption.REPLACE_EXISTING);
}

}


public static ImageProperties getImageProperties(File f){
try{
Metadata metadata = ImageMetadataReader.readMetadata(f);
GpsDirectory directory = metadata.getFirstDirectoryOfType(GpsDirectory.class);


ImageProperties imp = new ImageProperties();
imp.setLatitude(directory.getGeoLocation().getLatitude());
Expand Down Expand Up @@ -304,3 +362,4 @@ public static Double calc_bearing(Double start_latitude, Double start_longitude,
return Math.toDegrees( Math.atan2(Math.sin(dLongitude)*Math.cos(end_latitude), Math.cos(start_latitude)*Math.sin(end_latitude) - Math.sin(start_latitude)*Math.cos(end_latitude)*Math.cos(dLongitude)));
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package ca.osmcanada.osvuploadr.Utils;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.MathContext;

/**
*
* @author james
*/
public class Rational {

private int num, denom;

static int[] reduceFraction(BigInteger num, BigInteger den) {
int gcd = num.gcd(den).intValue();
int[] rf = { num.divide(BigInteger.valueOf(gcd)).intValue(), den.divide(BigInteger.valueOf(gcd)).intValue() };
return rf;
}
public Rational(double d) {
String[] parts = String.valueOf(d).split("\\.");
BigDecimal den=null;
if(parts[1].length()>6){
den=BigDecimal.TEN.pow(6);
parts[1]=parts[1].substring(0, 6);
}
else{
den=BigDecimal.TEN.pow(parts[1].length());
}
BigDecimal num = (new BigDecimal(parts[0]).multiply(den)).add(new BigDecimal(parts[1])); // numerator
int[] fraction = reduceFraction(num.toBigInteger(), den.toBigInteger());
this.num = fraction[0]; this.denom = fraction[1];
}

public Rational(int num, int denom) {
this.num = num; this.denom = denom;
}

Rational() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

public int getNumerator(){
return num;
}

public int getDenominator(){
return denom;
}
public String toString() {
return String.valueOf(num) + "/" + String.valueOf(denom);
}
}

0 comments on commit d21c3f1

Please sign in to comment.