19 posts / 0 new
Last post
Diego Velazquez
diegolv's picture
Offline
Mobile Wizard
Joined: 4 Oct 2010
Posts:
Android: Camera

Hi,

I'm testing Camera on Android and I found some issues using MAUI.

1) maCameraFormatNumber gets the number of possible sizes, but there's no way to retrieve each format resolution.

2) maCameraFormat sets a new resolution. Why index must be >=0 and < maCameraFormatNumber since it simply adds width/height to userWidths and userHeights? In fact, there's no relation between format and those Arrays.

3) Android requires a valid resolution, so you call mPreview.getOptimalSize. Ok, what's the size? I need 1280x1024 jpeg pictures, so i'm planning to use RAW + resize + my own encoder. That's why I need pic size.

4) maCameraStart tests if mMoSyncCameraController.hasView(). If I call maCameraStop and maCameraStart again, preview is not shown. Is there a workaround?

Btw, maCameraStart calls mMoSyncCameraController.setPreview using cameraPreview. maCameraStops calls maWidgetDestroy(cameraScreen). hasView() is probably wrong isn't it?

5) Testing on Xperia X10 w/ GB 2.3.3, I get the curious scenario:
maCameraStart();
maCameraFormat index 0 to 640x480 (which is not valid);
maCameraSnaphhot w/ index 0;
it returns a 1920x1024 picture;
maCameraClose();
maCameraStart() (no preview, check item #4);
maCameraFormat index 0 to 640x480 (which is not valid);
maCameraSnaphhot w/ index 0;
it returns a 640x480 picture;
maCameraClose();

Regards,

Diego

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Sam Pickard
rival's picture
Online
Mobile Archmage
Joined: 19 Mar 2009
Posts:

Hi Diego, I'm going to pass these comments to the Android team.

Thanks

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

Hi Sam,

1) Someone asked but I didnt read the answer. Are you working for MoSync?
2) Regarding Camera: It would be nice having setJpegQuality method.

Regards,

Diego

Sam Pickard
rival's picture
Online
Mobile Archmage
Joined: 19 Mar 2009
Posts:

Hey Diego, this one passed me by. I don't work directly for MoSync (i.e. I'm not an employee of MoSync AB), but they do pay me to answer questions on the forum. I am on the MoSync email system and I do have direct access to the core developers. I'm also available to produce mobile apps for anyone ;-)

I'll have a look at the setJpegQuality for the Camera and see if it is on the list.

Thanks

Sam

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

Hi,

not fixed in 2.7

Regards,

Diego

Ali Sarrafi
ali.s's picture
Offline
MoSync User
Joined: 20 Nov 2010
Posts:

Hi diego,

1, 2 :Your question is very valid but there are some reasons for that implementation on android. The most important reason for that is backward compatibility with the old API. I am not saying that we are not thinking about changing the API. In fact we are working to add some new improved features to the camera API.

Regarding the rest of your questions I will try to reproduce the problem and look into the code and come back to you as soon as possible.

UPDATE: The start/stop problem with the preview is fixed and will be in the next nightly build.
Cheers
Ali

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

Tks,

I will try it!

Diego

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

Hi,

maCameraSnaphhot raises Panic 40001. Using 2011-10-31 build.
Tested on Morotola Defy (2.2) and Xoom (3.1)

Regards,

Diego

Ali Sarrafi
ali.s's picture
Offline
MoSync User
Joined: 20 Nov 2010
Posts:

Hi Diego,

I have tried a couple of times on some phones we have here and didn't get the panic. Can you please copy your block of code that calls camera here so I can try to reproduce your problem?

Cheers
Ali

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

 

#include <MAUtil/Moblet.h>
#include <MAUtil/PlaceholderPool.h>
#include <conprint.h>
#include <mavsprintf.h>
using namespace MAUtil;
/**
 * A Moblet is a high-level class that defines the
 * behaviour of a MoSync program.
 */
