Monday, March 18, 2013

Training Courses on Tap for Spring 2013

It's that time again. Flowers are sprouting, pollen is flowing, and, lo and behold, Chariot is running more training...

We have some exciting additions to our courses this quarter, including a guest course by Neosoft, our usual Spring training courses, a Groovy and Grails offering, and more. Here's our lineup:

  • March 26-28 - Comprehensive Maven 3 and Nexus training - We are running a 3 day maven intro, intermediate and advanced course starting on March 26. You'll learn how to build Java application with Maven, including JARs, WARs, and multi-module projects.
  • April 9-12 - Core Spring Developer Training/Certification - Recently updated, this course covers Spring 3.1 and above, all three styles of dependency injection (XML, annotations, JavaConfig), JDBC, Hibernate/JPA, an introduction to Spring Web MVC, Security, JMX and more. A deep discussion of the Spring lifecycle and features such as proxying, interface-driven development, JUnit-based testing and Aspect Oriented Programming provides a strong foundation for new Spring developers.
  • April 17-19 - Neo4j Tutorial by Neo Technology - We are featuring a guest training course by NeoSoft, discussing their NoSQL Graph database, Neo4J. This is a two-day course to get developers up to speed in using Neo4J.
  • April 30-May 3 - Groovy and Grails v2.0 - new for Chariot training, we're featuring a four-day Groovy and Grails training course, written by the developers of Grails at VMware. This course begins with a heavy introduction to the Groovy programming language, and then dives into Grails, the convention-driven Spring-and-Hibernate application framework. Configuration, controllers, services, scaffolding, testing, writing plugins, and GSP tags are covered.
  • May 9-10 - Advanced Scala - This Typesafe course, taught by Michael Pigg, takes our Scala training even further than Fast Track to Scala, and includes a deep dive into functional programming concepts, internal DSLs, custom Scala collections, and details on the Scala type system.
As usual, we offer discounts for Chariot training alumni. Contact us for information on obtaining a discount code - please provide your name, course, and estimated training date. We also provide private training and can offer courses in Hadoop, Java, Tomcat, and other technologies. Check our course catalog for more information.

Friday, March 15, 2013

Recap - Chariot Day 2013 - Our internal conference...

One thing Chariot people love to do is learn new technologies and techniques. Being consultants constantly challenged in the field, keeping our blades sharp is key to doing battle. Every year or so we get together and run our own internal conference to see what each of our current passions are. We call it Chariot Day.

Earlier this month, on a Saturday, we had two rooms going from 9-5PM, each with 45-minute sessions on subjects such as:
  • Becoming a better programmer, Mythbusters Style
  • Async networking for Android w/Robospice
  • A great session on zsh
  • Data Mining - it's not just for kids
  • Universal EventBus with Vert.x
  • Functional programming with Scala
  • Single-page Javascript with AngularJS
  • Billions of Things (you can imagine scalability and performance on a large scale)
  • Having fun with the Raspberry Pi
  • HTML5 Canvas
  • Understanding the Persistent Data Structures of Clojure
  • Get low w/HBase
  • Getting started with Phone Gap
  • 20 tools that turbo charge a 1.51 person development team
We will be reviewing the content from these sessions to see if we can share them with you. 

Wednesday, March 13, 2013

Take (and Manipulate!) a Photo with a Web Page

So not that long ago, if you wanted an app to take a photo, it had to be a native app -- such as a Windows/Mac app or a native mobile application.  But HTML5 has brought a number of new APIs that allow not only taking photos, but analyzing and manipulating them all within a browser.

Sadly, there is still inconsistent support for these features across browsers (both desktop and mobile).  So in practice, the best approach may be to target the APIs supported by iOS 6, which is more or less the minimal feature set across browsers that support any of this at all.

The three pieces we'll need are:
  • The Canvas
  • File I/O
  • Changes to the File Upload input and Media Capture
    • Note that Media Capture has been superceded by getUserMedia (more info) but we don't use that here because iOS doesn't support getUserMedia yet

