public virtual void Dispose() { if (_phoneCamera != null) { _phoneCamera.Dispose(); } }
public void SetPreferedCameraType(CameraType type) { if (PhotoCamera.IsCameraTypeSupported(type)) { _cameraType = type; _currentCamera.Dispose(); _currentCamera = null; LoadCameraBrush(); } }
protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e) { //we're navigating away from this page, we won't be scanning any barcodes _scanTimer.Stop(); if (_phoneCamera != null) { // Cleanup _phoneCamera.Dispose(); _phoneCamera.Initialized -= cam_Initialized; CameraButtons.ShutterKeyHalfPressed -= CameraButtons_ShutterKeyHalfPressed; } }
private void UserControl_Unloaded(object sender, RoutedEventArgs e) { if (_photoCamera != null) { // Dispose camera to minimize power consumption and to expedite shutdown. _photoCamera.Dispose(); // Release memory, ensure garbage collection. _photoCamera.Initialized -= PhotoCameraOnInitialized; _photoCamera.AutoFocusCompleted -= PhotoCameraOnAutoFocusCompleted; _reader.ResultFound -= ReaderOnResultFound; } }
/* * protected override void OnOrientationChanged(OrientationChangedEventArgs e) * { * if (cam != null) * { * // LandscapeRight rotation when camera is on back of phone. * int landscapeRightRotation = 180; * * // Change LandscapeRight rotation for front-facing camera. * if (cam.CameraType == CameraType.FrontFacing) landscapeRightRotation = -180; * * // Rotate video brush from camera. * if (e.Orientation == PageOrientation.LandscapeRight) * { * // Rotate for LandscapeRight orientation. * videoBrush.RelativeTransform = * new CompositeTransform() { CenterX = 0.5, CenterY = 0.5, Rotation = landscapeRightRotation }; * } * else * { * // Rotate for standard landscape orientation. * videoBrush.RelativeTransform = * new CompositeTransform() { CenterX = 0.5, CenterY = 0.5, Rotation = 0 }; * } * } * * /* * if( cam != null ) * { * Dispatcher.BeginInvoke (() => * { * double rotation = cam.Orientation; * * switch (this the Orientation) * { * case PageOrientation.LandscapeLeft: * rotation = cam.Orientation - 90; * break; * * case PageOrientation.LandscapeRight: * rotation = cam.Orientation + 90; * break; * } * } * } * * * base.OnOrientationChanged(e); * } */ protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e) { if (cam != null) { cam.Dispose(); } }
/** * Reinitializes the camera: creates a new one, and adds it as a source for the * video brush. */ private void InitCamera() { mCamera.Dispose(); 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 { } }); 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(); }
/// <summary> /// Escanea el buffer de la cámara y si /// detectó un código QR lo manda a la vista ImagePicker /// </summary> private void ScanPreviewBuffer() { try { photoCamera.GetPreviewBufferY(luminance.PreviewBufferY); var binarizer = new HybridBinarizer(luminance); var binBitmap = new BinaryBitmap(binarizer); var result = reader.decode(binBitmap); if (result == null) { return; } // Se leyó el código QR this.qrCodeText = result.Text; this.Dispatcher.BeginInvoke(() => { timer.Stop(); photoCamera.Dispose(); NavigationService.Navigate(new Uri(String.Format("/ImagePicker.xaml?qr={0}", qrCodeText), UriKind.Relative)); }); } catch (Exception) { MessageBox.Show("Ocurrió un error al tratar de decodificar el código QR", "Photo Sharing", MessageBoxButton.OK); } }
protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e) { //we're navigating away from this page, we won't be scanning any barcodes _scanTimer.Stop(); if (_phoneCamera != null) { // Cleanup _phoneCamera.Dispose(); _phoneCamera.Initialized -= Camera_Initialized; CameraButtons.ShutterKeyHalfPressed -= CameraButtons_ShutterKeyHalfPressed; } if (_scanConfirmation.IsOpen) { _scanConfirmation.IsOpen = false; } if (OpticalReaderTask.TaskPending) { if (e.NavigationMode == NavigationMode.Back) { OpticalReaderTask.CancelTask(true); } else { OpticalReaderTask.CancelTask(false); } } }
protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e) { _cam.CancelFocus(); _cam.Dispose(); base.OnNavigatingFrom(e); }
void CleanUpCamera() { CameraButtons.ShutterKeyPressed -= cameraButtons_ShutterKeyPressed; camera.CaptureImageAvailable -= camera_CaptureImageAvailable; camera.CaptureCompleted -= camera_CaptureCompleted; camera.Dispose(); camera = null; }
protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e) { if (cam != null) { cam.Dispose(); cam.CaptureImageAvailable -= cam_CaptureImageAvailable; } }
// ページがアクティブでなくなったら呼び出される protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e) { //カメラを解放 if (camera != null) { camera.Dispose(); camera = null; } }
protected override void OnNavigatingFrom(NavigatingCancelEventArgs e) { if (_camera == null) { return; } // Dispose camera to minimize power consumption and to expedite shutdown. _camera.Dispose(); }
protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e) { myVM.PhoneOrientationChanged -= myVM_PhoneOrientationChanged; _gameTimer.Update -= _gameTimer_Update; _gameTimer.Draw -= _gameTimer_Draw; OnExternalUserControlChanged -= ARView_OnExternalUserControlChanged; this.ExternalUserControlPlaceHolder.Children.Clear(); myVM.Suspend(); if (_motion != null) { _motion.Stop(); _motion.Dispose(); _motion = null; } if (_cam != null) { _cam.Dispose(); _cam = null; } // Stop the timer if (_gameTimer != null) { _gameTimer.Stop(); _gameTimer.Dispose(); } //Stop gesture Timer if (_gestureTimer != null) { _gestureTimer.Stop(); _gestureTimer = null; } // Set the sharing mode of the graphics device to turn off XNA rendering SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(false); foreach (LocationsVM.Location l in myVM.SelectedNearbyLocations) { placeholderGrid.Children.Remove(l.PlacemarkPresenter); radarCanvas.Children.Remove(l.RadarPointRepresenter); l.IsClickedOnView = false; l.IsAnimatingView = false; l.IsAnimatingWorld = false; } //if (e.NavigationMode == NavigationMode.Back) // NavigationService.Navigate(new Uri("/RealSquare;component/MainPage.xaml", UriKind.Relative)); base.OnNavigatingFrom(e); }
protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e) { if (_photoCamera != null) { _timer.Stop(); _photoCamera.CancelFocus(); _photoCamera.Dispose(); } base.OnNavigatingFrom(e); }
protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e) { _scanTimer.Stop(); if (_phoneCamera != null) { _phoneCamera.Dispose(); _phoneCamera.Initialized -= cam_Initialized; CameraButtons.ShutterKeyHalfPressed -= CameraButtons_ShutterKeyHalfPressed; } }
protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e) { if (cam != null) { // Dispose of the camera to minimize power consumption and to expedite shutdown. cam.Dispose(); // Release memory, ensure garbage collection. cam.Initialized -= cam_Initialized; } }
protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e) { if (cam != null) { // Notify the background worker to stop processing. bgPleaseExit = true; bgThread.Join(); // Dispose of the camera object to free memory. cam.Dispose(); } }
protected override void OnNavigatingFrom(NavigatingCancelEventArgs e) { if (!cameraInitialized) { closeAfterCameraInit = true; e.Cancel = true; return; } photoCamera?.Dispose(); timer?.Stop(); base.OnNavigatingFrom(e); }
protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e) { if (_cam == null) { return; } _cam.Dispose(); _cam.CaptureCompleted -= CamCaptureCompleted; _cam.CaptureImageAvailable -= CamCaptureImageAvailable; _cam.CaptureThumbnailAvailable -= CamCaptureThumbnailAvailable; }
/// <summary> /// Stops the camera service so it's no longer retrieving data. /// </summary> /// <remarks> /// This method is already protected and only called when the service is currently running. /// </remarks> protected override void StopService() { _photoCamera.Initialized -= OnPhotoCameraInitialized; _photoCamera.AutoFocusCompleted -= OnPhotoCameraAutoFocusCompleted; _photoCamera.CaptureStarted -= OnPhotoCameraCaptureStarted; _photoCamera.CaptureThumbnailAvailable -= OnPhotoCameraCaptureThumbnailAvailable; _photoCamera.CaptureImageAvailable -= OnPhotoCameraCaptureImageAvailable; _photoCamera.CaptureCompleted -= OnPhotoCameraCaptureCompleted; _photoCamera.Dispose(); _photoCamera = null; }
protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e) { if (_photoCamera != null) { // nama 3 ovat omia lisayksia _previewVideo = null; CameraButtons.ShutterKeyHalfPressed -= (o, arg) => _photoCamera.Focus(); _timer.Tick -= (o, arg) => ScanPreviewBuffer(); _photoCamera.Initialized -= OnPhotoCameraInitialized; _photoCamera.Dispose(); } }
public void StopCamera() { if (_camera != null) { // Notify the background worker to stop processing. _threadPleaseExit = true; // Dispose camera to minimize power consumption and to expedite shutdown. _camera.Dispose(); _camera = null; _thread = null; } }
protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e) { if (cam != null) { // Dispose camera to minimize power consumption and to expedite shutdown. cam.Dispose(); // Release memory, ensure garbage collection. cam.Initialized -= cam_Initialized; cam.CaptureCompleted -= cam_CaptureCompleted; cam.CaptureImageAvailable -= cam_CaptureImageAvailable; cam.CaptureThumbnailAvailable -= cam_CaptureThumbnailAvailable; } }
protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e) { if (cam != null) { //Close camera cam.Dispose(); //Release memory cam.Initialized -= cam_Initialized; cam.CaptureCompleted -= cam_CaptureCompleted; cam.CaptureImageAvailable -= cam_CaptureImageAvailable; cam.CaptureThumbnailAvailable -= cam_CaptureThumbnailAvailable; CameraButtons.ShutterKeyPressed -= OnButtonFullPress; } }
/// <summary> /// Stops the camera and capture process /// </summary> public void Stop() { doCancel = true; if (_timer != null && _timer.IsEnabled) { _timer.Stop(); } if (_photoCamera != null) { _photoCamera.Dispose(); _photoCamera = null; } }
protected override void OnNavigatedFrom(NavigationEventArgs e) { base.OnNavigatedFrom(e); if ((Application.Current.RootVisual as PhoneApplicationFrame) != null) { (Application.Current.RootVisual as PhoneApplicationFrame).RemoveBackEntry(); } if (_photoCamera != null) { _timer.Stop(); _photoCamera.CancelFocus(); _photoCamera.Dispose(); } }
/// <summary> /// Closes the control and reverts any changes made to the underlying page. /// </summary> private void Close() { _timer.Stop(); _camera.Initialized -= Camera_Initialized; _camera.Dispose(); _isDone = true; _frame.BackKeyPress -= Frame_BackKeyPress; SystemTray.SetIsVisible(_underPage, _underPageTrayVisible); _underPage.ApplicationBar.IsVisible = _underPageAppBarVisible; // No null check, we know it's always non-null CloseRequested(this, EventArgs.Empty); }
private bool stopCamera() { if (_photoCamera == null) { return(false); } _timer.Stop(); _timer = null; _luminance = null; _reader = null; _photoCamera.Dispose(); _photoCamera = null; return(true); }
public void TeardownPage() { isInitialized = false; isDetecting = false; //turn off camera photoCamera.Dispose(); photoCamera.Initialized -= photoCamera_Initialized; // Stop the timer timer.Stop(); //pass control to the asynchronous background thread and turn off the camera //and models so that popups display correctly. SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(false); }
void UninitializeCamera(PhotoCamera camera) { if (camera == null) return; camera.Initialized -= CollectCameraCaps; camera.Dispose(); }
/** * Initializing the ioctls. */ public void Init(Ioctls ioctls, Core core, Runtime runtime) { mCamera = new PhotoCamera(mCameraType); mVideoBrush = new VideoBrush(); runtime.RegisterCleaner(delegate() { if (null != mCamera) { mCamera.Dispose(); mCamera = null; } }); PhoneApplicationPage currentPage = (((PhoneApplicationFrame)Application.Current.RootVisual).Content as PhoneApplicationPage); // set the initial camera orientation in respect to the current page orientation SetInitialCameraOrientation(currentPage); // handle current page orientation and adjust the camera orientation accordingly HandleDeviceOrientation(currentPage); /** * Stores an output format in fmm parameter. * @param _index int the index of the required format. * @param _fmt int the momory address at which to write the output format dimensions. * * Note: the _index should be greater than 0 and smaller than the number of camera formats. */ ioctls.maCameraFormat = delegate(int _index, int _fmt) { System.Windows.Size dim; if (GetCameraFormat(_index, out dim) == false) return MoSync.Constants.MA_CAMERA_RES_FAILED; core.GetDataMemory().WriteInt32(_fmt + MoSync.Struct.MA_CAMERA_FORMAT.width, (int)dim.Width); core.GetDataMemory().WriteInt32(_fmt + MoSync.Struct.MA_CAMERA_FORMAT.height, (int)dim.Height); return MoSync.Constants.MA_CAMERA_RES_OK; }; /** * 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(); resolutions.MoveNext(); int number = 0; while (resolutions.Current != null) { number++; resolutions.MoveNext(); if (resolutions.Current == new System.Windows.Size(0, 0)) break; } return number; }; /** * Starts the viewfinder and the camera */ ioctls.maCameraStart = delegate() { InitCamera(); MoSync.Util.RunActionOnMainThreadSync(() => { mCameraPrev.StartViewFinder(); }); return 0; }; /** * stops the view finder and the camera. */ ioctls.maCameraStop = delegate() { MoSync.Util.RunActionOnMainThreadSync(() => { mCameraPrev.StopViewFinder(); }); return 0; }; /** * Adds a previewWidget to the camera controller in devices that support native UI. */ 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)) { return MoSync.Constants.MA_CAMERA_RES_FAILED; } mCameraPrev = (NativeUI.CameraPreview)w; mCameraPrev.SetViewFinderContent(mVideoBrush); return MoSync.Constants.MA_CAMERA_RES_OK; }; /** * Returns the number of available Camera on the device. */ ioctls.maCameraNumber = delegate() { if (PhotoCamera.IsCameraTypeSupported(CameraType.FrontFacing) && PhotoCamera.IsCameraTypeSupported(CameraType.Primary)) return 2; else if (PhotoCamera.IsCameraTypeSupported(CameraType.FrontFacing) || PhotoCamera.IsCameraTypeSupported(CameraType.Primary)) return 1; return 0; }; /** * Captures an image and stores it as a new data object in the * supplied placeholder. * @param _formatIndex int the required format. * @param _placeHolder int the placeholder used for storing the image. */ ioctls.maCameraSnapshot = delegate(int _formatIndex, int _placeHolder) { AutoResetEvent are = new AutoResetEvent(false); System.Windows.Size dim; if (GetCameraFormat(_formatIndex, out dim) == false) return MoSync.Constants.MA_CAMERA_RES_FAILED; mCamera.Resolution = dim; if (mCameraSnapshotDelegate != null) mCamera.CaptureImageAvailable -= mCameraSnapshotDelegate; mCameraSnapshotDelegate = delegate(object o, ContentReadyEventArgs args) { MoSync.Util.RunActionOnMainThreadSync(() => { Resource res = runtime.GetResource(MoSync.Constants.RT_PLACEHOLDER, _placeHolder); Stream data = null; try { // as the camera always takes a snapshot in landscape left orientation, // we need to rotate the resulting image 90 degrees for a current PortraitUp orientation // and 180 degrees for a current LandscapeRight orientation int rotateAngle = 0; if (currentPage.Orientation == PageOrientation.PortraitUp) { rotateAngle = 90; } else if (currentPage.Orientation == PageOrientation.LandscapeRight) { rotateAngle = 180; } // if the current page is in a LandscapeLeft orientation, the orientation angle will be 0 data = RotateImage(args.ImageStream, rotateAngle); } catch { // the orientation angle was not a multiple of 90 - we keep the original image data = args.ImageStream; } MemoryStream dataMem = new MemoryStream((int)data.Length); MoSync.Util.CopySeekableStreams(data, 0, dataMem, 0, (int)data.Length); res.SetInternalObject(dataMem); }); are.Set(); }; mCamera.CaptureImageAvailable += mCameraSnapshotDelegate; mCamera.CaptureImage(); are.WaitOne(); return MoSync.Constants.MA_CAMERA_RES_OK; }; /** * Captures an image and stores it as a new data object in new * placeholder that is sent via #EVENT_TYPE_CAMERA_SNAPSHOT event. * @param _formatIndex int the required format index (size index). */ ioctls.maCameraSnapshotAsync = delegate(int _formatIndex) { System.Windows.Size dim; if (GetCameraFormat(_formatIndex, out dim) == false) { return MoSync.Constants.MA_CAMERA_RES_FAILED; } mCamera.Resolution = dim; if (mCameraSnapshotDelegate != null) { mCamera.CaptureImageAvailable -= mCameraSnapshotDelegate; } mCameraSnapshotDelegate = delegate(object o, ContentReadyEventArgs args) { MoSync.Util.RunActionOnMainThreadSync(() => { Stream data = null; try { // as the camera always takes a snapshot in landscape left orientation, // we need to rotate the resulting image 90 degrees for a current PortraitUp orientation // and 180 degrees for a current LandscapeRight orientation int rotateAngle = 0; if (currentPage.Orientation == PageOrientation.PortraitUp) { rotateAngle = 90; } else if (currentPage.Orientation == PageOrientation.LandscapeRight) { rotateAngle = 180; } // if the current page is in a LandscapeLeft orientation, the orientation angle will be 0 data = RotateImage(args.ImageStream, rotateAngle); } catch { // the orientation angle was not a multiple of 90 - we keep the original image data = args.ImageStream; } MemoryStream dataMem = new MemoryStream((int)data.Length); MoSync.Util.CopySeekableStreams(data, 0, dataMem, 0, (int)data.Length); Memory eventData = new Memory(20); const int MAEventData_eventType = 0; const int MAEventData_snapshotImageDataHandle = 4; const int MAEventData_snapshotFormatIndex = 8; const int MAEventData_snapshotImageDataRepresentation = 12; const int MAEventData_snapshotReturnCode = 16; eventData.WriteInt32(MAEventData_eventType, MoSync.Constants.EVENT_TYPE_CAMERA_SNAPSHOT); // Create new place holder. eventData.WriteInt32(MAEventData_snapshotImageDataHandle, runtime.AddResource( new Resource(dataMem, MoSync.Constants.RT_BINARY, true))); eventData.WriteInt32(MAEventData_snapshotFormatIndex, _formatIndex); eventData.WriteInt32(MAEventData_snapshotImageDataRepresentation, MoSync.Constants.MA_IMAGE_REPRESENTATION_RAW); eventData.WriteInt32(MAEventData_snapshotReturnCode, MoSync.Constants.MA_CAMERA_RES_OK); runtime.PostEvent(new Event(eventData)); }); }; mCamera.CaptureImageAvailable += mCameraSnapshotDelegate; mCamera.CaptureImage(); return MoSync.Constants.MA_CAMERA_RES_OK; }; /** * Sets the property represented by the string situated at the * _property address with the value situated at the _value address. * @param _property int the property name address * @param _value int the value address * * Note: the fallowing properties are not available on windows phone * MA_CAMERA_FOCUS_MODE, MA_CAMERA_IMAGE_FORMAT, MA_CAMERA_ZOOM, * MA_CAMERA_MAX_ZOOM. */ 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); if (property.Equals(MoSync.Constants.MA_CAMERA_FLASH_MODE)) { if (value.Equals(MoSync.Constants.MA_CAMERA_FLASH_ON)) { mCamera.FlashMode = FlashMode.On; mFlashMode = FlashMode.On; } else if (value.Equals(MoSync.Constants.MA_CAMERA_FLASH_OFF)) { mCamera.FlashMode = FlashMode.Off; mFlashMode = FlashMode.Off; } else if (value.Equals(MoSync.Constants.MA_CAMERA_FLASH_AUTO)) { mCamera.FlashMode = FlashMode.Auto; mFlashMode = FlashMode.Auto; } else return MoSync.Constants.MA_CAMERA_RES_INVALID_PROPERTY_VALUE; return MoSync.Constants.MA_CAMERA_RES_OK; } else if (property.Equals(MoSync.Constants.MA_CAMERA_FOCUS_MODE)) { return MoSync.Constants.MA_CAMERA_RES_PROPERTY_NOTSUPPORTED; } else if (property.Equals(MoSync.Constants.MA_CAMERA_IMAGE_FORMAT)) { return MoSync.Constants.MA_CAMERA_RES_PROPERTY_NOTSUPPORTED; } else if (property.Equals(MoSync.Constants.MA_CAMERA_ZOOM)) { return MoSync.Constants.MA_CAMERA_RES_PROPERTY_NOTSUPPORTED; } else if (property.Equals(MoSync.Constants.MA_CAMERA_MAX_ZOOM)) { return MoSync.Constants.MA_CAMERA_RES_PROPERTY_NOTSUPPORTED; } else return MoSync.Constants.MA_CAMERA_RES_PROPERTY_NOTSUPPORTED; }; /** * Selects a camera from the avalable ones; * in this eigther the back or the front camera is * chosen */ 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) { mCameraType = CameraType.Primary; InitCamera(); } } else if (MoSync.Constants.MA_CAMERA_CONST_FRONT_CAMERA == _camera) { if (mCamera.CameraType != CameraType.FrontFacing) { mCameraType = CameraType.FrontFacing; InitCamera(); MoSync.Util.RunActionOnMainThreadSync(() => { SetInitialCameraOrientation(currentPage); } ); } } else return MoSync.Constants.MA_CAMERA_RES_FAILED; return MoSync.Constants.MA_CAMERA_RES_OK; }; /** * Retrieves the specified property value in the given buffer. * @param _property int the address for the property string * @param _value int the address for the property value string (the buffer) * @param _bufSize int the buffer size */ ioctls.maCameraGetProperty = delegate(int _property, int _value, int _bufSize) { String property = core.GetDataMemory().ReadStringAtAddress(_property); if (property.Equals(MoSync.Constants.MA_CAMERA_MAX_ZOOM)) { core.GetDataMemory().WriteStringAtAddress( _value, "0", _bufSize); } else if (property.Equals(MoSync.Constants.MA_CAMERA_ZOOM_SUPPORTED)) { core.GetDataMemory().WriteStringAtAddress( _value, "false", _bufSize); } else if (property.Equals(MoSync.Constants.MA_CAMERA_FLASH_SUPPORTED)) { core.GetDataMemory().WriteStringAtAddress( _value, "true", _bufSize); } else return MoSync.Constants.MA_CAMERA_RES_PROPERTY_NOTSUPPORTED; return 0; }; ioctls.maCameraRecord = delegate(int _stopStartFlag) { return MoSync.Constants.MA_CAMERA_RES_FAILED; }; }
/** * Initializing the ioctls. */ public void Init(Ioctls ioctls, Core core, Runtime runtime) { mCamera = new PhotoCamera(mCameraType); mVideoBrush = new VideoBrush(); runtime.RegisterCleaner(delegate() { 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 = 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. * @param _fmt int the momory address at which to write the output format dimensions. * * Note: the _index should be greater than 0 and smaller than the number of camera formats. */ ioctls.maCameraFormat = delegate(int _index, int _fmt) { System.Windows.Size dim; if (GetCameraFormat(_index, out dim) == false) return MoSync.Constants.MA_CAMERA_RES_FAILED; core.GetDataMemory().WriteInt32(_fmt + MoSync.Struct.MA_CAMERA_FORMAT.width, (int)dim.Width); core.GetDataMemory().WriteInt32(_fmt + MoSync.Struct.MA_CAMERA_FORMAT.height, (int)dim.Height); return MoSync.Constants.MA_CAMERA_RES_OK; }; /** * 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(); resolutions.MoveNext(); int number = 0; while (resolutions.Current != null) { number++; resolutions.MoveNext(); if (resolutions.Current == new System.Windows.Size(0, 0)) break; } return number; }; /** * Starts the viewfinder and the camera */ ioctls.maCameraStart = delegate() { initCamera(); MoSync.Util.RunActionOnMainThreadSync(() => { mCameraPrev.StartViewFinder(); }); return 0; }; /** * stops the view finder and the camera. */ ioctls.maCameraStop = delegate() { MoSync.Util.RunActionOnMainThreadSync(() => { mCameraPrev.StopViewFinder(); }); return 0; }; /** * Adds a previewWidget to the camera controller in devices that support native UI. */ 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)) { return MoSync.Constants.MA_CAMERA_RES_FAILED; } mCameraPrev = (NativeUI.CameraPreview)w; mCameraPrev.SetViewFinderContent(mVideoBrush); return MoSync.Constants.MA_CAMERA_RES_OK; }; /** * Returns the number of available Camera on the device. */ ioctls.maCameraNumber = delegate() { if (PhotoCamera.IsCameraTypeSupported(CameraType.FrontFacing) && PhotoCamera.IsCameraTypeSupported(CameraType.Primary)) return 2; else if (PhotoCamera.IsCameraTypeSupported(CameraType.FrontFacing) || PhotoCamera.IsCameraTypeSupported(CameraType.Primary)) return 1; return 0; }; /** * Captures an image and stores it as a new data object in the * supplied placeholder. * @param _formatIndex int the required format. * @param _placeHolder int the placeholder used for storing the image. */ ioctls.maCameraSnapshot = delegate(int _formatIndex, int _placeHolder) { AutoResetEvent are = new AutoResetEvent(false); System.Windows.Size dim; if (GetCameraFormat(_formatIndex, out dim) == false) return MoSync.Constants.MA_CAMERA_RES_FAILED; mCamera.Resolution = dim; if (mCameraSnapshotDelegate != null) mCamera.CaptureImageAvailable -= mCameraSnapshotDelegate; mCameraSnapshotDelegate = delegate(object o, ContentReadyEventArgs args) { MoSync.Util.RunActionOnMainThreadSync(() => { Resource res = runtime.GetResource(MoSync.Constants.RT_PLACEHOLDER, _placeHolder); Stream data = args.ImageStream; MemoryStream dataMem = new MemoryStream((int)data.Length); MoSync.Util.CopySeekableStreams(data, 0, dataMem, 0, (int)data.Length); res.SetInternalObject(dataMem); }); are.Set(); }; mCamera.CaptureImageAvailable += mCameraSnapshotDelegate; mCamera.CaptureImage(); are.WaitOne(); return 0; }; /** * Sets the property represented by the string situated at the * _property address with the value situated at the _value address. * @param _property int the property name address * @param _value int the value address * * Note: the fallowing properties are not available on windows phone * MA_CAMERA_FOCUS_MODE, MA_CAMERA_IMAGE_FORMAT, MA_CAMERA_ZOOM, * MA_CAMERA_MAX_ZOOM. */ 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); if (property.Equals(MoSync.Constants.MA_CAMERA_FLASH_MODE)) { if (value.Equals(MoSync.Constants.MA_CAMERA_FLASH_ON)) { mCamera.FlashMode = FlashMode.On; mFlashMode = FlashMode.On; } else if (value.Equals(MoSync.Constants.MA_CAMERA_FLASH_OFF)) { mCamera.FlashMode = FlashMode.Off; mFlashMode = FlashMode.Off; } else if (value.Equals(MoSync.Constants.MA_CAMERA_FLASH_AUTO)) { mCamera.FlashMode = FlashMode.Auto; mFlashMode = FlashMode.Auto; } else return MoSync.Constants.MA_CAMERA_RES_INVALID_PROPERTY_VALUE; return MoSync.Constants.MA_CAMERA_RES_OK; } else if (property.Equals(MoSync.Constants.MA_CAMERA_FOCUS_MODE)) { return MoSync.Constants.MA_CAMERA_RES_PROPERTY_NOTSUPPORTED; } else if (property.Equals(MoSync.Constants.MA_CAMERA_IMAGE_FORMAT)) { return MoSync.Constants.MA_CAMERA_RES_PROPERTY_NOTSUPPORTED; } else if (property.Equals(MoSync.Constants.MA_CAMERA_ZOOM)) { return MoSync.Constants.MA_CAMERA_RES_PROPERTY_NOTSUPPORTED; } else if (property.Equals(MoSync.Constants.MA_CAMERA_MAX_ZOOM)) { return MoSync.Constants.MA_CAMERA_RES_PROPERTY_NOTSUPPORTED; } else return MoSync.Constants.MA_CAMERA_RES_PROPERTY_NOTSUPPORTED; }; /** * Selects a camera from the avalable ones; * in this eigther the back or the front camera is * chosen */ 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) { mCameraType = CameraType.Primary; initCamera(); } } else if (MoSync.Constants.MA_CAMERA_CONST_FRONT_CAMERA == _camera) { if (mCamera.CameraType != CameraType.FrontFacing) { mCameraType = CameraType.FrontFacing; initCamera(); } } else return MoSync.Constants.MA_CAMERA_RES_FAILED; return MoSync.Constants.MA_CAMERA_RES_OK; }; /** * Retrieves the specified property value in the given buffer. * @param _property int the address for the property string * @param _value int the address for the property value string (the buffer) * @param _bufSize int the buffer size */ ioctls.maCameraGetProperty = delegate(int _property, int _value, int _bufSize) { String property = core.GetDataMemory().ReadStringAtAddress(_property); if (property.Equals(MoSync.Constants.MA_CAMERA_MAX_ZOOM)) { core.GetDataMemory().WriteStringAtAddress( _value, "0", _bufSize); } else if (property.Equals(MoSync.Constants.MA_CAMERA_ZOOM_SUPPORTED)) { core.GetDataMemory().WriteStringAtAddress( _value, "false", _bufSize); } else if (property.Equals(MoSync.Constants.MA_CAMERA_FLASH_SUPPORTED)) { core.GetDataMemory().WriteStringAtAddress( _value, "true", _bufSize); } else return MoSync.Constants.MA_CAMERA_RES_PROPERTY_NOTSUPPORTED; return 0; }; ioctls.maCameraRecord = delegate(int _stopStartFlag) { return MoSync.Constants.MA_CAMERA_RES_FAILED; }; }
public void Init(Ioctls ioctls, Core core, Runtime runtime) { mCamera = new PhotoCamera(CameraType.Primary); mVideoBrush = new VideoBrush(); mVideoBrush.SetSource(mCamera); mVideoBrush.Stretch = Stretch.Uniform; runtime.RegisterCleaner(delegate() { mCamera.Dispose(); mCamera = null; }); // this should be set according to the orientation of // the device I guess. mVideoBrush.RelativeTransform = new CompositeTransform() { CenterX = 0.5, CenterY = 0.5, Rotation = 90 }; ioctls.maCameraFormat = delegate(int _index, int _fmt) { System.Windows.Size dim; if (GetCameraFormat(_index, out dim) == false) return MoSync.Constants.MA_CAMERA_RES_FAILED; core.GetDataMemory().WriteInt32(_fmt + MoSync.Struct.MA_CAMERA_FORMAT.width, (int)dim.Width); core.GetDataMemory().WriteInt32(_fmt + MoSync.Struct.MA_CAMERA_FORMAT.height, (int)dim.Height); return MoSync.Constants.MA_CAMERA_RES_OK; }; ioctls.maCameraFormatNumber = delegate() { IEnumerable<System.Windows.Size> res = mCamera.AvailableResolutions; if (res == null) return 0; IEnumerator<System.Windows.Size> resolutions = res.GetEnumerator(); resolutions.MoveNext(); int number = 0; while (resolutions.Current != null) { number++; } return number; }; ioctls.maCameraStart = delegate() { return 0; }; ioctls.maCameraStop = delegate() { return 0; }; ioctls.maCameraSetPreview = delegate(int _widgetHandle) { // something like // videoBrush = ((CameraViewFinder)runtime.GetModule<MoSyncNativeUIModule>.GetWidget(_widgetHandle)).GetBrush(); // videoBrush.SetSource(mCamera) IWidget w = runtime.GetModule<NativeUIModule>().GetWidget(_widgetHandle); if (w.GetType() != typeof(MoSync.NativeUI.CameraPreview)) { return MoSync.Constants.MA_CAMERA_RES_FAILED; } NativeUI.CameraPreview prev = (NativeUI.CameraPreview)w; System.Windows.Controls.Canvas canvas = prev.GetViewFinderCanvas(); MoSync.Util.RunActionOnMainThreadSync(() => { canvas.Background = mVideoBrush; }); return 0; }; ioctls.maCameraSelect = delegate(int _cameraNumber) { CameraType cameraType = CameraType.Primary; if(_cameraNumber == MoSync.Constants.MA_CAMERA_CONST_BACK_CAMERA) { cameraType = CameraType.Primary; } else if(_cameraNumber == MoSync.Constants.MA_CAMERA_CONST_FRONT_CAMERA) { cameraType = CameraType.FrontFacing; } if(mCamera==null || mCamera.CameraType != cameraType) { mCamera = new PhotoCamera(cameraType); if(mVideoBrush == null) mVideoBrush = new VideoBrush(); mVideoBrush.SetSource(mCamera); } return 0; }; ioctls.maCameraNumber = delegate() { // front facing and back facing is the standard I believe. return 2; }; ioctls.maCameraSnapshot = delegate(int _formatIndex, int _placeHolder) { AutoResetEvent are = new AutoResetEvent(false); System.Windows.Size dim; if (GetCameraFormat(_formatIndex, out dim) == false) return MoSync.Constants.MA_CAMERA_RES_FAILED; mCamera.Resolution = dim; if (mCameraSnapshotDelegate != null) mCamera.CaptureImageAvailable -= mCameraSnapshotDelegate; mCameraSnapshotDelegate = delegate(object o, ContentReadyEventArgs args) { MoSync.Util.RunActionOnMainThreadSync(() => { Resource res = runtime.GetResource(MoSync.Constants.RT_PLACEHOLDER, _placeHolder); Stream data = args.ImageStream; Memory dataMem = new Memory((int)data.Length); dataMem.WriteFromStream(0, data, (int)data.Length); res.SetInternalObject(dataMem); }); are.Set(); }; mCamera.CaptureImageAvailable += mCameraSnapshotDelegate; mCamera.CaptureImage(); are.WaitOne(); return 0; }; ioctls.maCameraSetProperty = delegate(int _property, int _value) { return 0; }; ioctls.maCameraSelect = delegate(int _camera) { return 0; }; ioctls.maCameraGetProperty = delegate(int _property, int _value, int _bufSize) { String property = core.GetDataMemory().ReadStringAtAddress(_property); if (property.Equals(MoSync.Constants.MA_CAMERA_MAX_ZOOM)) { core.GetDataMemory().WriteStringAtAddress( _value, "0", _bufSize); } return 0; }; ioctls.maCameraRecord = delegate(int _stopStartFlag) { return 0; }; }