locked
Using the Camera in your app RRS feed

  • Question

  • Guys

    is it possible to open the camera app from within your own app

    I have loked at the camera UI smple which does exactly what I want but is incredibly complicated to incorporate into my app for such a newbie as me

    at the moment I hit the windows key and then run the camera app and take a picture then go back to my app and load the picture just taken.

    would like to do all at the press of a button

    Mark

    Wednesday, October 30, 2013 9:08 AM

Answers

  • Hi ,

    I suppose you have looked at the below

    http://code.msdn.microsoft.com/windowsapps/CameraCaptureUI-Sample-845a53ac/view/SourceCode#content

    I remember in Windows 8 they had a sample where you have to select first the device , initialize it and the start using it.

    You can start by adding a button like in the below example in your XAML page

    private async void CapturePhoto_Click(object sender, RoutedEventArgs e) 
            { 
                try 
                { 
                    rootPage.NotifyUser("", NotifyType.StatusMessage); 
     
                    // Using Windows.Media.Capture.CameraCaptureUI API to capture a photo 
                    CameraCaptureUI dialog = new CameraCaptureUI(); 
                    Size aspectRatio = new Size(169); 
                    dialog.PhotoSettings.CroppedAspectRatio = aspectRatio; 
     
                    StorageFile file = await dialog.CaptureFileAsync(CameraCaptureUIMode.Photo); 
                    if (file != null) 
                    { 
                        BitmapImage bitmapImage = new BitmapImage(); 
                        using (IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.Read)) 
                        { 
                            bitmapImage.SetSource(fileStream); 
                        } 
                        CapturedPhoto.Source = bitmapImage; 
                        ResetButton.Visibility = Visibility.Visible; 
     
                        // Store the file path in Application Data 
                        appSettings[photoKey] = file.Path; 
                    } 
                    else 
                    { 
                        rootPage.NotifyUser("No photo captured.", NotifyType.StatusMessage); 
                    } 
                } 
                catch (Exception ex) 
                { 
                    rootPage.NotifyUser(ex.Message, NotifyType.ErrorMessage); 
                } 
            } 

    imagine that you have a button in your XAML page.

    You don't need  the below

     rootPage.NotifyUser

    You can create your own display warnings mechanism. 

    It is kind of difficult to write you all the code because what you are asking is a complete project.

    I suggest you should place the code from the example in a button of yours in a XAML page ,press it and see how it behaves each time.

    thank you

    Wednesday, October 30, 2013 12:15 PM
  • Hi wambaugh1,

    I'm sorry that currently VB samples for win8.1 are not available in the code center.

    VB and C# are quite same, as I know you could use some third party stuff to convert the code, try to search on the Internet. If one part of the code is not working you could always ask question here, we are warmly to help you.

    Furthermore the VB version of the Zakker's code could be:

    Private Sub CapturePhoto_Click(sender As Object, e As RoutedEventArgs)
    	Try
    		rootPage.NotifyUser("", NotifyType.StatusMessage)
    
    		' Using Windows.Media.Capture.CameraCaptureUI API to capture a photo 
    		Dim dialog As New CameraCaptureUI()
    		Dim aspectRatio As New Size(16, 9)
    		dialog.PhotoSettings.CroppedAspectRatio = aspectRatio
    
    		Dim file As StorageFile = Await dialog.CaptureFileAsync(CameraCaptureUIMode.Photo)
    		If file IsNot Nothing Then
    			Dim bitmapImage As New BitmapImage()
    			Using fileStream As IRandomAccessStream = Await file.OpenAsync(FileAccessMode.Read)
    				bitmapImage.SetSource(fileStream)
    			End Using
    			CapturedPhoto.Source = bitmapImage
    			ResetButton.Visibility = Visibility.Visible
    
    			' Store the file path in Application Data 
    			appSettings(photoKey) = file.Path
    		Else
    			rootPage.NotifyUser("No photo captured.", NotifyType.StatusMessage)
    		End If
    	Catch ex As Exception
    		rootPage.NotifyUser(ex.Message, NotifyType.ErrorMessage)
    	End Try
    End Sub

    Best Regards,

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Friday, November 8, 2013 5:52 AM
    Moderator

