Example #1
0
	void Font::setResource(MAHandle font) {
		//printf("Font is using resource: %d\n", font);
		if(font == 0) {
			mFontImage = 0;
			return;
		}

		initFontReader(font);

		int fontInfoSize = readInt();
		int imageSize = readInt();

		if(this->mCharset)
			delete this->mCharset;

		this->mCharset = new Charset;
		if(!parseBitmapFontGeneratorFile(font, 8+fontInfoSize, this->mCharset)) {
			maPanic(0, "Font::setResource(MAHandle font), could not parse font");
			return;
		}
		closeFontReader();

		this->mFontImage = PlaceholderPool::alloc();//maCreatePlaceholder();
		int err;
		if((err=maCreateImageFromData(this->mFontImage, font, 8+fontInfoSize, imageSize))!=RES_OK) {
			if(err==RES_OUT_OF_MEMORY)
				maPanic(0, "Font::setResource(MAHandle font), out of memory, image could not be loaded");
			if(err==RES_BAD_INPUT)
				maPanic(0, "Font::setResource(MAHandle font), image could not be loaded due to bad input");
		}
	}
/**
 * Inherited from HighLevelHttpConnection.
 * Called when the HTTP connection has finished
 * downloading data.
 * Calls onDownloadComplete.
 * @param data Handle to the data, will be
 * 0 on error, > 0 on success.
 * @param result Result code, RES_OK on success,
 * otherwise an HTTP error code.
 */
void HighLevelImageDownloader::dataDownloaded(MAHandle data, int result)
{
	// The resulting image.
	MAHandle image = 0;

	// Do we have any data?
	if (data)
	{
		// Convert data to image.
		image = maCreatePlaceholder();
		int res = maCreateImageFromData(
			image,
			data,
			0,
			maGetDataSize(data));

		// Do we have an error?
		if (RES_OUT_OF_MEMORY == res)
		{
			// The image could not be created, set data handle to zero.
			image = 0;
		}

		// Deallocate the data object, we are done with it.
		maDestroyPlaceholder(data);
	}

	// Notify download complete.
	onDownloadComplete(image);
}
Example #3
0
MAHandle FacebookResponse::getImageData() const {
	MAHandle data = maCreatePlaceholder();
	maCreateData(data, getDataSize());
	maWriteData(data, getData(), 0, getDataSize());
	MAHandle image = maCreatePlaceholder();
	maCreateImageFromData(image, data, 0, getDataSize());
	maDestroyPlaceholder(data);
	return image;
}
MAHandle FacebookResponse::getImageData() const {
	MAHandle data = MAUtil::PlaceholderPool::alloc();
	maCreateData(data, getDataSize());
	maWriteData(data, getData(), 0, getDataSize());
	MAHandle image = MAUtil::PlaceholderPool::alloc();
	maCreateImageFromData(image, data, 0, getDataSize());
	MAUtil::PlaceholderPool::put(data);
	return image;
}
Example #5
0
/*
 * Load the images from resources according to the screen resolution.
 * @param screenWidth The width of the screen.
 */
void ScreenImageSwiper::loadImages(int screenWidth)
{
	// Set the image indexes inside the resources according to the resolution.
	int firstImage = RES_FIRST_IMAGE_LARGE;
	int lastImage = RES_LAST_IMAGE_LARGE;
	if (screenWidth <= SMALL_SCREEN_RESOLUTION)
	{
		firstImage = RES_FIRST_IMAGE_SMALL;
		lastImage = RES_LAST_IMAGE_SMALL;
	}
	else if (screenWidth <= MEDIUM_SCREEN_RESOLUTION)
	{
		firstImage = RES_FIRST_IMAGE_MEDIUM;
		lastImage = RES_LAST_IMAGE_MEDIUM;
	}

	// Compute the number of images.
	mImagesSize = lastImage - firstImage - 1;

	// Create the image widgets.
	for (int i = 0; i < mImagesSize; i++)
	{
		// Byte index in the resource data.
		int pos = 0;

		// Create an image object.
		mImages[i] = new ScreenImage();

		// Get the resource id of the current image.
		int resID = firstImage + i + 1;

		// Create a placeholder to store the current image.
		mImages[i]->setHandle(maCreatePlaceholder());

		// Load the image name from resources.
		MAUtil::String name;
		// This reads the string into name and updates
		// the value of pos to the start of the image data.
		readStringFromResource(resID, pos, name);
		// Set the image name.
		mImages[i]->setName(name);

		// Create the image from ubin. Note that pos is
		// updated by the call to readStringFromResource
		// to point at the start of the image data.
		maCreateImageFromData(
			mImages[i]->getHandle(),
			resID,
			pos,
			maGetDataSize(resID) - mImages[i]->getName().length() - 1);
	}
}
	void MoSyncCamController::snapshotDisplayRequested()
	{
		if ( !isDisplayed(*mImageViewerScreen) )
		{
			if ( mLastSnapshotDataHandle > 0 )
			{
				mDisplayedImageHandle = maCreatePlaceholder();

				maCreateImageFromData(
					mDisplayedImageHandle,
					mLastSnapshotDataHandle,
					0,
					maGetDataSize(mLastSnapshotDataHandle));

				mImageViewerScreen->setImageWithData(mDisplayedImageHandle);
				mImageViewerScreen->showWithTransition(mForwardTransition, SCREEN_TRANSITION_DURATION);

				setCurrentScreen(*mImageViewerScreen);
			}
		}
	}
