Anatomy of a Camera App for iOS and Android

Posted by on in Blogs
I've been watching some other blog posts about building Smartphone camera apps for Android, seeing all of the UI design work required and the lines of code needed.  While I always want to learn more about Smartphone APIs for Android and iOS, I also care a lot about getting applications built quickly with as few lines of code as possible that work with Android and iOS.  On the Embarcadero RAD Studio XE5 DocWiki and in a YouTube video we show developers how to build a Smartphone Camera App that lets you take pictures, preview them, save them to your camera roll and also share the image with other apps. Start with a blank app template project, add 6components (TToolBar, TImage, TActionList and 3 TButtons), set a few property values int he Object Inspector, choose 3 predefined actions (TTakePhotoFromLibraryAction, TTakePhotoFromCameraAction and TShowShareSheetAction), associate the 3 actions to each of the three buttons, connect the action execution for each button to create event handlers for each action and finally write 3 lines of code (one per action event).  Select Target Platform for iOS and Android and choose Build and you have finished camera apps for iOS and Android.

The 3 lines of code for the three action event handlers:

    procedure TForm1.TakePhotoFromCameraAction1DidFinishTaking(Image: TBitmap);
    begin
    Image1.Bitmap.Assign(Image);
    end;

    procedure TForm1.TakePhotoFromLibraryAction1DidFinishTaking(Image: TBitmap);
    begin
    Image1.Bitmap.Assign(Image);
    end;

    procedure TForm1.ShowShareSheetAction1BeforeExecute(Sender: TObject);
    begin
    ShowShareSheetAction1.Bitmap.Assign(Image1.Bitmap);
    end;


The Mobile Tutorial, "Taking and Sharing a Picture (iOS and Android)" can be found at http://docwiki.embarcadero.com/RADStudio/XE5/en/Mobile_Tutorial:_Taking_and_Sharing_a_Picture_(iOS_and_Android)

The Mobile Share Sheet with Delphi XE5 video can be found at http://www.youtube.com/watch?v=JS6o4RgDX1g

A couple of additional notes about Camera apps and sharing images:

  • iOS Share Sheet functionality first appeared in version 5 and was enhanced by Apple in iOS 6

  • Android ShareActionProvider is available starting with Android API Level 14 and higher

  • TShowShareSheetAction is part of the FMX MediaLibrary - http://docwiki.embarcadero.com/Libraries/XE5/en/FMX.MediaLibrary.Actions.TShowShareSheetAction

  • Make sure to check Project | Options | Uses Permissions - for Android make sure the Camera setting is set to true (by default we set this True in debug and release configurations, you can change all permission options for the defaults you want for debug and release configurations).


You can get started now by downloading the RAD Studio XE5 trial at http://www.embarcadero.com/products/rad-studio/downloads


About
Gold User, Rank: 1, Points: 2466
David Intersimone (known to many as David I.) is a passionate and innovative software industry veteran-often referred to as a developer icon-who extols and educates the world on Embarcadero developer tools. He shares his visions as an active member of the industry speaking circuit and is tapped as an expert source by the media. He is a long-standing champion of architects, developers and database professionals and works to ensure that their needs are folded into Embarcadero's strategic product plans. David holds a bachelor's degree in computer science from California Polytechnic State University at San Luis Obispo, California.