class MyMoblet : public Moblet
{
public:
	int x;
	/**
	 * Initialize the application in the constructor.
	 */
	MyMoblet()
	{
		printf("Press zero or back to exit\n");
		x = 0;
	}
	/**
	 * Called when a key is pressed.
	 */
	void keyPressEvent(int keyCode, int nativeCode)
	{
		if (MAK_BACK == keyCode || MAK_0 == keyCode)
		{
			// Call close to exit the application.
			close();
		}
		// Print the key character.
		printf("You typed: %c\n", keyCode);
	}
	/**
	 * Called when the screen is touched.
	 */
	void pointerPressEvent(MAPoint2d point)
	{
		// Print the x and y coordinate.
		if (x) {
			x=0;
			MAHandle h = MAUtil::PlaceholderPool::alloc();
			int r = maCameraSnapshot(0, h);
			maCameraStop();
			printf("maCameraSnapshot: %i", r);
			printf("Size: %i", maGetDataSize(h));

			MAHandle f = maFileOpen("/sdcard/x.jpg", MA_ACCESS_READ_WRITE);
			if (!maFileExists(f)) {
				maFileCreate(f);
			}
			maFileWriteFromData(f, h, 0, maGetDataSize(h));
			maFileClose(f);
			MAUtil::PlaceholderPool::put(h);

		} else {
			x=1;
			MA_CAMERA_FORMAT fmt;
			fmt.height = 480;
			fmt.width = 640;
			//maCameraFormat(0, &fmt);
			//maCameraSelect(0);
			int r = maCameraStart();
			if (r <= 0) {
				char *m = new char[50];
				sprintf(m, "Error %i", r);
				maPanic(r, m);
			}
		}

		printf("You touched: %i %i\n", point.x, point.y);
	}
};
/**
 * Entry point of the program. The MAMain function
 * needs to be declared as extern "C".
 */
extern "C" int MAMain()
{
	Moblet::run(new MyMoblet());
	return 0;
}

 

Hi,

1) If I set camera format 0 to fmt, maCameraSnapshot returns -2.

2) I think the problem is maFileWriteFromData. My app creates a datastore and uses maCopyData, but maFile is also raising panic.

Regards,

Diego

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

Hi,

Any good news?

Regards,

Diego

Ali Sarrafi
ali.s's picture
Offline
MoSync User
Joined: 20 Nov 2010
Posts:

Hi Diego,

Sorry for not coming back to you earlier. I have tried you app on my android phone and it worked pretty fine. Even the maCameraFormat part seems to work. The only problem is that if you skip calling maCameraFormat you need to call maCameraSnapshot wit ha negative number so it picks the default camera setup. On some phones it might just work. Unfortunately we don't have the devices you have so I can not test to see if there is something specific. Anyway I'll try it again on some other phones and tablets and will get back to you as soon as possible.

Regards
Ali

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

Hi,

nativeCreateBinaryResource does not raise a Panic if allocation goes wrong. Is that a possible problem? I'm using a 8M camera.

PictureCallback rawCallback = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
if(rawMode == true)
{
lock.lock();
try {
mMoSyncThread.nativeCreateBinaryResource(resourceIndex, data.length);
ByteBuffer byteBuffer = mMoSyncThread.mBinaryResources.get(resourceIndex);
byteBuffer.put(data);
dataReady = true;
condition.signalAll();
}
catch (Exception e) {
SYSLOG("Failed to create the data pool");
}
finally {
lock.unlock();
}
}
}
};

Regards,

Diego

BTW: maCameraSnapshot is returning a random number (neither MA_CAMERA_RES_OK, nor MA_CAMERA_RES_FAILED)

BTW2: CameraDemo is not working.
1) Take Picture > Image > Back does not starts camera.
2) Although set to 270x270, first picture has a different resolution. In fact, preview screen decreases the res after taking pic.

Ali Sarrafi
ali.s's picture
Offline
MoSync User
Joined: 20 Nov 2010
Posts:

Hi Diego,

As far as I know it doesn't throw a panic if it runs out of memory it just does not create the data. But I look into it, again and see if there can be something wrong when you use a really large image file.

Regarding BTW: on which device does that happen and is it in the nightly build or a major release?

Cheers
Ali

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

Hi Ali,

 

