Simple BREW Camera Application - 5 / 6 -
Utilize Camera Class
SimpleCamera Class
The following is the code of an application using the Camera class.
Header File of SimpleCamera Class
- SimpleCamera.hpp - #ifndef __SIMPLECAMERA_HPP #define __SIMPLECAMERA_HPP #include <SophiaFramework.hpp> #include "SimpleCamera.bid" #include "Camera.hpp" SFMTYPEDEFCLASS(SimpleCamera) class SimpleCamera : public SFCApplication { SFMSEALCOPY(SimpleCamera) public: static SFCInvokerPtr Factory(Void); private: Camera _camera; enum AppStatus { STATUS_NONE = 0, STATUS_PREVIEW, STATUS_SNAP }; AppStatus _status; SInt16 _brightnessMin; // min of brightness SInt16 _brightnessMax; // max of brightness SInt16 _brightness; // current brightness SInt16 _zoomMin; // min of zoom value SInt16 _zoomMax; // max of zoom value SInt16 _zoom; // current zoom value SimpleCamera(Void); virtual ~SimpleCamera(Void); virtual Bool Invoke(SFXEventConstRef event); Void OnAppStart(AEEAppStartPtr environment); Void OnAppStop(BoolPtr quitable); Bool OnKey(UInt16 key); // Callback function for camera events static Void CameraCallback(Camera::StatusEnum status, SFCError error, VoidPtr reference); // Handler for camera events Void OnSimpleCamera(Camera::StatusEnum status, SFCError error); // For displaying error messages Void PrintError(SFXWideStringConstRef msg); // Start to preview Void PreviewCamera(Void); // Process and display photo image taken in snapshot mode Void DisplayImage(Void); // Change brightness Void ChangeBrightness(Bool isBrighter); // Change zoom value Void ChangeZoom(Bool isZoomIn); }; #endif // __SIMPLECAMERA_HPP //
Event Handler
Register Event Handler
// Handler on staring application Void SimpleCamera::OnAppStart(AEEAppStartPtr environment) { ... // here is the code when this application starts // Initialization of camera if (_camera.Initialize( SFXGraphics::GetInstance()->GetDeviceRectangle(), CameraCallback, this) != SFERR_NO_ERROR) { PrintError(SFXWideString("Failed to Initialize Camera")); return; } ... }
Event Handler
Void SimpleCamera::CameraCallback(Camera::StatusEnum status, SFCError error, VoidPtr reference) { SimpleCameraPtr p = reinterpret_cast<SimpleCameraPtr>(reference); p->OnSimpleCamera(status, error); } Void SimpleCamera::OnSimpleCamera(Camera::StatusEnum status, SFCError error) { if (error != SFERR_NO_ERROR) { // Error occurred _camera.camTerminate(); _status = STATUS_NONE; PrintError(SFXWideString("problem occurred in camera")); return; } if (status == Camera::STATUS_ENCODE) { // Ready to get bitmap _status = STATUS_SNAP; DisplayImage(); } }
* As event handlers must be static functions, calling the funtion is done through two steps.
The Camera class will call the registered callback function after handling its events.
Camera::STATUS_ENCODE is the appropriate status to get a photo image taken in the Snapshot Mode.
The SimpleCamera class will do nothing in the Preview Mode, but will call DisplayImage() when a picture is taken in the Snapshot Mode.
Get Photo Image
Photo images can be retrieved by calling the GetBitmap() member function of the Camera class.
// display photo image taken in the snapshot mode Void SimpleCamera::DisplayImage(Void) { // make photo image like a developed film SFXGraphicsPtr pg(SFXGraphics::GetInstance()); SFBBitmapSmp displayBmp(pg->GetDestination()); displayBmp->BltIn(pg->GetDeviceRectangle(), _camera.GetBitmap(), SFXGrid(0, 0), AEE_RO_NOT); pg->Update(); }
In this example, the photo images are produced as developed film and displayed. This can be done by setting AEE_RO_NOT for raster operation when getting a bitmap of the screen and copying a bitmap taken by the camera.