Archive

Archive for July, 2011

ColdFusion 9: Another Way to Extract FLV Duration

July 29, 2011 5 comments

I saw an interesting question this week about using Tika with Railo.   If you are like me, and know very little about Tika,  it is described as a library that

    … detects and extracts metadata and structured text content from various documents..

Now despite knowing very little about it, I was pretty sure it was included in ColdFusion 9 (with its  Solr integration).  I took a look around and sure enough ColdFusion 9 includes a slightly older version of Tika (0.6) under the hood.

While reviewing the API I noticed it mentioned a simple FLVParser.  Interesting.  So I decided to give it a whirl.  First I created an instance of Metadata to store the details retrieved from my test file.  Next I loaded my sample file as an input stream.  Finally, I ran the objects through the Tika parser to actually populate the metadata.  Then with a simple call to Metadata.get() I had the file’s duration.

<cfscript>
    path   = "c:/myFile.flv";
    Tika   = createObject("java", "org.apache.tika.Tika");
    meta   = createObject("java", "org.apache.tika.metadata.Metadata").init();
    stream = createObject("java","java.io.FileInputStream").init( path );

    try {
        Tika.parse(stream, meta);
        WriteOutput("<p>duration="& meta.get("duration") &"</p>");
        // ....
    }
    finally {
        stream.close();
    }
</cfscript>

The parser also extracts a few other interesting details like framerate, width, height and a few others. To review all of the properties found,  just invoke MetaData.names() . Then iterate through the array of property names returned.  Keep in mind this class is just a simple parser. But no extra jars are required to use it!


<cfscript>
    // get all properties names returned
    names = meta.names();
    for (i = 1; i <= arrayLen(names); i++) {
       key = names[i];
       writeOutput("<p>"& key &"="& meta.get( javacast("string", key)) &"</p>");
    }
</cfscript>
Categories: ColdFusion, Java Tags: , , ,

ColdFusion 9 – How to Stream a CFSpreadSheet to the Browser

July 29, 2011 1 comment

I have seen this question raised a few times, but found no examples in the documentation.  So in case you were wondering how to stream a spreadsheet to a browser:

Code:

<!--- create your spreadsheet --->
<cfset sheet = SpreadSheetNew()>
<cfset SpreadsheetAddRow(sheet, "foo,bar")>

<!--- stream it to the browser --->
<cfheader name="Content-Disposition" value="inline; filename=foo.xls">
<cfcontent type="application/vnd.ms-excel" variable="#SpreadSheetReadBinary(sheet)#">

 

Categories: ColdFusion Tags: ,

Creating a Dial Chart with ColdFusion and JFreeChart

A recent question on stackoverflow.com prompted me to take a look at how to create dial charts with JFreeChart (rather than ACF’s webcharts3D engine).  Since you never know when starter code snippets will come in handy, here is the rudimentary example I put together for creating Dial Charts.  You can obviously do much more with it. Just review the api and/or the developer guide.

Sample Dial Chart

Installation:

Download latest jfreeChart. Copy the following jars into {cf_root}\WEB-INF\lib and restart CF. Note, jar version numbers may vary.

  • jfreechart-1.0.13.jar
  • jcommon-1.0.16.jar

Dial Chart Example (Compatibility MX7+)

<cfscript>
// my chart settings
chartTitle = "My Dial Chart";
arrowValue  = 55;
dialMinimum = 0;
dialMaximum = 100;
chartWidth  = 500;
chartHeight = 500;

// initialize basic components of the chart
// see jFreeChart API on how to customize the components settings further
DefaultValueDataset = createObject("java", "org.jfree.data.general.DefaultValueDataset");
pointerValue    = DefaultValueDataset.init(arrowValue);
dialPointer     = createObject("java", "org.jfree.chart.plot.dial.DialPointer$Pointer").init();
dialFrame       = createObject("java", "org.jfree.chart.plot.dial.StandardDialFrame").init();
dialBackground  = createObject("java", "org.jfree.chart.plot.dial.DialBackground").init();

// tweak the default range to make it more appealing.
// see angle/extent: http://java.sun.com/developer/technicalArticles/GUI/java2d/java2dpart1.html
dialScale = createObject("java", "org.jfree.chart.plot.dial.StandardDialScale").init();
dialScale.setLowerBound(dialMinimum);
dialScale.setUpperBound(dialMaximum);
dialScale.setStartAngle(-150);
dialScale.setExtent(-240);

