Well I spent last night working on this so I figured I'd put it out for anyone to look at. I cannot verify that it works because I have an android and found out my camera doesn't show up. But it may help someone who is on a platform that supports the camera features. I believe the main thing left to do is take the image data placeholder and save it.
#include
#define alert(x) maMessageBox("Alert!", x);
struct ImageFormat
{
int m_format_number;
int m_width;
int m_height;
};
struct CameraInfo
{
bool m_ispresent;
int m_formats;
};
class Camera
{
public:
CameraInfo m_info;
ImageFormat m_current_format;
bool m_initialized;
ImageFormat GetFormatInfo(int format_index);
bool SetCurrentFormat(int format_index);
bool SnapFrame();
bool Initialize();
void Close();
Camera();
~Camera();
};
Camera::Camera()
{
m_initialized = false;
memset(&m_info, 0, sizeof(m_info));
memset(&m_current_format, 0, sizeof(m_current_format));
}
Camera::~Camera()
{
}
ImageFormat Camera::GetFormatInfo(int format_index)
{
ImageFormat ret;
if(format_index < 0 || format_index > m_info.m_formats)
{
alert("Invalid format!");
ret.m_width = 0;
ret.m_height = 0;
return ret;
}
MA_CAMERA_FORMAT *lpfmt;
maCameraFormat(format_index, lpfmt);
ret.m_width = lpfmt->width;
ret.m_height = lpfmt->height;
return ret;
}
bool Camera::SetCurrentFormat(int format_index)
{
if(format_index < 0 || format_index > m_info.m_formats)
{
alert("Invalid format!");
return false;
}
MA_CAMERA_FORMAT *lpfmt;
maCameraFormat(format_index, lpfmt);
m_current_format.m_width = lpfmt->width;
m_current_format.m_height = lpfmt->height;
m_current_format.m_format_number = format_index;
return true;
}
bool Camera::Initialize()
{
int formats = maCameraFormatNumber();
if(formats <= 0)
{
alert("No camera or unknown format!");
return false;
}
//default to the first known format
SetCurrentFormat(1);
m_initialized = true;
int ret = maCameraStart();
if(ret!=1)
{
alert("View Finder couldn't start or is already running.");
return false;
}
return true;
}
void Camera::Close()
{
m_initialized = false;
maCameraStop();
}
bool Camera::SnapFrame()
{
if(m_initialized)
{
MAHandle placeholder = maCreatePlaceholder();
int ret = maCameraSnapshot(m_current_format.m_format_number, placeholder);
if(ret!=0)
{
alert("Error taking picture!");
return false;
}
}
//Todo: Save Image to file on phone memory or memory card
return true;
}
extern "C" int MAMain()
{
Camera *lpcamera = new Camera();
lpcamera->SnapFrame();
for(;;)
{
MAEvent e;
maWait(0);
while(maGetEvent(&e))
{
if(e.type == EVENT_TYPE_CLOSE)
{
maExit(0);
}
else if(e.type == EVENT_TYPE_KEY_PRESSED)
{
if(e.key == MAK_0)
{
maExit(0);
}
if(e.key == MAK_S)
{
if(!lpcamera->m_initialized)
lpcamera->Initialize();
else
lpcamera->Close();
}
}
}
}
return 0;
}