The Canvas is a space on the page to draw into, using either 2-D or 3-D APIs.  Conveniently, it also lets you draw images into it, inspect and alter the pixel data, and export its content as an image. Canvas is supported by IE 9 and modern versions of Firefox, WebKit/Safari/iOS 6, Opera, and Chrome.  However, not all browsers support 3-D (via WebGL), and browsers don't generally support ALL of the Canvas spec (which has continued to evolve even after initial browser support).  Still, the parts we need are supported across browsers.

The additional "accept" and "capture" attributes on the file upload input element let us specify that the user should be able to select an image file, getting it from an attached camera if possible.  Crucially, on iOS you are given the option to select a photo from the photo library or take a photo on the spot (though some desktop browsers only let you browse for an image file, even if a Webcam is attached). Then the "files" property on the input element lets us access the selected files (unfortunately, not supported in IE < 10).

So the formula for taking and manipulating a photo is:
  1. Configure a file input using the new attributes
  2. When an image file is selected (or photo taken), access the image file
  3. Check the image EXIF data for rotation using File I/O
  4. Decide what size to scale the image to (if needed)
  5. Configure a Canvas for the selected image dimensions
  6. Draw the image to the Canvas, rotating and scaling as appropriate
  7. Access and manipulate the pixel data for the Canvas
  8. Write the pixel data back to the Canvas
  9. Export the image on the Canvas as an image file

Taking these one step at a time (and note that I use jQuery for convenience, but it is not needed for this to work):

1. Configure a file input using the new attributes

There are two ways to configure the input (and it makes no difference to desktop browsers). An input configured like this will prompt the user to take a photo or select a saved impage on either iOS or Android:
<input type="file" id="PhotoPicker"
       accept="image/*" />
With the additional capture="camera" attribute, Android goes directly to the camera without giving the option to use a saved photo (though the iOS behavior is the same):
<input type="file" id="PhotoPicker"
       accept="image/*" capture="camera" />
Note that if you don't like the default appearance, you can conceal the input widget and connect a button or link (styled any way you like) to activate it.  The input doesn't work if set to display: 'none', but we can put it inside a div of size 0 to effectively hide it:
<div style="width: 0; height: 0; overflow: hidden;">
    <input type="file" id="PhotoPicker" 
           accept="image/*" capture="camera" />
</div>
<button class="lovely" id="PhotoButton">1. Select Photo</button>
$('#PhotoButton').click(function() {
    $('#PhotoPicker').trigger('click');
    return false;
});

2. When an image file is selected (or photo taken), access the image file

We just use the new files property on the input:
$('#PhotoPicker').on('change', function(e) {
    e.preventDefault();
    if(this.files.length === 0) return;
    var imageFile = this.files[0];
    ...
});

3. Check the image EXIF data for rotation using File I/O:

All images may have a rotation recorded by the camera, such that no matter how the camera was oriented when the photo was taken, the image can be displayed appropriately on screen.  In particular, an iPad considers the landscape orientation to be normal (though it can still be upside-down), and the portrait orientation takes photos with a 90 degree rotation.  The EXIF header in an image records this rotation, so we can read it in order to display the image appropriately.

This part uses two open source (MPL) EXIF-reading scripts:
<script src="http://www.nihilogic.dk/labs/exif/exif.js"
       type="text/javascript"></script>
<script src="http://www.nihilogic.dk/labs/binaryajax/binaryajax.js"
       type="text/javascript"></script>