//initialize plot and apply settings
plot = createObject("java", "org.jfree.chart.plot.dial.DialPlot").init();
plot.setDialFrame(dialFrame);
plot.setBackground(dialBackground);
plot.setDataset(pointerValue);
plot.addScale(0, dialScale);
plot.addPointer(dialPointer);

// create chart and convert it to an image
chart = createObject("java", "org.jfree.chart.JFreeChart").init(chartTitle, plot);
chartImage = chart.createBufferedImage(chartWidth, chartHeight);
ImageFormat = createObject("java", "org.jfree.chart.encoders.ImageFormat");
EncoderUtil = createObject("java", "org.jfree.chart.encoders.EncoderUtil");
bytes = EncoderUtil.encode( chartImage, ImageFormat.PNG);
</cfscript>

<!--- display in browser --->
<cfcontent type="image/png" variable="#bytes#">
Categories: ColdFusion, Java Tags: ,

Creating a Meter Chart with ColdFusion and JFreeChart

A recent question on stackoverflow.com prompted me to take a look at how to create dial charts with JFreeChart (rather than ACF’s webcharts3D engine).  Since you never know when starter code snippets will come in handy, here is the quick and dirty example I put together for creating Meter Charts.

Meter Chart Example (Compatibility MX7+)

<cfscript>
    // my chart settings
    chartTitle = "My Meter Chart";
    arrowValue  = 55;
    arrowUnits  = "widgets";
    chartWidth  = 500;
    chartHeight = 500;

    // initialize meter ranges (LOW, MEDIUM, HIGH)
    // note: quick and ugly code in dire need of improvement ...
    low  = createSolidMeterInterval("Low", 0, 40, createAwtColor(0, 255, 0, 120));
    med  = createSolidMeterInterval("Med", 40, 60, createAwtColor(255, 255, 0, 120));
    high = createSolidMeterInterval("High", 60, 100, createAwtColor(255, 0, 0, 120));

    // initialize arrow value
    DefaultValueDataset = createObject("java", "org.jfree.data.general.DefaultValueDataset");
    meterPointer = DefaultValueDataset.init(arrowValue);

    //initialize plot and apply settings
    plot = createObject("java", "org.jfree.chart.plot.MeterPlot").init();
    plot.setDataset(meterPointer);
    plot.setTickLabelsVisible(true);
    plot.addInterval(low);
    plot.addInterval(med);
    plot.addInterval(high);
    plot.setUnits(arrowUnits);

    // create chart and convert it to an image
    chart = createObject("java", "org.jfree.chart.JFreeChart").init(chartTitle, plot);
    ChartUtilities = createObject("java", "org.jfree.chart.ChartUtilities");
    ChartUtilities.applyCurrentTheme(chart);

    // applyCurrentTheme seems to overwrite some settings, so we must reapply them
    Color = createObject("java", "java.awt.Color");
    plot.setBackgroundPaint(Color.GRAY);
    plot.setNeedlePaint(Color.BLACK);

    chartImage = chart.createBufferedImage(chartWidth, chartHeight);
    ImageFormat = createObject("java", "org.jfree.chart.encoders.ImageFormat");
    EncoderUtil = createObject("java", "org.jfree.chart.encoders.EncoderUtil");
    bytes = EncoderUtil.encode( chartImage, ImageFormat.PNG);
</cfscript>
<!--- display in browser --->
<cfcontent type="image/png" variable="#bytes#">

Auxilary Functions:

<cfscript>
        // quick and ugly functions. could be improved ...
        function createSolidMeterInterval(Title, fromValue, toValue, BgColor) {
            var Range = createObject("java", "org.jfree.data.Range").init(arguments.fromValue, arguments.toValue);
            var MeterInterval = createObject("java", "org.jfree.chart.plot.MeterInterval");
            return MeterInterval.init(arguments.Title, Range   // interval from / to range
                                        , javacast("null", "") // outline color
                                        , javacast("null", "") // outline stroke
                                        , arguments.BgColor    // background color
                                    );
        }

        // using java.awt.Color is a pain due to all the javacasts ...
        function createAwtColor(r, g, b, alpha) {
            var color = createObject("java", "java.awt.Color");
            return color.init( javacast("int", arguments.r)
                                , javacast("int", arguments.g)
                                , javacast("int", arguments.b)
                                , javacast("int", arguments.alpha) // transparency
                            );
        }
</cfscript>
Categories: ColdFusion, Java Tags: ,