All replies

  • Hi ,

    I suppose you have looked at the below

    http://code.msdn.microsoft.com/windowsapps/CameraCaptureUI-Sample-845a53ac/view/SourceCode#content

    I remember in Windows 8 they had a sample where you have to select first the device , initialize it and the start using it.

    You can start by adding a button like in the below example in your XAML page

    private async void CapturePhoto_Click(object sender, RoutedEventArgs e) 
            { 
                try 
                { 
                    rootPage.NotifyUser("", NotifyType.StatusMessage); 
     
                    // Using Windows.Media.Capture.CameraCaptureUI API to capture a photo 
                    CameraCaptureUI dialog = new CameraCaptureUI(); 
                    Size aspectRatio = new Size(169); 
                    dialog.PhotoSettings.CroppedAspectRatio = aspectRatio; 
     
                    StorageFile file = await dialog.CaptureFileAsync(CameraCaptureUIMode.Photo); 
                    if (file != null) 
                    { 
                        BitmapImage bitmapImage = new BitmapImage(); 
                        using (IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.Read)) 
                        { 
                            bitmapImage.SetSource(fileStream); 
                        } 
                        CapturedPhoto.Source = bitmapImage; 
                        ResetButton.Visibility = Visibility.Visible; 
     
                        // Store the file path in Application Data 
                        appSettings[photoKey] = file.Path; 
                    } 
                    else 
                    { 
                        rootPage.NotifyUser("No photo captured.", NotifyType.StatusMessage); 
                    } 
                } 
                catch (Exception ex) 
                { 
                    rootPage.NotifyUser(ex.Message, NotifyType.ErrorMessage); 
                } 
            } 

    imagine that you have a button in your XAML page.

    You don't need  the below

     rootPage.NotifyUser

    You can create your own display warnings mechanism. 

    It is kind of difficult to write you all the code because what you are asking is a complete project.

    I suggest you should place the code from the example in a button of yours in a XAML page ,press it and see how it behaves each time.

    thank you

    Wednesday, October 30, 2013 12:15 PM
  • Hello wambaugh1,

    try this sample for how to use camera capture task: 

    http://www.c-sharpcorner.com/UploadFile/99bb20/capture-element-in-windows-store-apps-using-C-Sharp/

    Wednesday, October 30, 2013 12:19 PM
  • Hi Guys

    thanks for that I have the sample and it sort of does what I want only problem is its written in c#

    My App is in Visual Basic and I cant find an example

    Any ideas?

    Mark

    Monday, November 4, 2013 3:19 PM
  • Hi wambaugh1,

    I'm sorry that currently VB samples for win8.1 are not available in the code center.

    VB and C# are quite same, as I know you could use some third party stuff to convert the code, try to search on the Internet. If one part of the code is not working you could always ask question here, we are warmly to help you.

    Furthermore the VB version of the Zakker's code could be:

    Private Sub CapturePhoto_Click(sender As Object, e As RoutedEventArgs)
    	Try
    		rootPage.NotifyUser("", NotifyType.StatusMessage)
    
    		' Using Windows.Media.Capture.CameraCaptureUI API to capture a photo 
    		Dim dialog As New CameraCaptureUI()
    		Dim aspectRatio As New Size(16, 9)
    		dialog.PhotoSettings.CroppedAspectRatio = aspectRatio
    
    		Dim file As StorageFile = Await dialog.CaptureFileAsync(CameraCaptureUIMode.Photo)
    		If file IsNot Nothing Then
    			Dim bitmapImage As New BitmapImage()
    			Using fileStream As IRandomAccessStream = Await file.OpenAsync(FileAccessMode.Read)
    				bitmapImage.SetSource(fileStream)
    			End Using
    			CapturedPhoto.Source = bitmapImage
    			ResetButton.Visibility = Visibility.Visible
    
    			' Store the file path in Application Data 
    			appSettings(photoKey) = file.Path
    		Else
    			rootPage.NotifyUser("No photo captured.", NotifyType.StatusMessage)
    		End If
    	Catch ex As Exception
    		rootPage.NotifyUser(ex.Message, NotifyType.ErrorMessage)
    	End Try
    End Sub

    Best Regards,

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Friday, November 8, 2013 5:52 AM
    Moderator