Wiley.Games.on.Symbian.OS.A.Handbook.for.Mobile.Development.Apr.2008 (779888), страница 43
Текст из файла (страница 43)
In the example below we will be using CCamera::StartViewFinderBitmapsL().The client application needs to inherit from the interface class MCameraObserver in order to actively interact with the camera API. Thismixin class provides five pure virtual functions that must be implemented:• void ReserveComplete(): handles the completion result fromcamera reservation• void PowerOnComplete(): indicates whether camera power-oncompleted successfully• void ViewFinderFrameReady(): receives the data from the camera viewfinder bitmap• void ImageReady(): handles the completion event raised by imagecapture• void FrameBufferReady(): handles the completion event raisedby video capture.Having discussed the basic set-up we can now proceed with our specific example.
In its header file, ColorTrackerView.h, the followingcan be seen:• inclusion of ecam.h for the camera APIs and FBS.h for bitmapmanipulation (also requires linking against fbscli.lib in theproject’s MMP file)• inheritance from MCameraObserver• declaration of the tracking function TrackColor()• declaration of the iCamera and iViewFinderBitmap membervariables (the latter is a bitmap holding individual frames from thecamera) and a Boolean member variable (iAllowDraw) to indicatethe availability of a camera frame from the viewfinder.#include <ecam.h>#include <FBS.h>// Camera API// Fonts and Bitmap APIclass CColorTrackerAppView : public CCoeControl, MCameraObserver184EXPLOITING THE PHONE HARDWARE{public:...
// Construction, destruction omitted for clarityprivate:// Inherited from MCameraObservervoid ReserveComplete(TInt);void PowerOnComplete(TInt);void ViewFinderFrameReady(CFbsBitmap&);void ImageReady(CFbsBitmap*, HBufC8*, TInt);void FrameBufferReady(MFrameBuffer*, TInt);// The function to track colors in our example// @arg1: The color to track// @arg2: Reference to the application's graphics contextvoid TrackColor(TRgb&, CWindowGc&);private:CCamera* iCamera;CFbsBitmap* iViewFinderBitmap;TBool iAllowDraw;};On instantiation, as part of the second phase construction of a CColorTrackerAppView object, the ConstructL() method constructs theiCamera object and reserves it, and constructs the viewfinder bitmapiViewFinderBitmap.void CColorTrackerAppView::ConstructL(const TRect& aRect){...// Omitted for clarity// Instantiate iCamera:// arg1: reference to the inherited MCameraObserver interface// arg2: the index of the device camera to use - 0 for the// back camera.iCamera = CCamera::NewL(*this, 0);// Check if there is a cameraif( iCamera->CamerasAvailable() > 0 ){iCamera->Reserve();// Construct the bitmap to hold the frames captured by the cameraiViewFinder = new(ELeave) CFbsBitmap();}else{User::Leave(KErrNotFound);}}As we previously discussed CCamera::Reserve(), CCamera::PowerOn(), and CCamera::StartViewFinderBitmapsL() are allasynchronous functions, and each must only be called when the preceding function in the sequence has completed, because dependencies exist.For example, StartViewFinderBitmapsL() is dependent on thecompletion of PowerOn(), which is itself dependent on the completionCAMERA185of Reserve().
If StartViewFinderBitmapsL() is called before thePowerOn() method completes, it will panic the application.To manage this sequence of calls, we implement the callback functionsof the iMCameraObserver to provide the framework to monitor thecompletion of each camera operation. Therefore, we add the followingfunction implementations to ColorTrackerView.cpp.void CColorTrackerAppView::ReserveComplete(TInt aErr){// Check the camera is reserved successfully// Error handling is omitted for clarifyif( aErr==KErrNone)// Turn the camera oniCamera->PowerOn(); // Asynchronous completion}void CColorTrackerAppView::PowerOnComplete(TInt aErr){if (aErr==KErrNone){// Set the size of the rectangle on screen to use// for the view finderTSize size(240,180);// Start capturing frames – asynchronous completionTRAPD( result, iCamera->StartViewFinderBitmapsL(size) );if( result != KErrNone){// Error handling omitted for clarity}}}void CColorTrackerAppView::ViewFinderFrameReady(CFbsBitmap& aFrame){// There is one frame ready to display,iAllowDraw = ETrue;// duplicate it,iViewFinder->Duplicate(aFrame.Handle());// and render it on screen.DrawNow();}void CColorTrackerAppView::ImageReady(CFbsBitmap*, HBufC8*, TInt){// No need for implementation.}void CColorTrackerAppView::FrameBufferReady(MFrameBuffer*, TInt){// No need for implementation.}void CColorTrackerAppView::Draw(const TRect& aRect) const{...// Omitted for clarity// Allow bitmap drawing and manipulationif(iAllowDraw){// The color to track (red)TRgb colorToTrack(255,0,0);// Draw the frame on screen186EXPLOITING THE PHONE HARDWAREgc.BitBlt(TPoint(0,0), iViewFinder);// Track the red colorTrackColor(colorToTrack, gc);}}When the camera reservation is finished, an event is raised that resultsin a call to MCameraObserver::ReserveComplete() to notify it ofcompletion.
On checking that there is no error passed as a parameter to thecallback function, to confirm that reserving the camera was successful,PowerOn() is called from within the handler function, which startspowering up the camera asynchronously. If this completes successfullywe call StartViewFinderBitmapsL() from PowerOnComplete(),which starts loading the frames from the camera viewfinder.When the first bitmap frame is ready, the MCameraObserver::ViewFinderFrameReady() observer function is called by the framework, passing as a parameter that frame (aFrame), allowing it to becopied, or duplicated, into iViewFinderBitmap for later manipulation. The method sets iAllowDraw to ETrue to indicate that it ispossible now to start drawing bitmap frames to the screen as there is atleast one that has been successfully loaded.
DrawNow() in ViewFinderFrameReady() calls Draw() to perform the on-screen drawing.You should not call Draw() directly as there are several steps WSERVtakes before and after a call to Draw() which DrawNow() deals withinternally.The previous code illustrates the use of the camera on any Symbian OSsmartphone. To complete this example, the following function performsthe actual tracking of a color, red in our example. The function is calledfrom within Draw(), which will draw white squares on the screen inplace of the red pixels. Note that this is a very simple color trackingalgorithm, and we acknowledge that there are more sophisticated andaccurate solutions available.TrackColor() identifies a threshold for the color to track in aspherical shape surrounding it.
The function calculates the distancebetween the color to track, represented as the centre of the sphere, andthe color of an arbitrary pixel on screen in the RGB (0 ≤ R, G andB ≤ 255) space, as shown in Figure 6.5. Point C represents the centre ofthe color threshold. R is the radius of the sphere and r is the distancebetween B (which falls within the boundary) and C. Apparently point Ais outside the sphere boundary making L much beyond the length of R,and thus A is not considered for detection.If the distance between the color of an arbitrary screen pixel and thecolor to track (in the RGB space) is less than the predefined radius of thesurrounding sphere, 125 in our example, then the two colors match, andthat pixel is replaced with white.
Otherwise, the color is not recognizedCAMERA187GreenRCrBLBlueARedFigure 6.5 Spherical boundary color detectionand the functions continue checking other pixels. The following codeimplements this detection method.void CColorTrackerAppView::TrackColor(TRgb& aColor, CWindowGc& aGc){// Set the color of the dots to drawaGc.SetBrushColor( KRgbWhite );TUint r, g, b, distance;TRgb pixelColor;// Traversing the 240×180 viewfinder bitmap capturedfor(TUint x=0; x<240; x+=2){for(TUint y=0; y<180; y+=2){iViewFinder->GetPixel(pixelColor,TPoint(x,y));r = pixelColor.Red();g = pixelColor.Green();b = pixelColor.Blue();// Calculating the threshold as a sphere radius.