Comments

  • Guest
    Jack de Veer Tuesday, 17 September 2013

    How can you explain that lots of people including me, are not able to use the camera under Android. This has been an issue in several bèta's and is still not solved.?

    P.S. Using the debugger trying to find out wat is wrong is not possible due to the timeouts that occur when debugging is enabled in an Android application

  • Guest
    Stefano Moratto Tuesday, 17 September 2013

    David I, thanks for your efforts to improve Delphi (I'm a delphi user from 1)
    I have read on some blogs's posts the Android emulator being very slow dues to it emulates the ARM architecture onx x86 ( of course this is a fault of the Android SDK).
    I found and interesting article about a x86 Android emulator:

    http://www.drdobbs.com/architecture-and-design/android-on-x86-understanding-android-dev/240161434?elq=a10350cf3238440a85374ffb039d64d6.

    However I think it is not usable until you provide a ARM/Android/X86 compiler.

    Best Regards,
    Stefano Moratto

  • Guest
    Eric Fleming Bonilha Tuesday, 17 September 2013

    Excellent David I.
    I have read that blog post as well and I was thinking about the very same thing... the amount of work required to make a simple camera app in that other framework is just absurd... I would like to answer to his blog post but I would not be able to reply to his attacks with proper words and articulations because I´m not a native english speaker.
    I was time for someone to reply to his posts because he just publishes bullshit

  • Guest
    Larry Hengen Wednesday, 18 September 2013

    The number of lines of code to perform any given action is not the only measure of a development tool, especially when that measure is taken in a specific situation for which there may be an inordinate amount of framework support. Also the IDE support such as refactoring, code completion,code insight, templating etc, may make the time spent to produce the additional lines of code negligible. You are also comparing apples to oranges when dealing with one tool that accesses the native platform APIs and is thus constrained by the way those APIs work, to another framework that uses it's own UI APIs and only interfaces with the native APIs when necessary. There are trade offs to both approaches.

    Not linking to the blog post(s) in question does not provide a complete picture for readers of this post, and combined with the content, leaves it open to interpretation as to whether this post is simply naive, or purposely misleading. Never underestimate the critical thinking abilities of developers....

  • Guest
    David Intersimone Wednesday, 18 September 2013

    Jack - "not able to use the camera under Android" - there may have been some issues in some of the field tests and on a few specific devices, but we have tested on a bunch of Android devices up and into the release version with success. What device did you test on? I have successfully run the ShareSheet snippet and my own camera demo apps on my Samsung Galaxy S4 and my Nexus 7 (2012 version) tablet.

  • Guest
    David Intersimone Wednesday, 18 September 2013

    Larry - we are focused both on programming and also on programmer productivity. Using more components and standard actions will help everyone be more productive on iOS and Android. Of course all developers can choose to take complete control in code.

    This blog post was not meant to be a number of lines of code comparison and more of a productivity and XE5 feature highlight. The focus of XE5 is same code/components on iOS and Android.

    Everyone can still also use the direct APIs on each platform. Our technology partner TMS Software provides iOS API specific components for Delphi iOS if developers want to go direct for the iOS platform - http://www.tmssoftware.com/site/tmsicl.asp.

    The DeviceInfo mobile code snippet shows how you can also go direct to the device APIs via Objective-C (iOS) and Java (Android) if you want to or need to. Developers are not blocked from anything on all platforms and devices that we provide.

    You can find the DeviceInfo snippet for iOS/Android at http://sourceforge.net/p/radstudiodemos/code/HEAD/tree/branches/RadStudio_XE5/MobileCodeSnippets/DeviceInfo/

    Source code is at http://sourceforge.net/p/radstudiodemos/code/HEAD/tree/branches/RadStudio_XE5/MobileCodeSnippets/DeviceInfo/uMain.pas

    iOS using the OCClass:

    procedure TDeviceInfoForm.btnGetDeviceInfoClick(Sender: TObject);
    var
    Device : UIDevice;
    begin
    Device := TUIDevice.Wrap(TUIDevice.OCClass.currentDevice);
    lbOSName.Text := Format('OS Name: %s', [Device.systemName.UTF8String]);
    lbOSVersion.Text := Format('OS Version: %s', [Device.systemVersion.UTF8String]);
    lbDeviceType.Text := Format('Device Type: %s', [Device.model.UTF8String]);
    end;

    Android using the Java class:

    procedure TDeviceInfoForm.btnGetDeviceInfoClick(Sender: TObject);
    var
    codename: string;
    version: TAndroidVersion;
    begin
    codename := 'Unknown';
    version := TAndroidVersion(TJBuild_VERSION.JavaClass.SDK_INT);
    lbDeviceType.Text := Format('Device Type: %s', [JStringToString(TJBuild.JavaClass.MODEL)]);
    ...
    lbOSName.Text := Format('OS Name: %s', [codename]);
    lbOSVersion.Text := Format('OS Version: %s', [JStringToString(TJBuild_VERSION.JavaClass.RELEASE)]);
    end;

  • Guest
    David Intersimone Wednesday, 18 September 2013

    To answer some questions about specific device support, our team has put together a starting document on our Embarcadero DocWiki that has some of the details about chip and device support. You can find this document online at http://docwiki.embarcadero.com/RADStudio/XE5/en/Android_Devices_Supported_for_Application_Development#Specific_Android_Devices_that_We_Have_Tested_and_Verified

    This document will be continually updated as we do updates, additional testing and also as new devices appear and old devices disappear :)

  • Guest
    Jack de Veer Wednesday, 18 September 2013

    David: Tested the camera on a 2 months old HP 7 Slate with Android 4.2.1 on it.
    On my release XE5 version the TCameraComponent.OnSampleBufferReady event is not fired rendering the TCamera component useless. The XE5 version was downloaded from the link in the maintenance portal.

  • Guest
    Jim McKeeth Tuesday, 12 November 2013

    Jack:

    I think there was a bug in the TCameraComponent that was fixed in Update 1. Check it out again and let me know!

    -Jim

  • Please login first in order for you to submit comments
  • Page :
  • 1

Check out more tips and tricks in this development video: