Skip to content

Commit d42d1af

Browse files
committed
- Added logic to the MetadataParser to parse an array of rational numbers.
- Updated GPS coordinate parser. git-svn-id: svn://192.168.0.80/JavaXT/javaxt-core@205 2c7b0aa6-e0b2-3c4e-bb4a-8b65b6c465ff
1 parent f085a7e commit d42d1af

File tree

1 file changed

+96
-14
lines changed

1 file changed

+96
-14
lines changed

src/javaxt/io/Image.java

Lines changed: 96 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -283,18 +283,49 @@ private Graphics2D getGraphics(){
283283
//**************************************************************************
284284
//** addText
285285
//**************************************************************************
286-
/** Used to add text to the image at a given position. */
287-
286+
/** Used to add text to the image at a given position.
287+
* @param x Lower left coordinate of the text
288+
* @param y Lower left coordinate of the text
289+
*/
288290
public void addText(String text, int x, int y){
289291
addText(text, x, y, new Font ("SansSerif",Font.TRUETYPE_FONT,12), 0, 0, 0);
290292
}
291293

294+
295+
//**************************************************************************
296+
//** addText
297+
//**************************************************************************
298+
/** Used to add text to the image at a given position.
299+
* @param x Lower left coordinate of the text
300+
* @param y Lower left coordinate of the text
301+
* @param fontName Name of the font face (e.g. "Tahoma", "Helvetica", etc.)
302+
* @param fontSize Size of the font
303+
* @param r Value for the red channel (0-255)
304+
* @param g Value for the green channel (0-255)
305+
* @param b Value for the blue channel (0-255)
306+
*/
292307
public void addText(String text, int x, int y, String fontName, int fontSize, int r, int g, int b){
293308
addText(text, x, y, new Font(fontName, Font.TRUETYPE_FONT, fontSize), r,g,b);
294309
}
295310

311+
312+
//**************************************************************************
313+
//** addText
314+
//**************************************************************************
315+
/** Used to add text to the image at a given position.
316+
* @param x Lower left coordinate of the text
317+
* @param y Lower left coordinate of the text
318+
* @param font Font
319+
* @param r Value for the red channel (0-255)
320+
* @param g Value for the green channel (0-255)
321+
* @param b Value for the blue channel (0-255)
322+
*/
296323
public void addText(String text, int x, int y, Font font, int r, int g, int b){
297324
g2d = getGraphics();
325+
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
326+
RenderingHints.VALUE_ANTIALIAS_ON);
327+
328+
298329
g2d.setColor(new Color(r, g, b));
299330
g2d.setFont(font);
300331
g2d.drawString(text, x, y);
@@ -1489,8 +1520,8 @@ private void parseExif(){
14891520
public double[] getGPSCoordinate(){
14901521
getExifTags();
14911522
try{
1492-
Double lat = Double.parseDouble(gps.get(0x0002));
1493-
Double lon = Double.parseDouble(gps.get(0x0004));
1523+
Double lat = getCoordinate(gps.get(0x0002));
1524+
Double lon = getCoordinate(gps.get(0x0004));
14941525
String latRef = gps.get(0x0001); //N
14951526
String lonRef = gps.get(0x0003); //W
14961527

@@ -1504,6 +1535,37 @@ public double[] getGPSCoordinate(){
15041535
return null;
15051536
}
15061537

1538+
private double getCoordinate(String RationalArray) {
1539+
1540+
//num + "/" + den
1541+
String[] arr = RationalArray.substring(1, RationalArray.length()-1).split(",");
1542+
String[] deg = arr[0].trim().split("/");
1543+
String[] min = arr[1].trim().split("/");
1544+
String[] sec = arr[2].trim().split("/");
1545+
1546+
double degNumerator = Double.parseDouble(deg[0]);
1547+
double degDenominator = 1D; try{degDenominator = Double.parseDouble(deg[1]);} catch(Exception e){}
1548+
double minNumerator = Double.parseDouble(min[0]);
1549+
double minDenominator = 1D; try{minDenominator = Double.parseDouble(min[1]);} catch(Exception e){}
1550+
double secNumerator = Double.parseDouble(sec[0]);
1551+
double secDenominator = 1D; try{secDenominator = Double.parseDouble(sec[1]);} catch(Exception e){}
1552+
1553+
double m = 0;
1554+
if (degDenominator != 0 || degNumerator != 0){
1555+
m = (degNumerator / degDenominator);
1556+
}
1557+
1558+
if (minDenominator != 0 || minNumerator != 0){
1559+
m += (minNumerator / minDenominator) / 60D;
1560+
}
1561+
1562+
if (secDenominator != 0 || secNumerator != 0){
1563+
m += (secNumerator / secDenominator / 3600D);
1564+
}
1565+
1566+
return m;
1567+
}
1568+
15071569

15081570
//**************************************************************************
15091571
//** getGPSDatum
@@ -1595,17 +1657,16 @@ public IIOMetadataNode[] getMetadataByTagName(String tagName){
15951657
private class MetadataParser {
15961658

15971659
// Implementation notes:
1598-
// (1) Unknown/Undefined values are Base64 encoded.
1599-
// (2) The EXIF format includes unsigned integers which aren't directly
1600-
// available in Java. They probably have to be turned into longs but for
1601-
// the moment, they're all treated as signed. This shouldn't be a problem
1602-
// b/c we've never seen an EXIF unsigned value too large to represent in a
1603-
// Java int.
1660+
// (1) Unknown/Undefined values are all Base64 encoded.
1661+
// (2) Unsigned integers are treated as signed ints (should be longs). Note
1662+
// that its pretty rare to see a EXIF unsigned value too large to represent
1663+
// in a Java int.
16041664
// (3) Added logic to parse GPS Info using the GPS IFD offset value (tag 34853,
16051665
// hex 0x8825).
1606-
// (4) Improved performance in the parseExif() method by serializing only the
1666+
// (4) Added logic to parse an array of rational numbers (e.g. GPS metadata).
1667+
// (5) Improved performance in the parseExif() method by serializing only the
16071668
// first 8 characters into a string (vs the entire EXIF byte array).
1608-
// (5) Need to come up with a scheme to parse MakerNotes.
1669+
// (6) TODO: Need to come up with a clever scheme to parse MakerNotes.
16091670

16101671

16111672
private final int bytesPerFormat[] = { 0, 1, 1, 2, 4, 8, 1,
@@ -1641,6 +1702,8 @@ public MetadataParser(byte[] data, int marker) {
16411702
case 0xED: parseIptc(data);
16421703
case 0xE1: parseExif(data);
16431704
}
1705+
1706+
data = null;
16441707
}
16451708

16461709

@@ -1686,7 +1749,7 @@ private void parseIptc(byte[] iptcData) {
16861749
str = new String(data, offset, tagByteCount, "UTF-8");
16871750
offset += tagByteCount;
16881751
}
1689-
catch (Exception ex) {
1752+
catch (Exception e) {
16901753
}
16911754
}
16921755
tags.put(tagIdentifier, str);
@@ -1856,8 +1919,27 @@ private void processExifDir(int dirStart, int offsetBase, HashMap<Integer, Strin
18561919
break;
18571920
case FMT_URATIONAL:
18581921
case FMT_SRATIONAL:
1859-
assignRational(tag, tags, valueOffset);
1922+
1923+
if (components>1) {
1924+
1925+
//Create a string representing an array of rational numbers
1926+
StringBuffer rationals = new StringBuffer();
1927+
rationals.append("[");
1928+
for (int i = 0; i < components; i++){
1929+
assignRational(tag, tags, valueOffset + (8 * i));
1930+
rationals.append(tags.get(tag));
1931+
if (i < components-1) rationals.append(",");
1932+
}
1933+
rationals.append("]");
1934+
tags.put(tag, rationals.toString());
1935+
}
1936+
else{
1937+
assignRational(tag, tags, valueOffset);
1938+
}
18601939
break;
1940+
1941+
1942+
18611943
default:
18621944
//System.err.println("Unknown format " + format +
18631945
// " for " + tagName);

0 commit comments

Comments
 (0)