#include <MAUtil/Moblet.h>
#include <MAUI/Screen.h>
#include <MAUI/Label.h>
#include <MAUtil/PlaceholderPool.h>
#include <conprint.h>
#include <mavsprintf.h>
using namespace MAUtil;
using namespace MAUI;
class MyMoblet : public Moblet
	{
	public:
	    int x;
	    MAHandle hPicture;
	    void setupCameraProperties() {
	    	// *** Seleciona formato da foto ***
	    	MA_CAMERA_FORMAT fmt;
			fmt.height = 270;
			fmt.width = 270;
			maCameraFormat(0, &fmt);
			// *** Seleciona camera ***
			maCameraSelect(0);
			// *** Seleciona flash ***
			maCameraSetProperty(MA_CAMERA_FLASH_MODE, MA_CAMERA_FLASH_OFF);
			// *** Retorna ZOOM maximo ***
			char buffer[256];
			maCameraGetProperty(MA_CAMERA_MAX_ZOOM, buffer, 256);
			maScreenSetOrientation(SCREEN_ORIENTATION_LANDSCAPE);
	    }
	    /**
	     * Initialize the application in the constructor.
	     */
	    MyMoblet() {
	    	hPicture = 0;
	    	x = 0;
	    	setupCameraProperties();
	    	maCameraStart();
	    }
	    ~MyMoblet() {
	    	maCameraStop();
	    }
	    /**
	     * Called when a key is pressed.
	     */
	    void keyPressEvent(int keyCode, int nativeCode)
	    {
	        if (MAK_BACK == keyCode || MAK_0 == keyCode)
	        {
	            // Call close to exit the application.
	            close();
	        }
	        // Print the key character.
	        printf("You typed: %c\n", keyCode);
	    }
	    /**
	     * Called when the screen is touched.
	     */
	    void pointerPressEvent(MAPoint2d point) {
	    	x++;
	    	if (hPicture != 0) {
	    		maDestroyObject(hPicture);
	    	}
	    	hPicture = maCreatePlaceholder();
	    	int ret = maCameraSnapshot(0, hPicture);
	    	//*** Grava figura ***
	    	char s[50];
	    	sprintf(s, "/sdcard/%s.%i.%i.jpg", __TIME__, x, ret);
	    	s[10] = '_';
	    	s[13] = '_';
	    	s[16] = '_';
	    	// #1
	    	if (ret >= 0) {
				MAHandle f = maFileOpen(s, MA_ACCESS_READ_WRITE);
				if (!maFileExists(f)) {
					maFileCreate(f);
				}
				maFileWriteFromData(f, hPicture, 0, maGetDataSize(hPicture));
				maFileClose(f);
	    	} else {
	    		hPicture = 0;
	    		MAHandle f = maFileOpen(s, MA_ACCESS_READ_WRITE);
				if (!maFileExists(f)) {
					maFileCreate(f);
				}
				maFileClose(f);
	    	}

	    	// #2
	    	maCameraStop();
	    	// #3
	    	setupCameraProperties();
	    	// #4
	    	maCameraStart();
	    }
	};
	/**
	 * Entry point of the program. The MAMain function
	 * needs to be declared as extern "C".
	 */
	extern "C" int MAMain()
	{
	    Moblet::run(new MyMoblet());
	    return 0;
	}

 

This is the code I'm using. MoSync build 111117. Devices: Motorola Xoom, Motorola Defy (Froyo 2.2), SE Xperia X10 (GB 2.3.3 - Wolfxperia ROM)

Some points:

#1) Yes, I'm using file name to get error code. 1st click returns -2, 2nd sometimes OK. When returning OK, maFileWrite fails with 40001.

#4) W/o starting again, preview window is freezed w/ the last picture.

#3) Is it really necessary to setup again since camera class is not freed?

#2) maStop crashed the app if called twice.

Regards,

Diego

 

 

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

Hi,

could you send me a VM (VirtualBox/VMWare) w/ the building environment working? I could help you finding the problem.

Regards,

Diego

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

Hi, http://jira.mosync.com/browse/MOSYNC-425 states that maGetDataSize can't be used w/ pictures. Is that right? How do I save a snapshot? Regards, Diego

Mikael Kindborg
miki's picture
Offline
Mobile Wizard
Joined: 29 Jun 2010
Posts:

Hi Diego,

You can use maGetImageSize with a picture handle, it returns width and height.

maGetImageData reads the raw image data into a handle, and on that handle you can use maGetDataSize to get the total buffer size.

I discovered problems with maGetImageData on Android, my app just stops working when I call it on an image I captured from the camera. It is a Lua program, so it might be a Lua-related problem. Will look further into this.

Best regards, Mikael

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

Hi Mikael,

I dont use Lua and I'm also having problems w/ the image captured by the camera. I checked Jira and there are 3 or 4 opened issues regarding camera, but I don't know if one of them is related to this post.

Regards,

Diego