Then we use the new File API to read the image data (and there's a nice image here describing the EXIF Orientation codes):
var width;
var height;
var binaryReader = new FileReader();
binaryReader.onloadend=function(d) {
    var exif, transform = "none";
    exif=EXIF.readFromBinaryFile(createBinaryFile(d.target.result));

    if(exif.Orientation === 8) {
        width = img.height;
        height = img.width;
        transform = "left";
    } else if(exif.Orientation === 6) {
        width = img.height;
        height = img.width;
        transform = "right";
    }
    ...
};

binaryReader.readAsArrayBuffer(imageFile);

4. Decide what size to scale the image to (if needed)

If you want to limit the size of the image, you can scale the height and width accordingly. With mobile devices with high-resolution cameras, this may be necessary to limit memory usage:
var MAX_WIDTH = 1024;
var MAX_HEIGHT = 768;
if (width/MAX_WIDTH > height/MAX_HEIGHT) {
    if (width > MAX_WIDTH) {
        height *= MAX_WIDTH / width;
        width = MAX_WIDTH;
    }
} else {
    if (height > MAX_HEIGHT) {
        width *= MAX_HEIGHT / height;
        height = MAX_HEIGHT;
    }
}

5. Configure a Canvas for the selected image dimensions

If the canvas is on the page, you can just grab it:
var canvas = $('#PhotoEdit')[0];
Otherwise, you can use an offscreen canvas:
var canvas = document.createElement('canvas');
And then size it accordingly:
canvas.width = width;
canvas.height = height;

6. Draw the image to the Canvas, rotating and scaling as appropriate

In order to draw the image to a Canvas, first we have to load it in an <img />, and a convenient way is to create a URL to assign as the img.src from the selected file (using part of the File API):
var img = new Image();
var url = window.URL ? window.URL : window.webkitURL;
img.src = url.createObjectURL(imageFile);
img.onload = function(e) {
    url.revokeObjectURL(this.src);
    ...
};
Then inside the onload handler, we can draw the image to the canvas. (In practice, I put most of the logic we're discussing inside the onload handler.) A transformation matrix on the context handles rotating the image (if needed) and shifting it back into the viewable area.
var ctx = canvas.getContext("2d");
ctx.fillStyle = 'white';
ctx.fillRect(0, 0, 700, 600);
if(transform === 'left') {
    ctx.setTransform(0, -1, 1, 0, 0, height);
    ctx.drawImage(img, 0, 0, height, width);
} else if(transform === 'right') {
    ctx.setTransform(0, 1, -1, 0, width, 0);
    ctx.drawImage(img, 0, 0, height, width);
} else if(transform === 'flip') {
    ctx.setTransform(1, 0, 0, -1, 0, height);
    ctx.drawImage(img, 0, 0, width, height);
} else {
    ctx.setTransform(1, 0, 0, 1, 0, 0);
    ctx.drawImage(img, 0, 0, width, height);
}
ctx.setTransform(1, 0, 0, 1, 0, 0);

7. Access and manipulate the pixel data for the Canvas

The getImageData method returns an array of pixel data, with one byte each for red, green, blue, and alpha. This example applies a "green screen" effect, setting pixels to transparent (alpha = 0) if they are mainly green.
var pixels = ctx.getImageData(0, 0, canvas.width, canvas.height);
var r, g, b, i;
for (var py = 0; py < pixels.height; py += 1) {
    for (var px = 0; px < pixels.width; px += 1) {
        i = (py*pixels.width + px)*4;
        r = pixels.data[i];
        g = pixels.data[i+1];
        b = pixels.data[i+2];
        if(g > 100 && g > r*1.35 && g > b*1.6) pixels.data[i+3] = 0;
    }
}
There are of course other possibilities for filtering the image.

8. Write the pixel data back to the Canvas

Then we use putImageData to write the array of pixel data back to the canvas.
ctx.putImageData(pixels, 0, 0);

9. Export the image on the Canvas as an image file

Finally, the whole content of the canvas can be exported as an image. There may be a way to do this through the browser (in Firefox, for instance, right-clicking on any Canvas lets you view it as an image), but there is a canvas API call as well. Every browser supports saving the image as a PNG, and some may support other formats:
var data = canvas.toDataURL('image/png');
The resulting data URL looks like this:
data:image/png;base64,...
Where the "..." is the base 64 encoded version of the binary image file. So you can, for instance, upload the data URL to a server as a form field, and the server can strip off the data:image/png;base64, prefix and base 64 decode the rest and save it to a file like mycanvas.png.

Summary

So there you have it -- a Web page that takes a photograph, scales and orients it as appropriate, processes the image as desired, and then displays it to the user and/or uploads it to a server.
Unfortunately, there is not yet a way to present a "Save As" dialog to the user to save the file directly to their local disk -- the FileSystem and FileWriter APIs are not widely supported across browsers. But you can of course upload the image to the server and then redirect the user to a server URL to save it...

Consolidated Sample Code

Here's a single page with a working example using the code we've gone through here.

You can also use this sample image to see the green screen replacement effect.

Platform Support

Bold entries work fully:

Desktop Browsers:
  • Firefox 18
  • Safari 6
  • Chrome 25
  • IE 10
  • IE 9 (does not support getting selected files from file input)

Mobile Browsers:
  • iOS 6
  • iOS 5 (file input doesn't work)
  • BlackBerry 10
  • Android 4.1 (built-in browser)
  • Android 4.1 with Chrome from Play store
  • Android 2 (native browser; does not take photo)
  • Windows Phone 8 (file input doesn't work)
  • Surface RT (doesn't work)

Saturday, March 9, 2013

Configuring the Network used by Mac OS X Internet Connection Sharing

Changing the Subnet

On Mac OS X, Internet Connection Sharing by default hands out IPs on the 192.168.2.x subnet.  Normally this is fine, but sometimes you might want to change it (because you're at a hotel that uses 192.168.2.x or whatever).

The network to use is configured in /etc/bootpd.plist, but that file is overwritten every time you start Internet Connection Sharing (and deleted when you stop it).

The easiest way to change the network is to shut down Internet Connection Sharing, close System Preferences entirely (since it keeps settings in memory and overwrites configuration files), and run the following Terminal command (replacing the last parameter with the network you want it to use):

sudo defaults write \
   /Library/Preferences/SystemConfiguration/com.apple.nat \
   NAT -dict-add SharingNetworkNumberStart 192.168.3.0

Then you can go back into System Preferences and start Internet Connection Sharing again.  If you look at /etc/bootpd.plist you should see the new network listed.


Static IPs for Connected Devices

If you want to assign static IPs to any devices, you can create the file /etc/bootptab like this:

# Bootptab file
# Section 1 -- ignored
%%
# Section 2 -- used
# Hardware types: 1=Ethernet, 6=Wireless
#                 See http://www.ietf.org/rfc/rfc1700.txt
#
# machine entries have the following format:
#
# hostname         hwtype     hwaddr         ipaddr   bootfile
some-ethernet-machine 1 c4:2c:03:3a:7f:65 192.168.3.200 boot
a-wireless-machine    6 60:33:4b:27:6c:62 192.168.3.201 boot
another-ethernet      1 00:0d:93:72:e7:96 192.168.3.202 boot

You can man bootptab, though it isn't very helpful, and man bootpd has only a little more.  Bottom line, there must be a line with %% and the static IP definitions come after that.  The first parameter is the machine name, the second is the hardware type, the third is the Ethernet address of the relevant network interface of the machine, the fourth is the IP to assign, and the last is a boot file (which we aren't using, but it might be necessary to have something there).

Make sure to restart Internet Connection Sharing after editing this file.

Tuesday, February 26, 2013

Importing Data via Custom File Types in iOS

Introduction

I recently came across a situation where I needed the ability to share data between users of the same iOS application.  Unfortunately, these users can not share an iCloud account and there is no server side components to this application.  It is a standalone app.  Services like DropBox were not really an option either, as the devices are owned by an enterprise and will not have any service like that deployed onto them.

One solution is importing data via custom file types. Files can be sent to a device numerous ways (e.g. email, drop box, etc.), but for this post we will assume email. iOS makes it very simple to define a custom file type that your app can handle.  This way, when the device receives an email with that type as an attachment, the user can select our app, giving it access to the underlying file. This post will walk through creating a simple app to access the contents of a text file received via email.

Create the Project

Lets start by creating a new project in XCode.  I created a single view iPad application and called it "CustomUTIHandler".  I always use ARC.  You can choose to use storyboards or not, as the UI in this app will do nothing more than display the contents of our file.  The resulting project for this post can be found at https://github.com/stevenpsmith/CustomFileHandler.

Define the File Type

The next thing is to define a file type extension that our application will support.  This is also known as a Uniform Type Identifier (UTI).  For our purpose, we will use a filetype of 'csm' (for Chariot Solutions Mobile).  The contents of this file will be some string.  It could really be almost anything, but for simplicity we will use a string.  For a list of Apple defined UTIs, look here: http://developer.apple.com/library/ios/#documentation/Miscellaneous/Reference/UTIRef/Articles/System-DeclaredUniformTypeIdentifiers.html#//apple_ref/doc/uid/TP40009259-SW1.

Once we know what our custom file type extension is, we can add support for it to the project by modifying the Info.plist file for the project.  We are going to be the owner/exporter of the new file type, so we add the "Exported Type UTIs" key to the Info.plist file.  The default definition that gets created looks like this:


Based on the bundle key definitions found here: https://developer.apple.com/library/mac/#documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html, we can fill in the required values.  Since our example only has one file type that it is defining and the underlying data is text, we can add the system defined identifier for text to the "Conforms to UITs" item:

public.text

Next we need to add the identifier for our custom UTI.  Apple recommends using the reverse DNS format in order to ensure our new UTI is unique.  I will be using the following:

com.chariotsolutions.customUTIHandler.csm

Finally, we want to identify the file extension that our app will be handling.  This is defined under the
"Equivalent Types" node.  Add a node under the equivalent types with a name of "public.filename-extension" and a value of "csm".  You can optionally add a "Description" element under the Item for the exported type.  My Info.plist now looks like this:



Support our new File Type

In order for our app to be recognized by the OS as supporting the newly defined file type, we need to add a "Document types" declaration to our Info.plist file.  Doing so gives us the following in our Info.plist file:



The possible keys for the CFBundleDocumentTypes property can be found here: http://developer.apple.com/library/ios/#documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html#//apple_ref/doc/uid/TP40009249-101685-TPXREF107.  We can add anything for the "Document Type Name" value.  The handler rank will affect the order that Launch Services uses to present apps that handle this file type.  Since our app is really the owner of this file type, we will use that as the handler rank.  Now we need to add the "Document Content Type UITs" that our app will handle.  In this case, we are only handling one type (as defined in the export key above): 

com.chariotsolutions.customUTIHandler.csm

Just for fun, we will also give our file type an image icon (included in the project).  The specs for the image are defined in the keys document referenced above (64x64 in this case).  So our final Info.plist looks like the following:



Now all we have to do is create a text file with a ".csm" extension and email it to our device.  Once there, long press the resulting icon and we should see our app listed as one of the "Open in" apps.



Once we select our app, it will launch and pass the file URL to this app delegate method:

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation

So, adding a simple NSLog statement to the method and we can display the contents of our text file:

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    if (url){
        NSString *str = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
        NSLog(@"The file contained: %@",str);
    }
    return YES;

}

Important Safety Tip

The files that are being opened by the application are actually copied into a directory called "Inbox" within the apps Documents directory.  My file was called "This is a test.csm" (great name right, what am I a windows user?).  As shown in the image below, there are copies for each time I imported it on my device.



 These files should be cleaned up as required by your app.

Summary

So by adding some configuration to our apps Info.plist file and implementing a delegate method, we can handle any uniform types within our app.  The source code for this project can be found at https://github.com/stevenpsmith/CustomFileHandler.  I added some code so that it will display the contents of the file on the screen....very exciting.

Added Bonus

Since we are declaring to launch services that our app handles "csm" files, drop your custom file type into drop box.  Assuming you have the drop box app on you device, find the document in the app and try to open it.  You should get "Unable to view file".  But if you tap the icon in the upper right of the screen, you should get a list of apps that can open that file....including our app.



It's like magic :)  You see those other apps listed because we defined our UTI as text type, and those apps will handle text documents.

Monday, February 18, 2013

Single-Page Web Applications - Quizzo at ETE

Chariot's Ken Rimple and VMware's David Turanski are working on a presentation for ETE that focuses on modern Single-Page Web Applications, hosted on a Spring platform.  This talk helps to show current Spring developers how to pull together some of the newer Spring and client-side technologies to build dynamic, modern Javascript applications in the enterprise.

They are working on the project in the open at GitHub, and will be using technologies such as AngularJS, Yeoman, Spring MVC,  and Spring Data. Follow Ken's blog entries about the project at http://www.rimple.com/tech/tag/quizzo-ete-2013.

Monday, February 11, 2013

A Guiding "Light" to Learning Clojure


I've recently started learning Clojure after a little encouragement from co-worker of mine.  I've been developing in Java for a while now and spent the last 1 1/2 years with Groovy.  The Groovy experience was great and the use of closures certainly "whet my appetite" for functional programming.

Ok, for me, when I learn something new I need to have tools that are not distracting so that I'm focused on the material.  In this case, I wanted to take a little time to view the tools landscape for Clojure while trying not get carried away.  After doing some research, I narrowed it down to a few options.  First, I'm a long time Intellij IDEA user so I could just download the La Clojure plugin (I did) to get a REPL, syntax highlighting, source code navigation, etc.  This is fine but it's a bit of an overkill for just getting started with Clojure.  Intellij IDEA is a great IDE however, I wanted to stay open-minded to what else is out there.

Next, I tried configuring Vim for Clojure after seeing a co-worker work on a Clojure project with Vim.  The great thing about Vim is it's lightweight editor that's extremely fast.  If you're proficient Vim user then you get things done quickly.  If you're a basic Vim user (like me) then there's a learning curve.  Regardless, I thought I'd give it a try by downloading and configuring VimClojure.  There is a way to configure Vim to dynamically work with Clojure to provide documentation lookup, REPL running in a Vim buffer, etc.  You have to build a Nailgun interface by downloading a client program and so on.  I eventually got a dynamic Clojure development tool in Vim but I found that I was focusing more on learning this Vim/VimClojure/Nailgun environment than on Clojure!  So, I reluctantly moved on.

I thought about jumping into the deep end with Emacs, which is the choice for most Clojure developers but I'm not an Emacs user (please don't hold it against me).  I didn't want to get involved in a big learning curve again.  I was feeling a bit like, "Hi, my name is Rod and I'm a tools-aholic"...this was starting to consume a lot of my time and taking away from what I set out to do...learn Clojure.

Well the tools story continues, in November 2012 I attended Clojure-conj in Raleigh, NC.  That's where I saw Chris Granger present LightTable for the first time.  I was impressed with the presentation and thought to myself that I need to take a closer look at LightTable some time (not immediately but some time).  I was trying to resist the temptation and I did for a little while.  Well, three months later I "fell off the wagon" and took a "drink" of LightTable and I'm glad I did. :)


This is not a review of LightTable nor a "HowTo", I think of it more as getting the word out about a new and different kind of developer tool.  The UI is simple with a very modern look and color scheme.  My first impression was this could be a next generation Vim editor.  I found myself wanting to use the tool.  The intuitive UI is easy to use and it feels natural like you're closer to the terminal.

Instarepl


What appeals to me the most happens to be one of LightTable's guiding principles "Discover by doing".  The REPL (Read-Eval-Print-Loop) provides an interactive session to write and execute code.  This isn't something new to the "Lispers" since REPL has been around a long time.  However, the REPL (Instarepl) in LightTable takes things one step further.

Here's your typical REPL TryClojure.  Simply write some Clojure code and get the result.


Here's the same basic example in LightTable's Instarepl.


Ok, that's cool, you see what you type on the left, which is mirrored on the right with the result.  However, it's not much different than the Clojure REPL.  But wait, it gets better.


Here's a simple function that I pulled from Chris' blog post.  You can see that the right side of Instarepl is just showing the function as it was written.  However, things change after writing code to call the function.


The right side shows the data flowing through the function.  It is pretty awesome to try various inputs and watch how the values flow through the code.  I'm not sure if Chris has officially coined a term for this feature but I've read it being referenced as "live evaluation" or "real-time evaluation and visualization".  This is what makes me want to use LightTable even if nothing else works. :)  I have to wonder how this type of code "visualization" impacts TDD (Test-Driven Development).  It would be interesting to explore LightTable's impact on TDD...sounds like a good blog post. :)

There are many more features to LightTable and I highly recommend that you watch the video on the LightTable web page and see Chris drive the IDE.  The version I'm currently using is 0.2.7 and some of the features that he demonstrates in the video I wasn't able to get working.  However, there is a new 0.3.0 version coming out Feb.-Mar. timeframe and (according to Chris) this version is about "crafting an editing experience so a lot of things will be much, much better"...can't wait, I have seen the Light!