You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by se...@apache.org on 2010/09/10 03:35:52 UTC
svn commit: r995634 [5/6] - in
/commons/proper/sanselan/trunk/src/test/java/org/apache/sanselan: ./
common/ common/byteSources/ formats/bmp/ formats/gif/ formats/ico/
formats/jpeg/ formats/jpeg/exif/ formats/jpeg/iptc/ formats/jpeg/xmp/
formats/png/ fo...
Modified: commons/proper/sanselan/trunk/src/test/java/org/apache/sanselan/formats/tiff/TiffRoundtripTest.java
URL: http://svn.apache.org/viewvc/commons/proper/sanselan/trunk/src/test/java/org/apache/sanselan/formats/tiff/TiffRoundtripTest.java?rev=995634&r1=995633&r2=995634&view=diff
==============================================================================
--- commons/proper/sanselan/trunk/src/test/java/org/apache/sanselan/formats/tiff/TiffRoundtripTest.java (original)
+++ commons/proper/sanselan/trunk/src/test/java/org/apache/sanselan/formats/tiff/TiffRoundtripTest.java Fri Sep 10 01:35:50 2010
@@ -34,34 +34,34 @@ import org.apache.sanselan.util.Debug;
public class TiffRoundtripTest extends TiffBaseTest {
- public void test() throws IOException, ImageReadException,
- ImageWriteException {
- List images = getTiffImages();
- for (int i = 0; i < images.size(); i++) {
- if (i % 10 == 0)
- Debug.purgeMemory();
-
- File imageFile = (File) images.get(i);
- Debug.debug("imageFile", imageFile);
-
- IImageMetadata metadata = Sanselan.getMetadata(imageFile);
- assertNotNull(metadata);
-
- ImageInfo imageInfo = Sanselan.getImageInfo(imageFile);
- assertNotNull(imageInfo);
-
- BufferedImage image = Sanselan.getBufferedImage(imageFile);
- assertNotNull(image);
-
- File tempFile = createTempFile(imageFile.getName() + ".", ".tif");
- Map params = new HashMap();
- Sanselan.writeImage(image, tempFile, ImageFormat.IMAGE_FORMAT_TIFF,
- params);
- image = null;
-
- BufferedImage image2 = Sanselan.getBufferedImage(tempFile);
- assertNotNull(image2);
- }
- }
+ public void test() throws IOException, ImageReadException,
+ ImageWriteException {
+ List images = getTiffImages();
+ for (int i = 0; i < images.size(); i++) {
+ if (i % 10 == 0)
+ Debug.purgeMemory();
+
+ File imageFile = (File) images.get(i);
+ Debug.debug("imageFile", imageFile);
+
+ IImageMetadata metadata = Sanselan.getMetadata(imageFile);
+ assertNotNull(metadata);
+
+ ImageInfo imageInfo = Sanselan.getImageInfo(imageFile);
+ assertNotNull(imageInfo);
+
+ BufferedImage image = Sanselan.getBufferedImage(imageFile);
+ assertNotNull(image);
+
+ File tempFile = createTempFile(imageFile.getName() + ".", ".tif");
+ Map params = new HashMap();
+ Sanselan.writeImage(image, tempFile, ImageFormat.IMAGE_FORMAT_TIFF,
+ params);
+ image = null;
+
+ BufferedImage image2 = Sanselan.getBufferedImage(tempFile);
+ assertNotNull(image2);
+ }
+ }
}
Modified: commons/proper/sanselan/trunk/src/test/java/org/apache/sanselan/formats/xmp/XmpDumpTest.java
URL: http://svn.apache.org/viewvc/commons/proper/sanselan/trunk/src/test/java/org/apache/sanselan/formats/xmp/XmpDumpTest.java?rev=995634&r1=995633&r2=995634&view=diff
==============================================================================
--- commons/proper/sanselan/trunk/src/test/java/org/apache/sanselan/formats/xmp/XmpDumpTest.java (original)
+++ commons/proper/sanselan/trunk/src/test/java/org/apache/sanselan/formats/xmp/XmpDumpTest.java Fri Sep 10 01:35:50 2010
@@ -29,32 +29,32 @@ import org.apache.sanselan.util.Debug;
public class XmpDumpTest extends SanselanTest
{
- public void test() throws IOException, ImageReadException
- {
- List images = getTestImages();
- for (int i = 0; i < images.size(); i++)
- {
- if (i % 10 == 0)
- Debug.purgeMemory();
-
- File imageFile = (File) images.get(i);
-
- if (imageFile.getName().toLowerCase().endsWith(".png")
- && isInvalidPNGTestFile(imageFile))
- continue;
-
- Debug.debug("imageFile", imageFile);
- Debug.debug();
-
- String xmpXml = Sanselan.getXmpXml(imageFile);
- if (null == xmpXml)
- continue;
-
- assertNotNull(xmpXml);
-
- Debug.debug("xmpXml", xmpXml);
- Debug.debug();
- }
- }
+ public void test() throws IOException, ImageReadException
+ {
+ List images = getTestImages();
+ for (int i = 0; i < images.size(); i++)
+ {
+ if (i % 10 == 0)
+ Debug.purgeMemory();
+
+ File imageFile = (File) images.get(i);
+
+ if (imageFile.getName().toLowerCase().endsWith(".png")
+ && isInvalidPNGTestFile(imageFile))
+ continue;
+
+ Debug.debug("imageFile", imageFile);
+ Debug.debug();
+
+ String xmpXml = Sanselan.getXmpXml(imageFile);
+ if (null == xmpXml)
+ continue;
+
+ assertNotNull(xmpXml);
+
+ Debug.debug("xmpXml", xmpXml);
+ Debug.debug();
+ }
+ }
}
Modified: commons/proper/sanselan/trunk/src/test/java/org/apache/sanselan/formats/xmp/XmpUpdateTest.java
URL: http://svn.apache.org/viewvc/commons/proper/sanselan/trunk/src/test/java/org/apache/sanselan/formats/xmp/XmpUpdateTest.java?rev=995634&r1=995633&r2=995634&view=diff
==============================================================================
--- commons/proper/sanselan/trunk/src/test/java/org/apache/sanselan/formats/xmp/XmpUpdateTest.java (original)
+++ commons/proper/sanselan/trunk/src/test/java/org/apache/sanselan/formats/xmp/XmpUpdateTest.java Fri Sep 10 01:35:50 2010
@@ -34,63 +34,63 @@ import org.apache.sanselan.util.Debug;
public class XmpUpdateTest extends SanselanTest
{
- public void test() throws IOException, ImageReadException,
- ImageWriteException
- {
- List images = getTestImages();
- for (int i = 0; i < images.size(); i++)
- {
- if (i % 10 == 0)
- Debug.purgeMemory();
-
- File imageFile = (File) images.get(i);
-
- if (imageFile.getName().toLowerCase().endsWith(".png")
- && isInvalidPNGTestFile(imageFile))
- continue;
-
- Debug.debug("imageFile", imageFile);
- Debug.debug();
-
- ImageFormat imageFormat = Sanselan.guessFormat(imageFile);
-
- String xmpXml = Sanselan.getXmpXml(imageFile);
- if (null == xmpXml
- && imageFormat.equals(ImageFormat.IMAGE_FORMAT_GIF))
- xmpXml = "temporary test until I can locate a GIF with XMP in the wild.";
- if (null == xmpXml)
- continue;
-
- assertNotNull(xmpXml);
-
- if (imageFormat.equals(ImageFormat.IMAGE_FORMAT_PNG))
- ;
- else if (imageFormat.equals(ImageFormat.IMAGE_FORMAT_TIFF))
- ;
- else if (imageFormat.equals(ImageFormat.IMAGE_FORMAT_GIF))
- ;
- else
- continue;
-
- File tempFile = this.createTempFile(imageFile.getName() + ".", "."
- + imageFormat.extension);
- BufferedImage image = Sanselan.getBufferedImage(imageFile);
-
- // ----
-
- Map params = new HashMap();
- params.put(PARAM_KEY_XMP_XML, xmpXml);
- Sanselan.writeImage(image, tempFile, imageFormat, params);
-
- String xmpXmlOut = Sanselan.getXmpXml(tempFile);
-
- assertNotNull(xmpXmlOut);
-
- assertEquals(xmpXmlOut, xmpXml);
-
-// Debug.debug("xmpXmlOut", xmpXmlOut.length());
- // Debug.debug("xmpXml", xmpXml);
- // Debug.debug();
- }
- }
+ public void test() throws IOException, ImageReadException,
+ ImageWriteException
+ {
+ List images = getTestImages();
+ for (int i = 0; i < images.size(); i++)
+ {
+ if (i % 10 == 0)
+ Debug.purgeMemory();
+
+ File imageFile = (File) images.get(i);
+
+ if (imageFile.getName().toLowerCase().endsWith(".png")
+ && isInvalidPNGTestFile(imageFile))
+ continue;
+
+ Debug.debug("imageFile", imageFile);
+ Debug.debug();
+
+ ImageFormat imageFormat = Sanselan.guessFormat(imageFile);
+
+ String xmpXml = Sanselan.getXmpXml(imageFile);
+ if (null == xmpXml
+ && imageFormat.equals(ImageFormat.IMAGE_FORMAT_GIF))
+ xmpXml = "temporary test until I can locate a GIF with XMP in the wild.";
+ if (null == xmpXml)
+ continue;
+
+ assertNotNull(xmpXml);
+
+ if (imageFormat.equals(ImageFormat.IMAGE_FORMAT_PNG))
+ ;
+ else if (imageFormat.equals(ImageFormat.IMAGE_FORMAT_TIFF))
+ ;
+ else if (imageFormat.equals(ImageFormat.IMAGE_FORMAT_GIF))
+ ;
+ else
+ continue;
+
+ File tempFile = this.createTempFile(imageFile.getName() + ".", "."
+ + imageFormat.extension);
+ BufferedImage image = Sanselan.getBufferedImage(imageFile);
+
+ // ----
+
+ Map params = new HashMap();
+ params.put(PARAM_KEY_XMP_XML, xmpXml);
+ Sanselan.writeImage(image, tempFile, imageFormat, params);
+
+ String xmpXmlOut = Sanselan.getXmpXml(tempFile);
+
+ assertNotNull(xmpXmlOut);
+
+ assertEquals(xmpXmlOut, xmpXml);
+
+// Debug.debug("xmpXmlOut", xmpXmlOut.length());
+ // Debug.debug("xmpXml", xmpXml);
+ // Debug.debug();
+ }
+ }
}
Modified: commons/proper/sanselan/trunk/src/test/java/org/apache/sanselan/roundtrip/RoundtripTest.java
URL: http://svn.apache.org/viewvc/commons/proper/sanselan/trunk/src/test/java/org/apache/sanselan/roundtrip/RoundtripTest.java?rev=995634&r1=995633&r2=995634&view=diff
==============================================================================
--- commons/proper/sanselan/trunk/src/test/java/org/apache/sanselan/roundtrip/RoundtripTest.java (original)
+++ commons/proper/sanselan/trunk/src/test/java/org/apache/sanselan/roundtrip/RoundtripTest.java Fri Sep 10 01:35:50 2010
@@ -35,410 +35,410 @@ import org.apache.sanselan.util.IOUtils;
public class RoundtripTest extends SanselanTest
{
- private static final int COLOR_FULL_RGB = 0;
- private static final int COLOR_LIMITED_INDEX = 1;
- private static final int COLOR_GRAYSCALE = 2;
- private static final int COLOR_BITMAP = 3;
-
- private static class FormatInfo
- {
-
- public final ImageFormat format;
- public final boolean canRead;
- public final boolean canWrite;
- public final int colorSupport;
- public final boolean identicalSecondWrite;
-
- public FormatInfo(ImageFormat format, boolean canRead,
- boolean canWrite, int colorSupport,
- final boolean identicalSecondWrite)
- {
- this.canRead = canRead;
- this.canWrite = canWrite;
- this.colorSupport = colorSupport;
- this.format = format;
- this.identicalSecondWrite = identicalSecondWrite;
- }
- }
-
- private static final FormatInfo FORMAT_INFOS[] = { //
- new FormatInfo(ImageFormat.IMAGE_FORMAT_PNG, true, true,
- COLOR_FULL_RGB, true), //
- new FormatInfo(ImageFormat.IMAGE_FORMAT_GIF, true, true,
- COLOR_LIMITED_INDEX, true), //
- new FormatInfo(ImageFormat.IMAGE_FORMAT_ICO, true, true,
- COLOR_FULL_RGB, true), //
- new FormatInfo(ImageFormat.IMAGE_FORMAT_TIFF, true, true,
- COLOR_FULL_RGB, true), //
- new FormatInfo(ImageFormat.IMAGE_FORMAT_JPEG, false, false,
- COLOR_FULL_RGB, true), //
- new FormatInfo(ImageFormat.IMAGE_FORMAT_BMP, true, true,
- COLOR_FULL_RGB, true), //
- new FormatInfo(ImageFormat.IMAGE_FORMAT_PSD, true, false,
- COLOR_FULL_RGB, true), //
- new FormatInfo(ImageFormat.IMAGE_FORMAT_PBM, true, true,
- COLOR_BITMAP, true), //
- new FormatInfo(ImageFormat.IMAGE_FORMAT_PGM, true, true,
- COLOR_GRAYSCALE, true), //
- new FormatInfo(ImageFormat.IMAGE_FORMAT_PPM, true, true,
- COLOR_FULL_RGB, true), //
- // new FormatInfo(ImageFormat.IMAGE_FORMAT_PNM, true, true,
- // COLOR_FULL_RGB, true), //
- new FormatInfo(ImageFormat.IMAGE_FORMAT_TGA, false, false,
- COLOR_FULL_RGB, true), //
- };
-
- private BufferedImage createArgbBitmapImage(int width, int height)
- {
- BufferedImage result = new BufferedImage(width, height,
- BufferedImage.TYPE_INT_ARGB);
- for (int x = 0; x < width; x++)
- for (int y = 0; y < height; y++)
- {
- // alternating black and white.
- int modulator = y + 2; // make sure lines vary.
- int argb = (x + y) % modulator == 0 ? 0xff000000 : 0xffffffff;
- result.setRGB(x, y, argb);
- }
- return result;
- }
-
- private BufferedImage createBitmapBitmapImage(int width, int height)
- {
- BufferedImage result = new BufferedImage(width, height,
- BufferedImage.TYPE_BYTE_BINARY);
- for (int x = 0; x < width; x++)
- for (int y = 0; y < height; y++)
- {
- // alternating black and white.
- int modulator = y + 2; // make sure lines vary.
- int argb = (x + y) % modulator == 0 ? 0xff000000 : 0xffffffff;
- result.setRGB(x, y, argb);
- }
- return result;
- }
-
- private BufferedImage createArgbGrayscaleImage(int width, int height)
- {
- BufferedImage result = new BufferedImage(width, height,
- BufferedImage.TYPE_INT_ARGB);
- for (int x = 0; x < width; x++)
- for (int y = 0; y < height; y++)
- {
- int value = (256 * (x + y)) / (width + height);
- int argb = (0xff << 24) | (value << 16) | (value << 8)
- | (value << 0);
-
- result.setRGB(x, y, argb);
- }
- return result;
- }
-
- private BufferedImage createGrayscaleGrayscaleImage(int width, int height)
- {
- BufferedImage result = new BufferedImage(width, height,
- BufferedImage.TYPE_BYTE_GRAY);
- for (int x = 0; x < width; x++)
- for (int y = 0; y < height; y++)
- {
- int value = (256 * (x + y)) / (width + height);
- int argb = (0xff << 24) | (value << 16) | (value << 8)
- | (value << 0);
-
- result.setRGB(x, y, argb);
- }
- return result;
- }
-
- private BufferedImage createLimitedColorImage(int width, int height)
- {
- int colors[] = { 0xffffffff, 0xff000000, 0xfff00000, 0xff0000ff,
- 0xff123456, 0xfffefeff, 0xff7f817f, };
-
- BufferedImage result = new BufferedImage(width, height,
- BufferedImage.TYPE_INT_ARGB);
- for (int x = 0; x < width; x++)
- for (int y = 0; y < height; y++)
- {
- int argb = colors[(x + y) % colors.length];
- result.setRGB(x, y, argb);
- }
- return result;
- }
-
- private BufferedImage createFullColorImage(int width, int height)
- {
- BufferedImage result = new BufferedImage(width, height,
- BufferedImage.TYPE_INT_ARGB);
- for (int x = 0; x < width; x++)
- for (int y = 0; y < height; y++)
- {
- int red = (x * 255) / width;
- int green = (y * 255) / height;
- int blue = ((x + y) * 255) / (width + height);
- int argb = (0xff << 24) | (red << 16) | (green << 8)
- | (blue << 0);
- result.setRGB(x, y, argb);
- }
- return result;
- }
-
- private void compareImagesExact(BufferedImage a, BufferedImage b)
- {
- compareImages(a, b, 0);
- }
-
- // private void compareImagesOffByOne(BufferedImage a, BufferedImage b)
- // {
- // compareImages(a, b, 3); // one bit of rounding error for each channel
- // }
-
- private void compareImages(BufferedImage a, BufferedImage b, int tolerance)
- {
- assertEquals(a.getWidth(), b.getWidth());
- assertEquals(a.getHeight(), b.getHeight());
-
- for (int x = 0; x < a.getWidth(); x++)
- for (int y = 0; y < a.getHeight(); y++)
- {
- int a_argb = a.getRGB(x, y);
- int b_argb = b.getRGB(x, y);
- if (a_argb != b_argb)
- {
- if (calculateARGBDistance(a_argb, b_argb) <= tolerance)
- continue; // ignore.
- }
- if (a_argb != b_argb)
- {
- Debug.debug("width", a.getWidth());
- Debug.debug("height", a.getHeight());
- Debug.debug("distance", calculateARGBDistance(a_argb,
- b_argb));
- Debug.debug("x", x);
- Debug.debug("y", y);
- Debug.debug("a_argb", a_argb + " (0x"
- + Integer.toHexString(a_argb) + ")");
- Debug.debug("b_argb", b_argb + " (0x"
- + Integer.toHexString(b_argb) + ")");
- }
- assertEquals(a_argb, b_argb);
- }
- }
-
- private int calculateARGBDistance(int a, int b)
- {
- int aAlpha = 0xff & (a >> 24);
- int aRed = 0xff & (a >> 16);
- int aGreen = 0xff & (a >> 8);
- int aBlue = 0xff & (a >> 0);
- int bAlpha = 0xff & (b >> 24);
- int bRed = 0xff & (b >> 16);
- int bGreen = 0xff & (b >> 8);
- int bBlue = 0xff & (b >> 0);
- int diff = Math.abs(aAlpha - bAlpha) + Math.abs(aRed - bRed)
- + Math.abs(aGreen - bGreen) + Math.abs(aBlue - bBlue);
- return diff;
-
- }
-
- private void compareFilesExact(File a, File b) throws IOException
- {
- assertTrue(a.exists() && a.isFile());
- assertTrue(b.exists() && b.isFile());
- assertEquals(a.length(), b.length());
-
- byte aData[] = IOUtils.getFileBytes(a);
- byte bData[] = IOUtils.getFileBytes(b);
-
- for (int i = 0; i < a.length(); i++)
- {
- int aByte = 0xff & aData[i];
- int bByte = 0xff & bData[i];
-
- if (aByte != bByte)
- {
- Debug.debug("a", a);
- Debug.debug("b", b);
- Debug.debug("i", i);
- Debug.debug("aByte", aByte + " (0x"
- + Integer.toHexString(aByte) + ")");
- Debug.debug("bByte", bByte + " (0x"
- + Integer.toHexString(bByte) + ")");
- }
- assertEquals(aByte, bByte);
- }
- }
-
- public void testBitmapRoundtrip() throws IOException, ImageReadException,
- ImageWriteException
- {
- BufferedImage testImages[] = { //
-
- createArgbBitmapImage(1, 1), // minimal
- createArgbBitmapImage(2, 2), //
- createArgbBitmapImage(10, 10), // larger than 8
- createArgbBitmapImage(300, 300), // larger than 256
-
- createBitmapBitmapImage(1, 1), // minimal
- createBitmapBitmapImage(2, 2), //
- createBitmapBitmapImage(10, 10), // larger than 8
- createBitmapBitmapImage(300, 300), // larger than 256
- };
-
- for (int j = 0; j < testImages.length; j++)
- {
- BufferedImage testImage = testImages[j];
-
- for (int i = 0; i < FORMAT_INFOS.length; i++)
- {
- FormatInfo formatInfo = FORMAT_INFOS[i];
- if ((!formatInfo.canRead) || (!formatInfo.canWrite))
- continue;
-
- Debug.debug("bitmap test: " + formatInfo.format.name);
-
- roundtrip(formatInfo, testImage, "bitmap", true);
- }
- }
- }
-
- public void testGrayscaleRoundtrip() throws IOException,
- ImageReadException, ImageWriteException
- {
- BufferedImage testImages[] = { //
-
- createArgbBitmapImage(1, 1), // minimal
- createArgbGrayscaleImage(2, 2), //
- createArgbGrayscaleImage(10, 10), // larger than 8
- createArgbGrayscaleImage(300, 300), // larger than 256
-
- createGrayscaleGrayscaleImage(1, 1), // minimal
- createGrayscaleGrayscaleImage(2, 2), //
- createGrayscaleGrayscaleImage(10, 10), // larger than 8
- createGrayscaleGrayscaleImage(300, 300), // larger than 256
- };
-
- for (int j = 0; j < testImages.length; j++)
- {
- BufferedImage testImage = testImages[j];
-
- for (int i = 0; i < FORMAT_INFOS.length; i++)
- {
- FormatInfo formatInfo = FORMAT_INFOS[i];
- if ((!formatInfo.canRead) || (!formatInfo.canWrite))
- continue;
-
- Debug.debug("grayscale test: " + formatInfo.format.name);
-
- boolean imageExact = true;
- if (formatInfo.colorSupport == COLOR_BITMAP)
- imageExact = false;
-
- roundtrip(formatInfo, testImage, "gray", imageExact);
- }
- }
- }
-
- public void testLimitedColorRoundtrip() throws IOException,
- ImageReadException, ImageWriteException
- {
- BufferedImage testImages[] = { //
-
- createLimitedColorImage(1, 1), // minimal
- createLimitedColorImage(2, 2), //
- createLimitedColorImage(10, 10), // larger than 8
- createLimitedColorImage(300, 300), // larger than 256
- };
-
- for (int j = 0; j < testImages.length; j++)
- {
- BufferedImage testImage = testImages[j];
-
- for (int i = 0; i < FORMAT_INFOS.length; i++)
- {
- FormatInfo formatInfo = FORMAT_INFOS[i];
- if ((!formatInfo.canRead) || (!formatInfo.canWrite))
- continue;
-
- Debug.debug("indexable test: " + formatInfo.format.name);
-
- boolean imageExact = true;
- if (formatInfo.colorSupport == COLOR_BITMAP)
- imageExact = false;
- if (formatInfo.colorSupport == COLOR_GRAYSCALE)
- imageExact = false;
-
- roundtrip(formatInfo, testImage, "indexable", imageExact);
- }
- }
- }
-
- public void testFullColorRoundtrip() throws IOException,
- ImageReadException, ImageWriteException
- {
- BufferedImage testImages[] = { //
-
- createFullColorImage(1, 1), // minimal
- createFullColorImage(2, 2), //
- createFullColorImage(10, 10), // larger than 8
- createFullColorImage(300, 300), // larger than 256
- };
-
- for (int j = 0; j < testImages.length; j++)
- {
- BufferedImage testImage = testImages[j];
-
- for (int i = 0; i < FORMAT_INFOS.length; i++)
- {
- FormatInfo formatInfo = FORMAT_INFOS[i];
- if ((!formatInfo.canRead) || (!formatInfo.canWrite))
- continue;
-
- Debug.debug("fullColor test: " + formatInfo.format.name);
-
- boolean imageExact = true;
- if (formatInfo.colorSupport == COLOR_BITMAP)
- imageExact = false;
- if (formatInfo.colorSupport == COLOR_GRAYSCALE)
- imageExact = false;
- if (formatInfo.colorSupport == COLOR_LIMITED_INDEX)
- imageExact = false;
-
- roundtrip(formatInfo, testImage, "fullColor", imageExact);
- }
- }
- }
-
- private void roundtrip(FormatInfo formatInfo, BufferedImage testImage,
- String tempPrefix, boolean imageExact) throws IOException,
- ImageReadException, ImageWriteException
- {
- File temp1 = createTempFile(tempPrefix + ".", "."
- + formatInfo.format.extension);
- // Debug.debug("tempFile: " + tempFile.getName());
-
- Map params = new HashMap();
- Sanselan.writeImage(testImage, temp1, formatInfo.format, params);
-
- Map readParams = new HashMap();
- readParams.put(SanselanConstants.BUFFERED_IMAGE_FACTORY,
- new RgbBufferedImageFactory());
- BufferedImage image2 = Sanselan.getBufferedImage(temp1, readParams);
- assertNotNull(image2);
-
- if (imageExact)
- {
- // note tolerance when comparing grayscale images
- // BufferedImages of
- compareImagesExact(testImage, image2);
- }
-
- File temp2 = createTempFile(tempPrefix + ".", "."
- + formatInfo.format.extension);
- // Debug.debug("tempFile: " + tempFile.getName());
- Sanselan.writeImage(image2, temp2, formatInfo.format, params);
+ private static final int COLOR_FULL_RGB = 0;
+ private static final int COLOR_LIMITED_INDEX = 1;
+ private static final int COLOR_GRAYSCALE = 2;
+ private static final int COLOR_BITMAP = 3;
+
+ private static class FormatInfo
+ {
+
+ public final ImageFormat format;
+ public final boolean canRead;
+ public final boolean canWrite;
+ public final int colorSupport;
+ public final boolean identicalSecondWrite;
+
+ public FormatInfo(ImageFormat format, boolean canRead,
+ boolean canWrite, int colorSupport,
+ final boolean identicalSecondWrite)
+ {
+ this.canRead = canRead;
+ this.canWrite = canWrite;
+ this.colorSupport = colorSupport;
+ this.format = format;
+ this.identicalSecondWrite = identicalSecondWrite;
+ }
+ }
+
+ private static final FormatInfo FORMAT_INFOS[] = { //
+ new FormatInfo(ImageFormat.IMAGE_FORMAT_PNG, true, true,
+ COLOR_FULL_RGB, true), //
+ new FormatInfo(ImageFormat.IMAGE_FORMAT_GIF, true, true,
+ COLOR_LIMITED_INDEX, true), //
+ new FormatInfo(ImageFormat.IMAGE_FORMAT_ICO, true, true,
+ COLOR_FULL_RGB, true), //
+ new FormatInfo(ImageFormat.IMAGE_FORMAT_TIFF, true, true,
+ COLOR_FULL_RGB, true), //
+ new FormatInfo(ImageFormat.IMAGE_FORMAT_JPEG, false, false,
+ COLOR_FULL_RGB, true), //
+ new FormatInfo(ImageFormat.IMAGE_FORMAT_BMP, true, true,
+ COLOR_FULL_RGB, true), //
+ new FormatInfo(ImageFormat.IMAGE_FORMAT_PSD, true, false,
+ COLOR_FULL_RGB, true), //
+ new FormatInfo(ImageFormat.IMAGE_FORMAT_PBM, true, true,
+ COLOR_BITMAP, true), //
+ new FormatInfo(ImageFormat.IMAGE_FORMAT_PGM, true, true,
+ COLOR_GRAYSCALE, true), //
+ new FormatInfo(ImageFormat.IMAGE_FORMAT_PPM, true, true,
+ COLOR_FULL_RGB, true), //
+ // new FormatInfo(ImageFormat.IMAGE_FORMAT_PNM, true, true,
+ // COLOR_FULL_RGB, true), //
+ new FormatInfo(ImageFormat.IMAGE_FORMAT_TGA, false, false,
+ COLOR_FULL_RGB, true), //
+ };
+
+ private BufferedImage createArgbBitmapImage(int width, int height)
+ {
+ BufferedImage result = new BufferedImage(width, height,
+ BufferedImage.TYPE_INT_ARGB);
+ for (int x = 0; x < width; x++)
+ for (int y = 0; y < height; y++)
+ {
+ // alternating black and white.
+ int modulator = y + 2; // make sure lines vary.
+ int argb = (x + y) % modulator == 0 ? 0xff000000 : 0xffffffff;
+ result.setRGB(x, y, argb);
+ }
+ return result;
+ }
+
+ private BufferedImage createBitmapBitmapImage(int width, int height)
+ {
+ BufferedImage result = new BufferedImage(width, height,
+ BufferedImage.TYPE_BYTE_BINARY);
+ for (int x = 0; x < width; x++)
+ for (int y = 0; y < height; y++)
+ {
+ // alternating black and white.
+ int modulator = y + 2; // make sure lines vary.
+ int argb = (x + y) % modulator == 0 ? 0xff000000 : 0xffffffff;
+ result.setRGB(x, y, argb);
+ }
+ return result;
+ }
+
+ private BufferedImage createArgbGrayscaleImage(int width, int height)
+ {
+ BufferedImage result = new BufferedImage(width, height,
+ BufferedImage.TYPE_INT_ARGB);
+ for (int x = 0; x < width; x++)
+ for (int y = 0; y < height; y++)
+ {
+ int value = (256 * (x + y)) / (width + height);
+ int argb = (0xff << 24) | (value << 16) | (value << 8)
+ | (value << 0);
+
+ result.setRGB(x, y, argb);
+ }
+ return result;
+ }
+
+ private BufferedImage createGrayscaleGrayscaleImage(int width, int height)
+ {
+ BufferedImage result = new BufferedImage(width, height,
+ BufferedImage.TYPE_BYTE_GRAY);
+ for (int x = 0; x < width; x++)
+ for (int y = 0; y < height; y++)
+ {
+ int value = (256 * (x + y)) / (width + height);
+ int argb = (0xff << 24) | (value << 16) | (value << 8)
+ | (value << 0);
+
+ result.setRGB(x, y, argb);
+ }
+ return result;
+ }
+
+ private BufferedImage createLimitedColorImage(int width, int height)
+ {
+ int colors[] = { 0xffffffff, 0xff000000, 0xfff00000, 0xff0000ff,
+ 0xff123456, 0xfffefeff, 0xff7f817f, };
+
+ BufferedImage result = new BufferedImage(width, height,
+ BufferedImage.TYPE_INT_ARGB);
+ for (int x = 0; x < width; x++)
+ for (int y = 0; y < height; y++)
+ {
+ int argb = colors[(x + y) % colors.length];
+ result.setRGB(x, y, argb);
+ }
+ return result;
+ }
+
+ private BufferedImage createFullColorImage(int width, int height)
+ {
+ BufferedImage result = new BufferedImage(width, height,
+ BufferedImage.TYPE_INT_ARGB);
+ for (int x = 0; x < width; x++)
+ for (int y = 0; y < height; y++)
+ {
+ int red = (x * 255) / width;
+ int green = (y * 255) / height;
+ int blue = ((x + y) * 255) / (width + height);
+ int argb = (0xff << 24) | (red << 16) | (green << 8)
+ | (blue << 0);
+ result.setRGB(x, y, argb);
+ }
+ return result;
+ }
+
+ private void compareImagesExact(BufferedImage a, BufferedImage b)
+ {
+ compareImages(a, b, 0);
+ }
+
+ // private void compareImagesOffByOne(BufferedImage a, BufferedImage b)
+ // {
+ // compareImages(a, b, 3); // one bit of rounding error for each channel
+ // }
+
+ private void compareImages(BufferedImage a, BufferedImage b, int tolerance)
+ {
+ assertEquals(a.getWidth(), b.getWidth());
+ assertEquals(a.getHeight(), b.getHeight());
+
+ for (int x = 0; x < a.getWidth(); x++)
+ for (int y = 0; y < a.getHeight(); y++)
+ {
+ int a_argb = a.getRGB(x, y);
+ int b_argb = b.getRGB(x, y);
+ if (a_argb != b_argb)
+ {
+ if (calculateARGBDistance(a_argb, b_argb) <= tolerance)
+ continue; // ignore.
+ }
+ if (a_argb != b_argb)
+ {
+ Debug.debug("width", a.getWidth());
+ Debug.debug("height", a.getHeight());
+ Debug.debug("distance", calculateARGBDistance(a_argb,
+ b_argb));
+ Debug.debug("x", x);
+ Debug.debug("y", y);
+ Debug.debug("a_argb", a_argb + " (0x"
+ + Integer.toHexString(a_argb) + ")");
+ Debug.debug("b_argb", b_argb + " (0x"
+ + Integer.toHexString(b_argb) + ")");
+ }
+ assertEquals(a_argb, b_argb);
+ }
+ }
+
+ private int calculateARGBDistance(int a, int b)
+ {
+ int aAlpha = 0xff & (a >> 24);
+ int aRed = 0xff & (a >> 16);
+ int aGreen = 0xff & (a >> 8);
+ int aBlue = 0xff & (a >> 0);
+ int bAlpha = 0xff & (b >> 24);
+ int bRed = 0xff & (b >> 16);
+ int bGreen = 0xff & (b >> 8);
+ int bBlue = 0xff & (b >> 0);
+ int diff = Math.abs(aAlpha - bAlpha) + Math.abs(aRed - bRed)
+ + Math.abs(aGreen - bGreen) + Math.abs(aBlue - bBlue);
+ return diff;
+
+ }
+
+ private void compareFilesExact(File a, File b) throws IOException
+ {
+ assertTrue(a.exists() && a.isFile());
+ assertTrue(b.exists() && b.isFile());
+ assertEquals(a.length(), b.length());
+
+ byte aData[] = IOUtils.getFileBytes(a);
+ byte bData[] = IOUtils.getFileBytes(b);
+
+ for (int i = 0; i < a.length(); i++)
+ {
+ int aByte = 0xff & aData[i];
+ int bByte = 0xff & bData[i];
+
+ if (aByte != bByte)
+ {
+ Debug.debug("a", a);
+ Debug.debug("b", b);
+ Debug.debug("i", i);
+ Debug.debug("aByte", aByte + " (0x"
+ + Integer.toHexString(aByte) + ")");
+ Debug.debug("bByte", bByte + " (0x"
+ + Integer.toHexString(bByte) + ")");
+ }
+ assertEquals(aByte, bByte);
+ }
+ }
+
+ public void testBitmapRoundtrip() throws IOException, ImageReadException,
+ ImageWriteException
+ {
+ BufferedImage testImages[] = { //
+
+ createArgbBitmapImage(1, 1), // minimal
+ createArgbBitmapImage(2, 2), //
+ createArgbBitmapImage(10, 10), // larger than 8
+ createArgbBitmapImage(300, 300), // larger than 256
+
+ createBitmapBitmapImage(1, 1), // minimal
+ createBitmapBitmapImage(2, 2), //
+ createBitmapBitmapImage(10, 10), // larger than 8
+ createBitmapBitmapImage(300, 300), // larger than 256
+ };
+
+ for (int j = 0; j < testImages.length; j++)
+ {
+ BufferedImage testImage = testImages[j];
+
+ for (int i = 0; i < FORMAT_INFOS.length; i++)
+ {
+ FormatInfo formatInfo = FORMAT_INFOS[i];
+ if ((!formatInfo.canRead) || (!formatInfo.canWrite))
+ continue;
+
+ Debug.debug("bitmap test: " + formatInfo.format.name);
+
+ roundtrip(formatInfo, testImage, "bitmap", true);
+ }
+ }
+ }
+
+ public void testGrayscaleRoundtrip() throws IOException,
+ ImageReadException, ImageWriteException
+ {
+ BufferedImage testImages[] = { //
+
+ createArgbBitmapImage(1, 1), // minimal
+ createArgbGrayscaleImage(2, 2), //
+ createArgbGrayscaleImage(10, 10), // larger than 8
+ createArgbGrayscaleImage(300, 300), // larger than 256
+
+ createGrayscaleGrayscaleImage(1, 1), // minimal
+ createGrayscaleGrayscaleImage(2, 2), //
+ createGrayscaleGrayscaleImage(10, 10), // larger than 8
+ createGrayscaleGrayscaleImage(300, 300), // larger than 256
+ };
+
+ for (int j = 0; j < testImages.length; j++)
+ {
+ BufferedImage testImage = testImages[j];
+
+ for (int i = 0; i < FORMAT_INFOS.length; i++)
+ {
+ FormatInfo formatInfo = FORMAT_INFOS[i];
+ if ((!formatInfo.canRead) || (!formatInfo.canWrite))
+ continue;
+
+ Debug.debug("grayscale test: " + formatInfo.format.name);
+
+ boolean imageExact = true;
+ if (formatInfo.colorSupport == COLOR_BITMAP)
+ imageExact = false;
+
+ roundtrip(formatInfo, testImage, "gray", imageExact);
+ }
+ }
+ }
+
+ public void testLimitedColorRoundtrip() throws IOException,
+ ImageReadException, ImageWriteException
+ {
+ BufferedImage testImages[] = { //
+
+ createLimitedColorImage(1, 1), // minimal
+ createLimitedColorImage(2, 2), //
+ createLimitedColorImage(10, 10), // larger than 8
+ createLimitedColorImage(300, 300), // larger than 256
+ };
+
+ for (int j = 0; j < testImages.length; j++)
+ {
+ BufferedImage testImage = testImages[j];
+
+ for (int i = 0; i < FORMAT_INFOS.length; i++)
+ {
+ FormatInfo formatInfo = FORMAT_INFOS[i];
+ if ((!formatInfo.canRead) || (!formatInfo.canWrite))
+ continue;
+
+ Debug.debug("indexable test: " + formatInfo.format.name);
+
+ boolean imageExact = true;
+ if (formatInfo.colorSupport == COLOR_BITMAP)
+ imageExact = false;
+ if (formatInfo.colorSupport == COLOR_GRAYSCALE)
+ imageExact = false;
+
+ roundtrip(formatInfo, testImage, "indexable", imageExact);
+ }
+ }
+ }
+
+ public void testFullColorRoundtrip() throws IOException,
+ ImageReadException, ImageWriteException
+ {
+ BufferedImage testImages[] = { //
+
+ createFullColorImage(1, 1), // minimal
+ createFullColorImage(2, 2), //
+ createFullColorImage(10, 10), // larger than 8
+ createFullColorImage(300, 300), // larger than 256
+ };
+
+ for (int j = 0; j < testImages.length; j++)
+ {
+ BufferedImage testImage = testImages[j];
+
+ for (int i = 0; i < FORMAT_INFOS.length; i++)
+ {
+ FormatInfo formatInfo = FORMAT_INFOS[i];
+ if ((!formatInfo.canRead) || (!formatInfo.canWrite))
+ continue;
+
+ Debug.debug("fullColor test: " + formatInfo.format.name);
+
+ boolean imageExact = true;
+ if (formatInfo.colorSupport == COLOR_BITMAP)
+ imageExact = false;
+ if (formatInfo.colorSupport == COLOR_GRAYSCALE)
+ imageExact = false;
+ if (formatInfo.colorSupport == COLOR_LIMITED_INDEX)
+ imageExact = false;
+
+ roundtrip(formatInfo, testImage, "fullColor", imageExact);
+ }
+ }
+ }
+
+ private void roundtrip(FormatInfo formatInfo, BufferedImage testImage,
+ String tempPrefix, boolean imageExact) throws IOException,
+ ImageReadException, ImageWriteException
+ {
+ File temp1 = createTempFile(tempPrefix + ".", "."
+ + formatInfo.format.extension);
+ // Debug.debug("tempFile: " + tempFile.getName());
+
+ Map params = new HashMap();
+ Sanselan.writeImage(testImage, temp1, formatInfo.format, params);
+
+ Map readParams = new HashMap();
+ readParams.put(SanselanConstants.BUFFERED_IMAGE_FACTORY,
+ new RgbBufferedImageFactory());
+ BufferedImage image2 = Sanselan.getBufferedImage(temp1, readParams);
+ assertNotNull(image2);
+
+ if (imageExact)
+ {
+ // note tolerance when comparing grayscale images
+ // BufferedImages of
+ compareImagesExact(testImage, image2);
+ }
+
+ File temp2 = createTempFile(tempPrefix + ".", "."
+ + formatInfo.format.extension);
+ // Debug.debug("tempFile: " + tempFile.getName());
+ Sanselan.writeImage(image2, temp2, formatInfo.format, params);
- compareFilesExact(temp1, temp2);
- }
+ compareFilesExact(temp1, temp2);
+ }
}
Modified: commons/proper/sanselan/trunk/src/test/java/org/apache/sanselan/sampleUsage/ImageReadExample.java
URL: http://svn.apache.org/viewvc/commons/proper/sanselan/trunk/src/test/java/org/apache/sanselan/sampleUsage/ImageReadExample.java?rev=995634&r1=995633&r2=995634&view=diff
==============================================================================
--- commons/proper/sanselan/trunk/src/test/java/org/apache/sanselan/sampleUsage/ImageReadExample.java (original)
+++ commons/proper/sanselan/trunk/src/test/java/org/apache/sanselan/sampleUsage/ImageReadExample.java Fri Sep 10 01:35:50 2010
@@ -33,44 +33,44 @@ import org.apache.sanselan.common.IBuffe
public class ImageReadExample
{
- public static BufferedImage imageReadExample(File file)
- throws ImageReadException, IOException
- {
- Map params = new HashMap();
-
- // set optional parameters if you like
- params.put(SanselanConstants.BUFFERED_IMAGE_FACTORY,
- new ManagedImageBufferedImageFactory());
-
- // params.put(SanselanConstants.PARAM_KEY_VERBOSE, Boolean.TRUE);
-
- // read image
- BufferedImage image = Sanselan.getBufferedImage(file, params);
-
- return image;
- }
-
- public static class ManagedImageBufferedImageFactory
- implements
- IBufferedImageFactory
- {
-
- public BufferedImage getColorBufferedImage(int width, int height,
- boolean hasAlpha)
- {
- GraphicsEnvironment ge = GraphicsEnvironment
- .getLocalGraphicsEnvironment();
- GraphicsDevice gd = ge.getDefaultScreenDevice();
- GraphicsConfiguration gc = gd.getDefaultConfiguration();
- return gc.createCompatibleImage(width, height,
- Transparency.TRANSLUCENT);
- }
-
- public BufferedImage getGrayscaleBufferedImage(int width, int height,
- boolean hasAlpha)
- {
- return getColorBufferedImage(width, height, hasAlpha);
- }
- }
+ public static BufferedImage imageReadExample(File file)
+ throws ImageReadException, IOException
+ {
+ Map params = new HashMap();
+
+ // set optional parameters if you like
+ params.put(SanselanConstants.BUFFERED_IMAGE_FACTORY,
+ new ManagedImageBufferedImageFactory());
+
+ // params.put(SanselanConstants.PARAM_KEY_VERBOSE, Boolean.TRUE);
+
+ // read image
+ BufferedImage image = Sanselan.getBufferedImage(file, params);
+
+ return image;
+ }
+
+ public static class ManagedImageBufferedImageFactory
+ implements
+ IBufferedImageFactory
+ {
+
+ public BufferedImage getColorBufferedImage(int width, int height,
+ boolean hasAlpha)
+ {
+ GraphicsEnvironment ge = GraphicsEnvironment
+ .getLocalGraphicsEnvironment();
+ GraphicsDevice gd = ge.getDefaultScreenDevice();
+ GraphicsConfiguration gc = gd.getDefaultConfiguration();
+ return gc.createCompatibleImage(width, height,
+ Transparency.TRANSLUCENT);
+ }
+
+ public BufferedImage getGrayscaleBufferedImage(int width, int height,
+ boolean hasAlpha)
+ {
+ return getColorBufferedImage(width, height, hasAlpha);
+ }
+ }
}
Modified: commons/proper/sanselan/trunk/src/test/java/org/apache/sanselan/sampleUsage/ImageWriteExample.java
URL: http://svn.apache.org/viewvc/commons/proper/sanselan/trunk/src/test/java/org/apache/sanselan/sampleUsage/ImageWriteExample.java?rev=995634&r1=995633&r2=995634&view=diff
==============================================================================
--- commons/proper/sanselan/trunk/src/test/java/org/apache/sanselan/sampleUsage/ImageWriteExample.java (original)
+++ commons/proper/sanselan/trunk/src/test/java/org/apache/sanselan/sampleUsage/ImageWriteExample.java Fri Sep 10 01:35:50 2010
@@ -31,22 +31,22 @@ import org.apache.sanselan.formats.tiff.
public class ImageWriteExample
{
- public static byte[] imageWriteExample(File file)
- throws ImageReadException, ImageWriteException, IOException
- {
- // read image
- BufferedImage image = Sanselan.getBufferedImage(file);
-
- ImageFormat format = ImageFormat.IMAGE_FORMAT_TIFF;
- Map params = new HashMap();
-
- // set optional parameters if you like
- params.put(SanselanConstants.PARAM_KEY_COMPRESSION, new Integer(
- TiffConstants.TIFF_COMPRESSION_UNCOMPRESSED));
+ public static byte[] imageWriteExample(File file)
+ throws ImageReadException, ImageWriteException, IOException
+ {
+ // read image
+ BufferedImage image = Sanselan.getBufferedImage(file);
+
+ ImageFormat format = ImageFormat.IMAGE_FORMAT_TIFF;
+ Map params = new HashMap();
+
+ // set optional parameters if you like
+ params.put(SanselanConstants.PARAM_KEY_COMPRESSION, new Integer(
+ TiffConstants.TIFF_COMPRESSION_UNCOMPRESSED));
- byte bytes[] = Sanselan.writeImageToBytes(image, format, params);
+ byte bytes[] = Sanselan.writeImageToBytes(image, format, params);
- return bytes;
- }
+ return bytes;
+ }
}
Modified: commons/proper/sanselan/trunk/src/test/java/org/apache/sanselan/sampleUsage/MetadataExample.java
URL: http://svn.apache.org/viewvc/commons/proper/sanselan/trunk/src/test/java/org/apache/sanselan/sampleUsage/MetadataExample.java?rev=995634&r1=995633&r2=995634&view=diff
==============================================================================
--- commons/proper/sanselan/trunk/src/test/java/org/apache/sanselan/sampleUsage/MetadataExample.java (original)
+++ commons/proper/sanselan/trunk/src/test/java/org/apache/sanselan/sampleUsage/MetadataExample.java Fri Sep 10 01:35:50 2010
@@ -32,133 +32,133 @@ import org.apache.sanselan.formats.tiff.
public class MetadataExample
{
- public static void metadataExample(File file) throws ImageReadException,
- IOException
- {
- // get all metadata stored in EXIF format (ie. from JPEG or TIFF).
- // org.w3c.dom.Node node = Sanselan.getMetadataObsolete(imageBytes);
- IImageMetadata metadata = Sanselan.getMetadata(file);
-
- //System.out.println(metadata);
-
- if (metadata instanceof JpegImageMetadata)
- {
- JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata;
-
- // Jpeg EXIF metadata is stored in a TIFF-based directory structure
- // and is identified with TIFF tags.
- // Here we look for the "x resolution" tag, but
- // we could just as easily search for any other tag.
- //
- // see the TiffConstants file for a list of TIFF tags.
-
- System.out.println("file: " + file.getPath());
-
- // print out various interesting EXIF tags.
- printTagValue(jpegMetadata, TiffConstants.TIFF_TAG_XRESOLUTION);
- printTagValue(jpegMetadata, TiffConstants.TIFF_TAG_DATE_TIME);
- printTagValue(jpegMetadata,
- TiffConstants.EXIF_TAG_DATE_TIME_ORIGINAL);
- printTagValue(jpegMetadata, TiffConstants.EXIF_TAG_CREATE_DATE);
- printTagValue(jpegMetadata, TiffConstants.EXIF_TAG_ISO);
- printTagValue(jpegMetadata,
- TiffConstants.EXIF_TAG_SHUTTER_SPEED_VALUE);
- printTagValue(jpegMetadata, TiffConstants.EXIF_TAG_APERTURE_VALUE);
- printTagValue(jpegMetadata, TiffConstants.EXIF_TAG_BRIGHTNESS_VALUE);
- printTagValue(jpegMetadata, TiffConstants.GPS_TAG_GPS_LATITUDE_REF);
- printTagValue(jpegMetadata, TiffConstants.GPS_TAG_GPS_LATITUDE);
- printTagValue(jpegMetadata, TiffConstants.GPS_TAG_GPS_LONGITUDE_REF);
- printTagValue(jpegMetadata, TiffConstants.GPS_TAG_GPS_LONGITUDE);
-
- System.out.println();
-
- // simple interface to GPS data
- TiffImageMetadata exifMetadata = jpegMetadata.getExif();
- if (null != exifMetadata)
- {
- TiffImageMetadata.GPSInfo gpsInfo = exifMetadata.getGPS();
- if (null != gpsInfo)
- {
- String gpsDescription = gpsInfo.toString();
- double longitude = gpsInfo.getLongitudeAsDegreesEast();
- 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);
- }
- }
-
- // more specific example of how to manually access GPS values
- TiffField gpsLatitudeRefField = jpegMetadata
- .findEXIFValueWithExactMatch(TiffConstants.GPS_TAG_GPS_LATITUDE_REF);
- TiffField gpsLatitudeField = jpegMetadata
- .findEXIFValueWithExactMatch(TiffConstants.GPS_TAG_GPS_LATITUDE);
- TiffField gpsLongitudeRefField = jpegMetadata
- .findEXIFValueWithExactMatch(TiffConstants.GPS_TAG_GPS_LONGITUDE_REF);
- TiffField gpsLongitudeField = jpegMetadata
- .findEXIFValueWithExactMatch(TiffConstants.GPS_TAG_GPS_LONGITUDE);
- if (gpsLatitudeRefField != null && gpsLatitudeField != null
- && gpsLongitudeRefField != null
- && gpsLongitudeField != null)
- {
- // all of these values are strings.
- String gpsLatitudeRef = (String) gpsLatitudeRefField.getValue();
- RationalNumber gpsLatitude[] = (RationalNumber[]) (gpsLatitudeField
- .getValue());
- String gpsLongitudeRef = (String) gpsLongitudeRefField
- .getValue();
- RationalNumber gpsLongitude[] = (RationalNumber[]) gpsLongitudeField
- .getValue();
-
- RationalNumber gpsLatitudeDegrees = gpsLatitude[0];
- RationalNumber gpsLatitudeMinutes = gpsLatitude[1];
- RationalNumber gpsLatitudeSeconds = gpsLatitude[2];
-
- RationalNumber gpsLongitudeDegrees = gpsLongitude[0];
- RationalNumber gpsLongitudeMinutes = gpsLongitude[1];
- RationalNumber gpsLongitudeSeconds = gpsLongitude[2];
-
- // This will format the gps info like so:
- //
- // gpsLatitude: 8 degrees, 40 minutes, 42.2 seconds S
- // gpsLongitude: 115 degrees, 26 minutes, 21.8 seconds E
-
- 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();
-
- ArrayList items = jpegMetadata.getItems();
- for (int i = 0; i < items.size(); i++)
- {
- Object item = items.get(i);
- System.out.println(" " + "item: " + item);
- }
-
- System.out.println();
- }
- }
-
- private static void printTagValue(JpegImageMetadata jpegMetadata,
- TagInfo tagInfo)
- {
- 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 metadataExample(File file) throws ImageReadException,
+ IOException
+ {
+ // get all metadata stored in EXIF format (ie. from JPEG or TIFF).
+ // org.w3c.dom.Node node = Sanselan.getMetadataObsolete(imageBytes);
+ IImageMetadata metadata = Sanselan.getMetadata(file);
+
+ //System.out.println(metadata);
+
+ if (metadata instanceof JpegImageMetadata)
+ {
+ JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata;
+
+ // Jpeg EXIF metadata is stored in a TIFF-based directory structure
+ // and is identified with TIFF tags.
+ // Here we look for the "x resolution" tag, but
+ // we could just as easily search for any other tag.
+ //
+ // see the TiffConstants file for a list of TIFF tags.
+
+ System.out.println("file: " + file.getPath());
+
+ // print out various interesting EXIF tags.
+ printTagValue(jpegMetadata, TiffConstants.TIFF_TAG_XRESOLUTION);
+ printTagValue(jpegMetadata, TiffConstants.TIFF_TAG_DATE_TIME);
+ printTagValue(jpegMetadata,
+ TiffConstants.EXIF_TAG_DATE_TIME_ORIGINAL);
+ printTagValue(jpegMetadata, TiffConstants.EXIF_TAG_CREATE_DATE);
+ printTagValue(jpegMetadata, TiffConstants.EXIF_TAG_ISO);
+ printTagValue(jpegMetadata,
+ TiffConstants.EXIF_TAG_SHUTTER_SPEED_VALUE);
+ printTagValue(jpegMetadata, TiffConstants.EXIF_TAG_APERTURE_VALUE);
+ printTagValue(jpegMetadata, TiffConstants.EXIF_TAG_BRIGHTNESS_VALUE);
+ printTagValue(jpegMetadata, TiffConstants.GPS_TAG_GPS_LATITUDE_REF);
+ printTagValue(jpegMetadata, TiffConstants.GPS_TAG_GPS_LATITUDE);
+ printTagValue(jpegMetadata, TiffConstants.GPS_TAG_GPS_LONGITUDE_REF);
+ printTagValue(jpegMetadata, TiffConstants.GPS_TAG_GPS_LONGITUDE);
+
+ System.out.println();
+
+ // simple interface to GPS data
+ TiffImageMetadata exifMetadata = jpegMetadata.getExif();
+ if (null != exifMetadata)
+ {
+ TiffImageMetadata.GPSInfo gpsInfo = exifMetadata.getGPS();
+ if (null != gpsInfo)
+ {
+ String gpsDescription = gpsInfo.toString();
+ double longitude = gpsInfo.getLongitudeAsDegreesEast();
+ 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);
+ }
+ }
+
+ // more specific example of how to manually access GPS values
+ TiffField gpsLatitudeRefField = jpegMetadata
+ .findEXIFValueWithExactMatch(TiffConstants.GPS_TAG_GPS_LATITUDE_REF);
+ TiffField gpsLatitudeField = jpegMetadata
+ .findEXIFValueWithExactMatch(TiffConstants.GPS_TAG_GPS_LATITUDE);
+ TiffField gpsLongitudeRefField = jpegMetadata
+ .findEXIFValueWithExactMatch(TiffConstants.GPS_TAG_GPS_LONGITUDE_REF);
+ TiffField gpsLongitudeField = jpegMetadata
+ .findEXIFValueWithExactMatch(TiffConstants.GPS_TAG_GPS_LONGITUDE);
+ if (gpsLatitudeRefField != null && gpsLatitudeField != null
+ && gpsLongitudeRefField != null
+ && gpsLongitudeField != null)
+ {
+ // all of these values are strings.
+ String gpsLatitudeRef = (String) gpsLatitudeRefField.getValue();
+ RationalNumber gpsLatitude[] = (RationalNumber[]) (gpsLatitudeField
+ .getValue());
+ String gpsLongitudeRef = (String) gpsLongitudeRefField
+ .getValue();
+ RationalNumber gpsLongitude[] = (RationalNumber[]) gpsLongitudeField
+ .getValue();
+
+ RationalNumber gpsLatitudeDegrees = gpsLatitude[0];
+ RationalNumber gpsLatitudeMinutes = gpsLatitude[1];
+ RationalNumber gpsLatitudeSeconds = gpsLatitude[2];
+
+ RationalNumber gpsLongitudeDegrees = gpsLongitude[0];
+ RationalNumber gpsLongitudeMinutes = gpsLongitude[1];
+ RationalNumber gpsLongitudeSeconds = gpsLongitude[2];
+
+ // This will format the gps info like so:
+ //
+ // gpsLatitude: 8 degrees, 40 minutes, 42.2 seconds S
+ // gpsLongitude: 115 degrees, 26 minutes, 21.8 seconds E
+
+ 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();
+
+ ArrayList items = jpegMetadata.getItems();
+ for (int i = 0; i < items.size(); i++)
+ {
+ Object item = items.get(i);
+ System.out.println(" " + "item: " + item);
+ }
+
+ System.out.println();
+ }
+ }
+
+ private static void printTagValue(JpegImageMetadata jpegMetadata,
+ TagInfo tagInfo)
+ {
+ TiffField field = jpegMetadata.findEXIFValueWithExactMatch(tagInfo);
+ if (field == null)
+ System.out.println(tagInfo.name + ": " + "Not Found.");
+ else
+ System.out.println(tagInfo.name + ": "
+ + field.getValueDescription());
+ }
}
Modified: commons/proper/sanselan/trunk/src/test/java/org/apache/sanselan/sampleUsage/SampleUsage.java
URL: http://svn.apache.org/viewvc/commons/proper/sanselan/trunk/src/test/java/org/apache/sanselan/sampleUsage/SampleUsage.java?rev=995634&r1=995633&r2=995634&view=diff
==============================================================================
--- commons/proper/sanselan/trunk/src/test/java/org/apache/sanselan/sampleUsage/SampleUsage.java (original)
+++ commons/proper/sanselan/trunk/src/test/java/org/apache/sanselan/sampleUsage/SampleUsage.java Fri Sep 10 01:35:50 2010
@@ -34,77 +34,77 @@ import org.apache.sanselan.common.IImage
public class SampleUsage
{
- public SampleUsage()
- {
+ public SampleUsage()
+ {
- try
- {
- // <b>Code won't work unless these variables are properly initialized.
- // Sanselan works equally well with File, byte array or InputStream inputs.</b>
- BufferedImage someImage = null;
- byte someBytes[] = null;
- File someFile = null;
- InputStream someInputStream = null;
- OutputStream someOutputStream = null;
-
- // <b>The Sanselan class provides a simple interface to the library. </b>
-
- // <b>how to read an image: </b>
- byte imageBytes[] = someBytes;
- BufferedImage image_1 = Sanselan.getBufferedImage(imageBytes);
-
- // <b>methods of Sanselan usually accept files, byte arrays, or inputstreams as arguments. </b>
- BufferedImage image_2 = Sanselan.getBufferedImage(imageBytes);
- File file = someFile;
- BufferedImage image_3 = Sanselan.getBufferedImage(file);
- InputStream is = someInputStream;
- BufferedImage image_4 = Sanselan.getBufferedImage(is);
-
- // <b>Write an image. </b>
- BufferedImage image = someImage;
- File dst = someFile;
- ImageFormat format = ImageFormat.IMAGE_FORMAT_PNG;
- Map optional_params = new HashMap();
- Sanselan.writeImage(image, dst, format, optional_params);
-
- OutputStream os = someOutputStream;
- Sanselan.writeImage(image, os, format, optional_params);
-
- // <b>get the image's embedded ICC Profile, if it has one. </b>
- byte iccProfileBytes[] = Sanselan.getICCProfileBytes(imageBytes);
-
- ICC_Profile iccProfile = Sanselan.getICCProfile(imageBytes);
-
- // <b>get the image's width and height. </b>
- Dimension d = Sanselan.getImageSize(imageBytes);
-
- // <b>get all of the image's info (ie. bits per pixel, size, transparency, etc.) </b>
- ImageInfo imageInfo = Sanselan.getImageInfo(imageBytes);
-
- if (imageInfo.getColorType() == ImageInfo.COLOR_TYPE_GRAYSCALE)
- System.out.println("Grayscale image.");
- if (imageInfo.getHeight() > 1000)
- System.out.println("Large image.");
-
- // <b>try to guess the image's format. </b>
- ImageFormat image_format = Sanselan.guessFormat(imageBytes);
- image_format.equals(ImageFormat.IMAGE_FORMAT_PNG);
-
- // <b>get all metadata stored in EXIF format (ie. from JPEG or TIFF). </b>
- // <b>org.w3c.dom.Node node = Sanselan.getMetadataObsolete(imageBytes); </b>
- IImageMetadata metdata = Sanselan.getMetadata(imageBytes);
-
- // <b>print a dump of information about an image to stdout. </b>
- Sanselan.dumpImageFile(imageBytes);
-
- // <b>get a summary of format errors. </b>
- FormatCompliance formatCompliance = Sanselan
- .getFormatCompliance(imageBytes);
-
- }
- catch (Exception e)
- {
+ try
+ {
+ // <b>Code won't work unless these variables are properly initialized.
+ // Sanselan works equally well with File, byte array or InputStream inputs.</b>
+ BufferedImage someImage = null;
+ byte someBytes[] = null;
+ File someFile = null;
+ InputStream someInputStream = null;
+ OutputStream someOutputStream = null;
+
+ // <b>The Sanselan class provides a simple interface to the library. </b>
+
+ // <b>how to read an image: </b>
+ byte imageBytes[] = someBytes;
+ BufferedImage image_1 = Sanselan.getBufferedImage(imageBytes);
+
+ // <b>methods of Sanselan usually accept files, byte arrays, or inputstreams as arguments. </b>
+ BufferedImage image_2 = Sanselan.getBufferedImage(imageBytes);
+ File file = someFile;
+ BufferedImage image_3 = Sanselan.getBufferedImage(file);
+ InputStream is = someInputStream;
+ BufferedImage image_4 = Sanselan.getBufferedImage(is);
+
+ // <b>Write an image. </b>
+ BufferedImage image = someImage;
+ File dst = someFile;
+ ImageFormat format = ImageFormat.IMAGE_FORMAT_PNG;
+ Map optional_params = new HashMap();
+ Sanselan.writeImage(image, dst, format, optional_params);
+
+ OutputStream os = someOutputStream;
+ Sanselan.writeImage(image, os, format, optional_params);
+
+ // <b>get the image's embedded ICC Profile, if it has one. </b>
+ byte iccProfileBytes[] = Sanselan.getICCProfileBytes(imageBytes);
+
+ ICC_Profile iccProfile = Sanselan.getICCProfile(imageBytes);
+
+ // <b>get the image's width and height. </b>
+ Dimension d = Sanselan.getImageSize(imageBytes);
+
+ // <b>get all of the image's info (ie. bits per pixel, size, transparency, etc.) </b>
+ ImageInfo imageInfo = Sanselan.getImageInfo(imageBytes);
+
+ if (imageInfo.getColorType() == ImageInfo.COLOR_TYPE_GRAYSCALE)
+ System.out.println("Grayscale image.");
+ if (imageInfo.getHeight() > 1000)
+ System.out.println("Large image.");
+
+ // <b>try to guess the image's format. </b>
+ ImageFormat image_format = Sanselan.guessFormat(imageBytes);
+ image_format.equals(ImageFormat.IMAGE_FORMAT_PNG);
+
+ // <b>get all metadata stored in EXIF format (ie. from JPEG or TIFF). </b>
+ // <b>org.w3c.dom.Node node = Sanselan.getMetadataObsolete(imageBytes); </b>
+ IImageMetadata metdata = Sanselan.getMetadata(imageBytes);
+
+ // <b>print a dump of information about an image to stdout. </b>
+ Sanselan.dumpImageFile(imageBytes);
+
+ // <b>get a summary of format errors. </b>
+ FormatCompliance formatCompliance = Sanselan
+ .getFormatCompliance(imageBytes);
+
+ }
+ catch (Exception e)
+ {
- }
- }
+ }
+ }
}
Modified: commons/proper/sanselan/trunk/src/test/java/org/apache/sanselan/sampleUsage/WriteExifMetadataExample.java
URL: http://svn.apache.org/viewvc/commons/proper/sanselan/trunk/src/test/java/org/apache/sanselan/sampleUsage/WriteExifMetadataExample.java?rev=995634&r1=995633&r2=995634&view=diff
==============================================================================
--- commons/proper/sanselan/trunk/src/test/java/org/apache/sanselan/sampleUsage/WriteExifMetadataExample.java (original)
+++ commons/proper/sanselan/trunk/src/test/java/org/apache/sanselan/sampleUsage/WriteExifMetadataExample.java Fri Sep 10 01:35:50 2010
@@ -37,315 +37,315 @@ import org.apache.sanselan.util.IOUtils;
public class WriteExifMetadataExample
{
- public void removeExifMetadata(File jpegImageFile, File dst)
- throws IOException, ImageReadException, ImageWriteException
- {
- OutputStream os = null;
- try
- {
- os = new FileOutputStream(dst);
- os = new BufferedOutputStream(os);
-
- new ExifRewriter().removeExifMetadata(jpegImageFile, os);
- } finally
- {
- if (os != null)
- try
- {
- os.close();
- } catch (IOException e)
- {
-
- }
- }
- }
-
- /**
- * This example illustrates how to add/update EXIF metadata in a JPEG file.
- *
- * @param jpegImageFile
- * A source image file.
- * @param dst
- * The output file.
- * @throws IOException
- * @throws ImageReadException
- * @throws ImageWriteException
- */
- public void changeExifMetadata(File jpegImageFile, File dst)
- throws IOException, ImageReadException, ImageWriteException
- {
- OutputStream os = null;
- try
- {
- TiffOutputSet outputSet = null;
-
- // note that metadata might be null if no metadata is found.
- IImageMetadata metadata = Sanselan.getMetadata(jpegImageFile);
- JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata;
- if (null != jpegMetadata)
- {
- // note that exif might be null if no Exif metadata is found.
- TiffImageMetadata exif = jpegMetadata.getExif();
-
- if (null != exif)
- {
- // TiffImageMetadata class is immutable (read-only).
- // TiffOutputSet class represents the Exif data to write.
- //
- // Usually, we want to update existing Exif metadata by
- // changing
- // the values of a few fields, or adding a field.
- // In these cases, it is easiest to use getOutputSet() to
- // start with a "copy" of the fields read from the image.
- outputSet = exif.getOutputSet();
- }
- }
-
- // if file does not contain any exif metadata, we create an empty
- // set of exif metadata. Otherwise, we keep all of the other
- // existing tags.
- if (null == outputSet)
- outputSet = new TiffOutputSet();
-
- {
- // Example of how to add a field/tag to the output set.
- //
- // Note that you should first remove the field/tag if it already
- // exists in this directory, or you may end up with duplicate
- // tags. See above.
- //
- // Certain fields/tags are expected in certain Exif directories;
- // Others can occur in more than one directory (and often have a
- // different meaning in different directories).
- //
- // TagInfo constants often contain a description of what
- // directories are associated with a given tag.
- //
- // see
- // org.apache.sanselan.formats.tiff.constants.AllTagConstants
- //
- TiffOutputField aperture = TiffOutputField.create(
- TiffConstants.EXIF_TAG_APERTURE_VALUE,
- outputSet.byteOrder, new Double(0.3));
- TiffOutputDirectory exifDirectory = outputSet
- .getOrCreateExifDirectory();
- // make sure to remove old value if present (this method will
- // not fail if the tag does not exist).
- exifDirectory
- .removeField(TiffConstants.EXIF_TAG_APERTURE_VALUE);
- exifDirectory.add(aperture);
- }
-
- {
- // Example of how to add/update GPS info to output set.
-
- // New York City
- double longitude = -74.0; // 74 degrees W (in Degrees East)
- double latitude = 40 + 43 / 60.0; // 40 degrees N (in Degrees
- // North)
-
- outputSet.setGPSInDegrees(longitude, latitude);
- }
-
- // printTagValue(jpegMetadata, TiffConstants.TIFF_TAG_DATE_TIME);
-
- os = new FileOutputStream(dst);
- os = new BufferedOutputStream(os);
-
- new ExifRewriter().updateExifMetadataLossless(jpegImageFile, os,
- outputSet);
-
- os.close();
- os = null;
- } finally
- {
- if (os != null)
- try
- {
- os.close();
- } catch (IOException e)
- {
-
- }
- }
- }
-
- /**
- * This example illustrates how to remove a tag (if present) from EXIF
- * metadata in a JPEG file.
- *
- * In this case, we remove the "aperture" tag from the EXIF metadata if
- * present.
- *
- * @param jpegImageFile
- * A source image file.
- * @param dst
- * The output file.
- * @throws IOException
- * @throws ImageReadException
- * @throws ImageWriteException
- */
- public void removeExifTag(File jpegImageFile, File dst) throws IOException,
- ImageReadException, ImageWriteException
- {
- OutputStream os = null;
- try
- {
- TiffOutputSet outputSet = null;
-
- // note that metadata might be null if no metadata is found.
- IImageMetadata metadata = Sanselan.getMetadata(jpegImageFile);
- JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata;
- if (null != jpegMetadata)
- {
- // note that exif might be null if no Exif metadata is found.
- TiffImageMetadata exif = jpegMetadata.getExif();
-
- if (null != exif)
- {
- // TiffImageMetadata class is immutable (read-only).
- // TiffOutputSet class represents the Exif data to write.
- //
- // Usually, we want to update existing Exif metadata by
- // changing
- // the values of a few fields, or adding a field.
- // In these cases, it is easiest to use getOutputSet() to
- // start with a "copy" of the fields read from the image.
- outputSet = exif.getOutputSet();
- }
- }
-
- if (null == outputSet)
- {
- // file does not contain any exif metadata. We don't need to
- // update the file; just copy it.
- IOUtils.copyFileNio(jpegImageFile, dst);
- return;
- }
-
- {
- // Example of how to remove a single tag/field.
- // There are two ways to do this.
-
- // Option 1: brute force
- // Note that this approach is crude: Exif data is organized in
- // directories. The same tag/field may appear in more than one
- // directory, and have different meanings in each.
- outputSet.removeField(TiffConstants.EXIF_TAG_APERTURE_VALUE);
-
- // Option 2: precision
- // We know the exact directory the tag should appear in, in this
- // case the "exif" directory.
- // One complicating factor is that in some cases, manufacturers
- // will place the same tag in different directories.
- // To learn which directory a tag appears in, either refer to
- // the constants in ExifTagConstants.java or go to Phil Harvey's
- // EXIF website.
- TiffOutputDirectory exifDirectory = outputSet
- .getExifDirectory();
- if (null != exifDirectory)
- exifDirectory
- .removeField(TiffConstants.EXIF_TAG_APERTURE_VALUE);
- }
-
- os = new FileOutputStream(dst);
- os = new BufferedOutputStream(os);
-
- new ExifRewriter().updateExifMetadataLossless(jpegImageFile, os,
- outputSet);
-
- os.close();
- os = null;
- } finally
- {
- if (os != null)
- try
- {
- os.close();
- } catch (IOException e)
- {
-
- }
- }
- }
-
- /**
- * This example illustrates how to set the GPS values in JPEG EXIF metadata.
- *
- * @param jpegImageFile
- * A source image file.
- * @param dst
- * The output file.
- * @throws IOException
- * @throws ImageReadException
- * @throws ImageWriteException
- */
- public void setExifGPSTag(File jpegImageFile, File dst) throws IOException,
- ImageReadException, ImageWriteException
- {
- OutputStream os = null;
- try
- {
- TiffOutputSet outputSet = null;
-
- // note that metadata might be null if no metadata is found.
- IImageMetadata metadata = Sanselan.getMetadata(jpegImageFile);
- JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata;
- if (null != jpegMetadata)
- {
- // note that exif might be null if no Exif metadata is found.
- TiffImageMetadata exif = jpegMetadata.getExif();
-
- if (null != exif)
- {
- // TiffImageMetadata class is immutable (read-only).
- // TiffOutputSet class represents the Exif data to write.
- //
- // Usually, we want to update existing Exif metadata by
- // changing
- // the values of a few fields, or adding a field.
- // In these cases, it is easiest to use getOutputSet() to
- // start with a "copy" of the fields read from the image.
- outputSet = exif.getOutputSet();
- }
- }
-
- // if file does not contain any exif metadata, we create an empty
- // set of exif metadata. Otherwise, we keep all of the other
- // existing tags.
- if (null == outputSet)
- outputSet = new TiffOutputSet();
-
- {
- // Example of how to add/update GPS info to output set.
-
- // New York City
- double longitude = -74.0; // 74 degrees W (in Degrees East)
- double latitude = 40 + 43 / 60.0; // 40 degrees N (in Degrees
- // North)
-
- outputSet.setGPSInDegrees(longitude, latitude);
- }
-
- os = new FileOutputStream(dst);
- os = new BufferedOutputStream(os);
-
- new ExifRewriter().updateExifMetadataLossless(jpegImageFile, os,
- outputSet);
-
- os.close();
- os = null;
- } finally
- {
- if (os != null)
- try
- {
- os.close();
- } catch (IOException e)
- {
-
- }
- }
- }
+ public void removeExifMetadata(File jpegImageFile, File dst)
+ throws IOException, ImageReadException, ImageWriteException
+ {
+ OutputStream os = null;
+ try
+ {
+ os = new FileOutputStream(dst);
+ os = new BufferedOutputStream(os);
+
+ new ExifRewriter().removeExifMetadata(jpegImageFile, os);
+ } finally
+ {
+ if (os != null)
+ try
+ {
+ os.close();
+ } catch (IOException e)
+ {
+
+ }
+ }
+ }
+
+ /**
+ * This example illustrates how to add/update EXIF metadata in a JPEG file.
+ *
+ * @param jpegImageFile
+ * A source image file.
+ * @param dst
+ * The output file.
+ * @throws IOException
+ * @throws ImageReadException
+ * @throws ImageWriteException
+ */
+ public void changeExifMetadata(File jpegImageFile, File dst)
+ throws IOException, ImageReadException, ImageWriteException
+ {
+ OutputStream os = null;
+ try
+ {
+ TiffOutputSet outputSet = null;
+
+ // note that metadata might be null if no metadata is found.
+ IImageMetadata metadata = Sanselan.getMetadata(jpegImageFile);
+ JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata;
+ if (null != jpegMetadata)
+ {
+ // note that exif might be null if no Exif metadata is found.
+ TiffImageMetadata exif = jpegMetadata.getExif();
+
+ if (null != exif)
+ {
+ // TiffImageMetadata class is immutable (read-only).
+ // TiffOutputSet class represents the Exif data to write.
+ //
+ // Usually, we want to update existing Exif metadata by
+ // changing
+ // the values of a few fields, or adding a field.
+ // In these cases, it is easiest to use getOutputSet() to
+ // start with a "copy" of the fields read from the image.
+ outputSet = exif.getOutputSet();
+ }
+ }
+
+ // if file does not contain any exif metadata, we create an empty
+ // set of exif metadata. Otherwise, we keep all of the other
+ // existing tags.
+ if (null == outputSet)
+ outputSet = new TiffOutputSet();
+
+ {
+ // Example of how to add a field/tag to the output set.
+ //
+ // Note that you should first remove the field/tag if it already
+ // exists in this directory, or you may end up with duplicate
+ // tags. See above.
+ //
+ // Certain fields/tags are expected in certain Exif directories;
+ // Others can occur in more than one directory (and often have a
+ // different meaning in different directories).
+ //
+ // TagInfo constants often contain a description of what
+ // directories are associated with a given tag.
+ //
+ // see
+ // org.apache.sanselan.formats.tiff.constants.AllTagConstants
+ //
+ TiffOutputField aperture = TiffOutputField.create(
+ TiffConstants.EXIF_TAG_APERTURE_VALUE,
+ outputSet.byteOrder, new Double(0.3));
+ TiffOutputDirectory exifDirectory = outputSet
+ .getOrCreateExifDirectory();
+ // make sure to remove old value if present (this method will
+ // not fail if the tag does not exist).
+ exifDirectory
+ .removeField(TiffConstants.EXIF_TAG_APERTURE_VALUE);
+ exifDirectory.add(aperture);
+ }
+
+ {
+ // Example of how to add/update GPS info to output set.
+
+ // New York City
+ double longitude = -74.0; // 74 degrees W (in Degrees East)
+ double latitude = 40 + 43 / 60.0; // 40 degrees N (in Degrees
+ // North)
+
+ outputSet.setGPSInDegrees(longitude, latitude);
+ }
+
+ // printTagValue(jpegMetadata, TiffConstants.TIFF_TAG_DATE_TIME);
+
+ os = new FileOutputStream(dst);
+ os = new BufferedOutputStream(os);
+
+ new ExifRewriter().updateExifMetadataLossless(jpegImageFile, os,
+ outputSet);
+
+ os.close();
+ os = null;
+ } finally
+ {
+ if (os != null)
+ try
+ {
+ os.close();
+ } catch (IOException e)
+ {
+
+ }
+ }
+ }
+
+ /**
+ * This example illustrates how to remove a tag (if present) from EXIF
+ * metadata in a JPEG file.
+ *
+ * In this case, we remove the "aperture" tag from the EXIF metadata if
+ * present.
+ *
+ * @param jpegImageFile
+ * A source image file.
+ * @param dst
+ * The output file.
+ * @throws IOException
+ * @throws ImageReadException
+ * @throws ImageWriteException
+ */
+ public void removeExifTag(File jpegImageFile, File dst) throws IOException,
+ ImageReadException, ImageWriteException
+ {
+ OutputStream os = null;
+ try
+ {
+ TiffOutputSet outputSet = null;
+
+ // note that metadata might be null if no metadata is found.
+ IImageMetadata metadata = Sanselan.getMetadata(jpegImageFile);
+ JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata;
+ if (null != jpegMetadata)
+ {
+ // note that exif might be null if no Exif metadata is found.
+ TiffImageMetadata exif = jpegMetadata.getExif();
+
+ if (null != exif)
+ {
+ // TiffImageMetadata class is immutable (read-only).
+ // TiffOutputSet class represents the Exif data to write.
+ //
+ // Usually, we want to update existing Exif metadata by
+ // changing
+ // the values of a few fields, or adding a field.
+ // In these cases, it is easiest to use getOutputSet() to
+ // start with a "copy" of the fields read from the image.
+ outputSet = exif.getOutputSet();
+ }
+ }
+
+ if (null == outputSet)
+ {
+ // file does not contain any exif metadata. We don't need to
+ // update the file; just copy it.
+ IOUtils.copyFileNio(jpegImageFile, dst);
+ return;
+ }
+
+ {
+ // Example of how to remove a single tag/field.
+ // There are two ways to do this.
+
+ // Option 1: brute force
+ // Note that this approach is crude: Exif data is organized in
+ // directories. The same tag/field may appear in more than one
+ // directory, and have different meanings in each.
+ outputSet.removeField(TiffConstants.EXIF_TAG_APERTURE_VALUE);
+
+ // Option 2: precision
+ // We know the exact directory the tag should appear in, in this
+ // case the "exif" directory.
+ // One complicating factor is that in some cases, manufacturers
+ // will place the same tag in different directories.
+ // To learn which directory a tag appears in, either refer to
+ // the constants in ExifTagConstants.java or go to Phil Harvey's
+ // EXIF website.
+ TiffOutputDirectory exifDirectory = outputSet
+ .getExifDirectory();
+ if (null != exifDirectory)
+ exifDirectory
+ .removeField(TiffConstants.EXIF_TAG_APERTURE_VALUE);
+ }
+
+ os = new FileOutputStream(dst);
+ os = new BufferedOutputStream(os);
+
+ new ExifRewriter().updateExifMetadataLossless(jpegImageFile, os,
+ outputSet);
+
+ os.close();
+ os = null;
+ } finally
+ {
+ if (os != null)
+ try
+ {
+ os.close();
+ } catch (IOException e)
+ {
+
+ }
+ }
+ }
+
+ /**
+ * This example illustrates how to set the GPS values in JPEG EXIF metadata.
+ *
+ * @param jpegImageFile
+ * A source image file.
+ * @param dst
+ * The output file.
+ * @throws IOException
+ * @throws ImageReadException
+ * @throws ImageWriteException
+ */
+ public void setExifGPSTag(File jpegImageFile, File dst) throws IOException,
+ ImageReadException, ImageWriteException
+ {
+ OutputStream os = null;
+ try
+ {
+ TiffOutputSet outputSet = null;
+
+ // note that metadata might be null if no metadata is found.
+ IImageMetadata metadata = Sanselan.getMetadata(jpegImageFile);
+ JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata;
+ if (null != jpegMetadata)
+ {
+ // note that exif might be null if no Exif metadata is found.
+ TiffImageMetadata exif = jpegMetadata.getExif();
+
+ if (null != exif)
+ {
+ // TiffImageMetadata class is immutable (read-only).
+ // TiffOutputSet class represents the Exif data to write.
+ //
+ // Usually, we want to update existing Exif metadata by
+ // changing
+ // the values of a few fields, or adding a field.
+ // In these cases, it is easiest to use getOutputSet() to
+ // start with a "copy" of the fields read from the image.
+ outputSet = exif.getOutputSet();
+ }
+ }
+
+ // if file does not contain any exif metadata, we create an empty
+ // set of exif metadata. Otherwise, we keep all of the other
+ // existing tags.
+ if (null == outputSet)
+ outputSet = new TiffOutputSet();
+
+ {
+ // Example of how to add/update GPS info to output set.
+
+ // New York City
+ double longitude = -74.0; // 74 degrees W (in Degrees East)
+ double latitude = 40 + 43 / 60.0; // 40 degrees N (in Degrees
+ // North)
+
+ outputSet.setGPSInDegrees(longitude, latitude);
+ }
+
+ os = new FileOutputStream(dst);
+ os = new BufferedOutputStream(os);
+
+ new ExifRewriter().updateExifMetadataLossless(jpegImageFile, os,
+ outputSet);
+
+ os.close();
+ os = null;
+ } finally
+ {
+ if (os != null)
+ try
+ {
+ os.close();
+ } catch (IOException e)
+ {
+
+ }
+ }
+ }
}