Skip to content

Commit

Permalink
MOSYNC-2169: the video view now plays correctly because the VideoBrus…
Browse files Browse the repository at this point in the history
…h source from the camera module is not set when the module is initialized (the videobrush is based on a media element and inside a wp7 app only one media element can run at a time - because this got initialized before the video view, playing media didn't work); MOSYNC-2234: the camera now supports orientation (the video brush is rotated accordigly to the phone orientation) + updated the CameraDemo example to support orientation
  • Loading branch information
spiridon-alexandru committed Jun 19, 2012
1 parent 23ccc5c commit 41a1352
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 10 deletions.
5 changes: 5 additions & 0 deletions examples/cpp/CameraDemo/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ class AppScreen : public ButtonListener, public StackScreenListener
mCurrentZoomIndex = 0;
createUI();

// set the screen supported orientations
int mSupportedOrientations = MA_SCREEN_ORIENTATION_PORTRAIT |
MA_SCREEN_ORIENTATION_LANDSCAPE_RIGHT |
MA_SCREEN_ORIENTATION_LANDSCAPE_LEFT;
maScreenSetSupportedOrientations(mSupportedOrientations);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
using System.Collections.Generic;
using Microsoft.Devices;
using System.Threading;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Media;
using Microsoft.Phone.Controls;

namespace MoSync
{
Expand Down Expand Up @@ -40,9 +42,21 @@ public class CameraModule : IIoctlModule
*/
private FlashMode mFlashMode = FlashMode.Off;

/**
* Used to check if the camera is initialized or not.
*/
bool isCameraInitialized = false;

bool GetCameraFormat(int index, out System.Windows.Size dim)
{
dim = new System.Windows.Size();

// if the camera is not initialized, we cannot access any of its properties
if (!isCameraInitialized)
{
return false;
}

IEnumerable<System.Windows.Size> res = mCamera.AvailableResolutions;
if (res == null) return false;
IEnumerator<System.Windows.Size> resolutions = res.GetEnumerator();
Expand All @@ -65,24 +79,62 @@ bool GetCameraFormat(int index, out System.Windows.Size dim)
public void Init(Ioctls ioctls, Core core, Runtime runtime)
{
mCamera = new PhotoCamera(mCameraType);
mVideoBrush = new VideoBrush();
mVideoBrush.SetSource(mCamera);
mVideoBrush = new VideoBrush();

runtime.RegisterCleaner(delegate()
{
mCamera.Dispose();
mCamera = null;
if (null != mCamera)
{
mCamera.Dispose();
mCamera = null;
}
});

// this should be set according to the orientation of
// the device I guess.

// we need to handle the camera orientation by hand
PhoneApplicationPage currentPage = (((PhoneApplicationFrame)Application.Current.RootVisual).Content as PhoneApplicationPage);

// we need to handle the initial page orientation
double rotation = mCamera.Orientation;
if (currentPage.Orientation == PageOrientation.LandscapeLeft)
{
rotation -= 90;
}
else if (currentPage.Orientation == PageOrientation.LandscapeRight)
{
rotation += 90;
}
mVideoBrush.RelativeTransform = new CompositeTransform()
{
CenterX = 0.5,
CenterY = 0.5,
Rotation = 90
Rotation = rotation
};

// on orientation changed, we need to rotate the video brush
currentPage.OrientationChanged += new System.EventHandler<OrientationChangedEventArgs>(
delegate(object o, OrientationChangedEventArgs args)
{
rotation = mCamera.Orientation;
if (args.Orientation == PageOrientation.LandscapeLeft)
{
rotation -= 90;
}
else if (args.Orientation == PageOrientation.LandscapeRight)
{
rotation += 90;
}

mVideoBrush.RelativeTransform = new CompositeTransform()
{
CenterX = 0.5,
CenterY = 0.5,
Rotation = rotation
};
});

/**
* Stores an output format in fmm parameter.
* @param _index int the index of the required format.
Expand All @@ -105,10 +157,19 @@ public void Init(Ioctls ioctls, Core core, Runtime runtime)
};

/**
* Returns the number of camera output formats avalable.
*/
* Returns the number of different output formats supported by the current device's camera.
* \< 0 if there is no camera support.
* 0 if there is camera support, but the format is unknown.
*/
ioctls.maCameraFormatNumber = delegate()
{
// if the camera is not initialized, we cannot access any of its properties
if (!isCameraInitialized)
{
// because the cammera is supported but not initialized, we return 0
return 0;
}

IEnumerable<System.Windows.Size> res = mCamera.AvailableResolutions;
if (res == null) return 0;
IEnumerator<System.Windows.Size> resolutions = res.GetEnumerator();
Expand Down Expand Up @@ -157,6 +218,13 @@ public void Init(Ioctls ioctls, Core core, Runtime runtime)
*/
ioctls.maCameraSetPreview = delegate(int _widgetHandle)
{
// if the camera is not initialized, we need to initialize it before
// setting the preview
if (!isCameraInitialized)
{
initCamera();
}

IWidget w = runtime.GetModule<NativeUIModule>().GetWidget(_widgetHandle);
if (w.GetType() != typeof(MoSync.NativeUI.CameraPreview))
{
Expand Down Expand Up @@ -231,6 +299,12 @@ public void Init(Ioctls ioctls, Core core, Runtime runtime)
*/
ioctls.maCameraSetProperty = delegate(int _property, int _value)
{
// if the camera is not initialized, we cannot access any of its properties
if (!isCameraInitialized)
{
return MoSync.Constants.MA_CAMERA_RES_PROPERTY_NOTSUPPORTED;
}

String property = core.GetDataMemory().ReadStringAtAddress(_property);
String value = core.GetDataMemory().ReadStringAtAddress(_value);

Expand Down Expand Up @@ -282,6 +356,12 @@ public void Init(Ioctls ioctls, Core core, Runtime runtime)

ioctls.maCameraSelect = delegate(int _camera)
{
// if the camera is not initialized, we cannot access any of its properties
if (!isCameraInitialized)
{
return MoSync.Constants.MA_CAMERA_RES_FAILED;
}

if ( MoSync.Constants.MA_CAMERA_CONST_BACK_CAMERA == _camera)
{
if (mCamera.CameraType != CameraType.Primary)
Expand Down Expand Up @@ -350,23 +430,28 @@ private void initCamera()
mCamera = null;
mCamera = new PhotoCamera(mCameraType);

isCameraInitialized = false;

AutoResetEvent are = new AutoResetEvent(false);
mCamera.Initialized += new EventHandler<CameraOperationCompletedEventArgs>(
delegate(object o, CameraOperationCompletedEventArgs args)
{
try
{
mCamera.FlashMode = mFlashMode;
isCameraInitialized = true;
are.Set();
}
catch { }
});

if (null == mVideoBrush)
mVideoBrush = new VideoBrush();

MoSync.Util.RunActionOnMainThreadSync(() =>
{
mVideoBrush.SetSource(mCamera);
});
// we need to wait until the camere is initialized before doing other operations
// with it or getting/setting its properties
are.WaitOne();
}
} // end class CameraModule
} // end namespace MoSync

0 comments on commit 41a1352

Please sign in to comment.