4 posts / 0 new
Last post
sasij
sasij's picture
Offline
Mobile Conjurer
Joined: 2 Apr 2011
Posts:
Camera Class

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;
}

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
ishanmlvy
ishanmlvy's picture
Offline
Mobile Conjurer
Joined: 7 Feb 2011
Posts:

Hi,

I also wrote a header/source combo for using camera api, the problem i faced with my code was that, the camera viewfinder is only half the screen height.
Used your code , am facing the same problem, is this a restriction or i am missing something.

Regards,
Ishan

Fredrik Eldh
Fredrik's picture
Offline
Mobile Sorcerer
Joined: 16 Feb 2009
Posts:

sasij, you can try your code on our emulator. It's not connected to a real camera, and there's only one format, but all the functions work.

Diego Velazquez
diegolv's picture
Offline
Mobile Wizard
Joined: 4 Oct 2010
Posts:

Hi Fredrik,

do you know when this API will be available in Android?

Best regards,

Diego