Example #7
0
int NativeScreen::handleImageData(MAHandle myImageData)
{
	printf("handleImageData(%d)", myImageData);

	int resCode = -1;

	MAHandle hImage = maCreatePlaceholder();
	int dataSize = maGetDataSize(myImageData);
	int createImageRes = maCreateImageFromData(hImage, myImageData, 0, dataSize);

	// Used for testing only.
	MAUtil::String info = "Ready.Size = " + MAUtil::integerToString(dataSize)
			            + " res = " + MAUtil::integerToString(createImageRes);
	printf("\n%s\n", info.c_str());

	if (createImageRes != RES_OK) {
		// If the Android VM gets an out of memory exception, get the image handle instead.
		maAlert("Memory Warning", " The image cannot be created. Try again", NULL, NULL, NULL);
		maWidgetSetProperty(mEventReturnTypeCheckbox, MAW_CHECK_BOX_CHECKED, "false");
		maImagePickerOpen();
	} else {
		char imgHandle[256];
		sprintf(imgHandle, "%d", hImage);
		printf("imgHandle=%s---hImage=%d", imgHandle, hImage);

		// display the preview
		resCode = maWidgetSetProperty(mPreview, MAW_IMAGE_IMAGE, imgHandle);
	}

	// at this point the new selected image is either displayed or non-existent (out of memory)
	// the former displayed image (if exists) can be now safely deleted and reused
	if (mLastDisplayedImageHandle != -1) {
		maDestroyPlaceholder(mLastDisplayedImageHandle);
	}
	mLastDisplayedImageHandle = hImage;

	return resCode;
}
Example #8
0
/**
 * Handle widget events.
 * @param event A MoSync event data structure.
 */
void NativeScreen::customEvent(const MAEvent& event)
{
	// Get the information sent by the widget.
	MAWidgetEventData* widgetEventData = (MAWidgetEventData*) event.data;

	if ( event.type == EVENT_TYPE_IMAGE_PICKER)
	{
		if ( event.imagePickerState == 1 )
		{
			// ready, get handle
			MAHandle myImage = event.imagePickerItem;

			char checkboxBuffer[BUF_SIZE];
			maWidgetGetProperty(mEventReturnTypeCheckbox, MAW_CHECK_BOX_CHECKED, checkboxBuffer, BUF_SIZE);
	        MAUtil::String value = MAUtil::lowerString(checkboxBuffer);
	        if ( strcmp(value.c_str(),"true") == 0 )
			{
				MAHandle hImage = maCreatePlaceholder();
				int dataSize = maGetDataSize(event.imagePickerItem);
				int createImageRes= maCreateImageFromData(hImage, event.imagePickerItem, 0, dataSize);

				// Used for testing only.
				MAUtil::String info = "Ready.Size = " + MAUtil::integerToString(dataSize) +
						"res = " + MAUtil::integerToString(createImageRes) +
						", mime type = " + MAUtil::integerToString(event.imagePickerEncodingType);

				if ( createImageRes != RES_OK )
				{
					maAlert("Memory Warning", " The image cannot be created. Try again", NULL, NULL, NULL);
					maWidgetSetProperty(mEventReturnTypeCheckbox, MAW_CHECK_BOX_CHECKED, "false");
					// If the Android VM gets an out of memory exception, get the image handle instead.
					maImagePickerOpen();
				}
				else
				{
					char imgHandle[256];
					sprintf(imgHandle, "%d", hImage);
					int resCode = maWidgetSetProperty(mPreview, MAW_IMAGE_IMAGE, imgHandle);
				}

				maDestroyPlaceholder(hImage);
			}
			else
			{
				char buffer[256];
				sprintf(buffer, "%d", myImage);
				int resCode = maWidgetSetProperty(mPreview, MAW_IMAGE_IMAGE, buffer);
			}

			setLabelText(mLabel, "Preview is available");
		}
		else
		{
			setLabelText(mLabel, "The user canceled the image selection");
		}
	}

	// Check that the event was a click (touch) event.
	if (widgetEventData->eventType == MAW_EVENT_CLICKED)
	{
		// Handle the event emitted by the widget
		widgetClicked(widgetEventData->widgetHandle);
	}
}
	/**
	 * Loads an image from a file and returns the handle to it.
	 *
	 * @param imagePath relative path to the image file.
	 */
	MAHandle ResourceMessageHandler::loadImageResource(const char* imagePath)
	{
		if (!getFileUtil())
		{
			return 0;
		}

		// Get the current apllication directory path.
		String appPath = getFileUtil()->getAppPath();

		// Construct image path.
		char completePath[2048];
		sprintf(completePath,
				"%s%s",
				appPath.c_str(),
				imagePath);

		// Load the image and create a data handle from it.
		MAHandle imageFile = maFileOpen(completePath, MA_ACCESS_READ);
		if (imageFile < 0)
		{
			return 0;
		}

		int fileSize = maFileSize(imageFile);
		if (fileSize < 1)
		{
			return 0;
		}

		// Create buffer to hold file data.
		MAHandle fileData = maCreatePlaceholder();
		int result = maCreateData(fileData, fileSize);
		if (RES_OK != result)
		{
			maDestroyPlaceholder(fileData);
			return 0;
		}

		// Read data from file.
		result = maFileReadToData(imageFile, fileData, 0, fileSize);
		maFileClose(imageFile);
		if (result < 0)
		{
			maDestroyPlaceholder(fileData);
			return 0;
		}

		// Create image.
		MAHandle imageHandle = maCreatePlaceholder();
		result = maCreateImageFromData(
				imageHandle,
				fileData,
				0,
				maGetDataSize(fileData));
		maDestroyPlaceholder(fileData);
		if (RES_OK != result)
		{
			maDestroyPlaceholder(imageHandle);
			return 0;
		}

		// Return the handle to the loaded image.
		return imageHandle;
	}