Leave That Thing Alone Blog

Viewing Camera RAW Images with ColdFusion

This example demonstrates how to open/save/view Camera RAW images with ColdFusion and jrawio. jrawio is a Service Provider Implementation for the java ImageIO it provides the ability to read camera raw image formats such as Canon CR2/CRW and Nikon NEF. Check the jrawio site for a list of currently supported cameras.

Setup jrawio

The first step is to download the jrawio binary.
Take the jar (it.tidalwave.imageio.raw-[version].jar) and place it in ColdFusion's "\lib\" directory.

Read RAW image and write to JPG

This first example reads a camera raw file and converts it to a JPG and writes it to a file. The benefit of writing the file as a JPG is that this process is relatively fast.

<cfscript>
	//input file
	filename = expandPath("CRW_3933.CRW");
	fileio = createObject("Java","java.io.File").init(filename);
	//read RAW
	imageio = createObject("Java","javax.imageio.ImageIO").read(fileio);
	//write jpg
	outname = expandPath("CRW_3933.jpg");
	output = createObject("Java","java.io.File").init(outname);
	createObject("Java","javax.imageio.ImageIO").write(imageio, "jpg", output);
</cfscript>

Read RAW image, resize, and writetobrowser

This next example reads a camera raw image, resizes the image to a thumbnail, then writes that image to browser. Warning this can be very slow.

<cfscript>
	//input file
	filename = expandPath("CRW_3933.CRW");
	fileio = createObject("Java","java.io.File").init(filename);
	//read RAW
	imageio = createObject("Java","javax.imageio.ImageIO").read(fileio);
	//resize
	imageWidth = 200;
	imageHeight = 200;
	scaledImage = imageio.getScaledInstance(JavaCast("int", imageWidth), JavaCast("int", imageHeight), imageio.SCALE_FAST);
	bufferedImage = createObject("java", "java.awt.image.BufferedImage").init(JavaCast("int", imageWidth), JavaCast("int", imageHeight), imageio.TYPE_INT_RGB);
	graphics = bufferedImage.createGraphics();
	graphics.drawImage(scaledImage, 0, 0, Javacast("null", ""));
	//create cfimage from buffered image
	cfimage = imageNew(bufferedImage);
</cfscript>

<cfimage action="writeToBrowser" source="#cfimage#">

Can be slow...

Be aware that processing of RAW images can be very slow and processor intensive. For example the second code sample may take 10-30 seconds with a large raw image.

Retrieve Exif Metadata from Camera RAW and TIFF in ColdFusion 8

This example demonstrates how to get Exif metadata from Camera RAW and TIFF files in ColdFusion. In this sample I'm using a raw CR2 file from a Canon D30. Please note this example only reads RAW exif metdata, it does not read/process the RAW image

update: Viewing Camera RAW Images with ColdFusion

ColdFusion 8's image tags allow the retrieval of Exif metadata from only certain image types, ColdFusion actually uses Drew Noakes Metadata Extract library. There is now a beta version (metadata-extractor-2.4.0-beta-1.jar) that introduces code for processing camera RAW images and TIFF files.

One way to use this beta library is to go into the [coldfusion8]\lib\ folder and rename the 'metadata-extractor-2.2.2.jar' file to .bak then copy the latest metadata-extractor JAR file into that directory.

However, in this example i'm going to use Mark Mandel's JavaLoader so I can use the beta library separately from the stable version cfimage uses.

The code below reads a camera Raw image then iterates through all the metadata:

<cfscript>
	//read Canon RAW CR2 file
	photoFile = createObject("java","java.io.File").init("#expandpath('IMG_7913.CR2')#");
	//set the path
	paths[1] = "D:\test\metadata-extractor-2.4.0-beta-1.jar";
	//create the loader
	javaLoader = createObject("component", "JavaLoader").init(paths);
	//create the TiffMetadataReader instace
	tiffMetadataReader = javaLoader.create("com.drew.imaging.tiff.TiffMetadataReader");
	//read metadata
	metadata = tiffMetadataReader.readMetadata(photoFile);
	//get directories
	directories = metadata.getDirectoryIterator();
</cfscript>

<cfoutput> <table cellspacing="0"> <cfloop condition="directories.hasNext()"> <cfset currentDirectory = directories.next() /> <cfset tags = currentDirectory.getTagIterator() /> <cfloop condition="tags.hasNext()"> <cfset currentTag = tags.next()> <tr> <td valign="top">#currentTag.getTagType()#</td> <td style="vertical-align:top;width:150px;">#currentTag.getTagName()#</td> <td valign="top">#currentTag.getDescription()# </td> <td>#currentTag.getTagTypeHex()#</td> </tr> </cfloop> </cfloop> </table> </cfoutput>

Sample output:

coldfusion raw tiff exif