3 - working with the phone, camera and photos (3t)

39
DONG NAI UNIVERSITY OF TECHNOLOGY 1 6. Extending the Photos Hub 5. Capturing photos 3. Acquiring a single photo 2. Search extensibility 1. Launchers and Choosers 7. Lenses 8. Sharing photos 4. Working with the media library

Upload: neversayloveyou

Post on 19-Jan-2016

24 views

Category:

Documents


0 download

DESCRIPTION

3 - Working With the Phone, Camera and Photos (3t)

TRANSCRIPT

Page 1: 3 - Working With the Phone, Camera and Photos (3t)

1

DONG NAI UNIVERSITY OF TECHNOLOGY

6. Extending the Photos Hub

5. Capturing photos

3. Acquiring a single photo

2. Search extensibility

1. Launchers and Choosers

7. Lenses

8. Sharing photos

4. Working with the media library

Page 2: 3 - Working With the Phone, Camera and Photos (3t)

2

DONG NAI UNIVERSITY OF TECHNOLOGY

1. Launchers and Choosers

Both Launchers and Choosers are API mechanisms for invoking existing apps and services on the phone. In the API, they’re all called “tasks.”

The difference between Launchers and Choosers is that Launchers start a feature but don’t return a value, whereas Choosers launch a feature and do return a value.

Invoking a Launcher or Chooser causes your app (the invoking app) to be deactivated.

using Microsoft.Phone.Tasks;

Page 4: 3 - Working With the Phone, Camera and Photos (3t)

4

DONG NAI UNIVERSITY OF TECHNOLOGY

1. Launchers and Choosers

Launchers

Task Description

BingMapsDirectionsTask Launches the Maps app, specifying a starting and/or ending location, for which driving or walking directions are displayed (identical to the MapsDirectionsTask).

BingMapsTask Launches the Maps app centered at the specified or current location (identical to the MapsTask)

ConnectionSettingsTask Launches a settings dialog with which the user can change the device’s connection settings

EmailComposeTask Composes a new email

MapDownloaderTask Provides a mechanism for the user to download maps for offline use

MapUpdaterTask Provides a mechanism for the user to update map data he has previously downloaded for offline use.

Page 5: 3 - Working With the Phone, Camera and Photos (3t)

5

DONG NAI UNIVERSITY OF TECHNOLOGY

1. Launchers and Choosers

Launchers

Task Description

MapsDirectionsTask Launches the Maps app, specifying a starting and/or ending location, for which driving or walking directions are displayed (identical to BingMapsDirectionsTask).

MapsTask Launches the Maps app centered at the specified or current location (identical to BingMapsTask)

MarketplaceDetailTask Launches the Windows Phone Store and displays the details for a specific app

MarketplaceHubTask Launches the Windows Phone Store and searches for a particular type of content

MarketplaceReviewTask Displays the Windows Phone Store review page for the current app

MarketplaceSearchTask Launches the Windows Phone Store and displays the search results from the specified search terms.

Page 6: 3 - Working With the Phone, Camera and Photos (3t)

6

DONG NAI UNIVERSITY OF TECHNOLOGY

1. Launchers and Choosers

Launchers

Task Description

MediaPlayerLauncher Launches Media Player, and plays the specified media file

PhoneCallTask Initiates a phone call to a specified number.

SaveAppointmentTask Provides a mechanism for the user to save an appointment from your app

SearchTask Launches Microsoft Bing Search with a specified search term

ShareLinkTask Launches a dialog with which the user can share a link on the social networks of her choice. If the user does not have any social networks set up, the Launcher silently fails

ShareMediaTask Provides a mechanism for your app to share a media item with one of the media-sharing apps on the phone

Page 7: 3 - Working With the Phone, Camera and Photos (3t)

7

DONG NAI UNIVERSITY OF TECHNOLOGY

1. Launchers and Choosers

Launchers

Task Description

ShareStatusTask Launches a dialog with which the user can share a status message on the social networks of her choice. If the user does not have any social networks set up, the Launcher silently fails

SmsComposeTask Composes a new text message.

WebBrowserTask Launches Microsoft Internet Explorer and browses to a specific URI

Page 8: 3 - Working With the Phone, Camera and Photos (3t)

8

DONG NAI UNIVERSITY OF TECHNOLOGY

1. Launchers and Choosers

Choosers

Task Description

AddWalletItemTask Launches the Wallet app and allows the user to add the supplied item to his wallet

AddressChooserTask Launches the Contacts app with which the user can find an address.

CameraCaptureTask Opens the Camera app to take a photo

EmailAddressChooserTask Provides a mechanism for the user to select an email address from his Contacts List.

GameInviteTask Shows the game invite screen with which the user can invite players to a multiplayer game session.

PhoneNumberChooserTask Provides a mechanism for the user to select a phone number from his Contacts List

Page 9: 3 - Working With the Phone, Camera and Photos (3t)

9

DONG NAI UNIVERSITY OF TECHNOLOGY

1. Launchers and Choosers

Choosers

Task Description

PhotoChooserTask Provides a mechanism for the user to select an image from his Picture Gallery or take a photo

SaveContactTask Launches the Contacts app with which the user can save a contact.

SaveEmailAddressTask Saves an email address to an existing or new contact.

SavePhoneNumberTask Saves a phone number to an existing or new contact

SaveRingtoneTask Launches the Ringtones app with which the user can save a ringtone from your app to the system ringtones list

Page 10: 3 - Working With the Phone, Camera and Photos (3t)

10

DONG NAI UNIVERSITY OF TECHNOLOGY

1. Launchers and Choosers

The basic steps for using a Launcher are as follows:

1. Create an instance of the type that represents the

specific Launcher feature that you want to use.

2. Set properties on the object as appropriate.

3. Invoke the Show method

Page 11: 3 - Working With the Phone, Camera and Photos (3t)

11

DONG NAI UNIVERSITY OF TECHNOLOGY

1. Launchers and Choosers

The basic steps for using a Launcher are as follows:

Simple Example for SMS message:

SmsComposeTask smsTask = new SmsComposeTask(); smsTask.To = "0987773061"; smsTask.Body = "Alô Năm M i ! m i h n năm cũ"ớ ớ ơ ; smsTask.Show();

Page 12: 3 - Working With the Phone, Camera and Photos (3t)

12

DONG NAI UNIVERSITY OF TECHNOLOGY

1. Launchers and Choosers

The basic steps for using a Chooser are as follows:

1. Create an instance of the type that represents the specific

Chooser feature that you want to use. It is common to declare

this as a field in your page class.

2. Hook up the Completed event on the object (or provide an

inline delegate or lambda), which will be invoked when the

Chooser task completes.3. Set properties on the object as appropriate.4. Invoke the Show method.5. In your Completed event handler or delegate, process the return value from the Chooser.

Page 13: 3 - Working With the Phone, Camera and Photos (3t)

13

DONG NAI UNIVERSITY OF TECHNOLOGY

1. Launchers and Choosers

The basic steps for using a Chooser are as follows:

Simple Example for PhoneNumberChooserTask :PhoneNumberChooserTask pnChooserTask;

private void Button_Click_1(object sender, RoutedEventArgs e) { pnChooserTask = new PhoneNumberChooserTask(); pnChooserTask.Completed += new EventHandler<PhoneNumberResult>(pnChooserTask_Completed); pnChooserTask.Show(); }

Page 14: 3 - Working With the Phone, Camera and Photos (3t)

14

DONG NAI UNIVERSITY OF TECHNOLOGY

1. Launchers and Choosers

void pnChooserTask_Completed(object sender, PhoneNumberResult e) {if (e.TaskResult == TaskResult.OK) { MessageBox.Show("The phone number for " + e.DisplayName + " is " + e.PhoneNumber);

PhoneCallTask phoneCallTask = new PhoneCallTask(); phoneCallTask.DisplayName = e.DisplayName; phoneCallTask.PhoneNumber = e.PhoneNumber; phoneCallTask.Show(); } }

PhoneCallTask is protected by the ID_CAP_PHONEDIALER capability

Page 15: 3 - Working With the Phone, Camera and Photos (3t)

15

DONG NAI UNIVERSITY OF TECHNOLOGY

2. Search extensibility

Another way that your app can communicate with standard phone services is by extending the Bing search experience with custom behavior, integrating your app seamlessly with the search results.

There are two ways by which you can extend the Bing search behavior in your apps: App Connect and App Instant Answer. With both features, you can set up your app so that it shows up in the Bing search results when the user taps the hardware Search button

Please visit this link to learn more: http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh286420(v=vs.105).aspx

Page 16: 3 - Working With the Phone, Camera and Photos (3t)

16

DONG NAI UNIVERSITY OF TECHNOLOGY

2. Search extensibility

Requirement App Connect App Instant Answer

WMAppManifest.xml Requires Extensions entries for each Bing category that you want to extend

No specific changes required

Extras.xml Required. Specifies captions to be used in the search results apps pivot item.

Not used

UriMapper Recommended. Allows you to re-route to a specific page on app startup.

Not required

Target page You can re-route to multiple different pages, depending on the search item, if you want.

No option. Your app is launched as normal, with its default startup page

Page 17: 3 - Working With the Phone, Camera and Photos (3t)

17

DONG NAI UNIVERSITY OF TECHNOLOGY

2. Search extensibility

Requirement App Connect App Instant Answer

Query string You should parse the incoming query string for categories and item names for which you want to provide extensions

You should parse the incoming query string for the bing_query value.

Search connection Bing includes your app in the search results when the user’s search matches the categories for which you registered extensions.

Bing includes your app in the search results, based on whether it thinks the query is relevant to your app.

Page 18: 3 - Working With the Phone, Camera and Photos (3t)

18

DONG NAI UNIVERSITY OF TECHNOLOGY

2. Search extensibility

The App Connect extensibility model

Page 19: 3 - Working With the Phone, Camera and Photos (3t)

19

DONG NAI UNIVERSITY OF TECHNOLOGY

2. Search extensibility

The App Instant Answer extensibility model

Page 20: 3 - Working With the Phone, Camera and Photos (3t)

20

DONG NAI UNIVERSITY OF TECHNOLOGY

3. Acquiring a single photo

Many apps need to provide the option to acquire a single photo from the user, either selected from the media library or captured by the camera. Windows Phone offers built-in tasks that make this process simple: PhotoChooserTask and CameraChooserTask

using Microsoft.Phone.Tasks;using System.Windows.Media.Imaging;

Page 21: 3 - Working With the Phone, Camera and Photos (3t)

21

DONG NAI UNIVERSITY OF TECHNOLOGY

3. Acquiring a single photo

PhotoChooserTask photoChooser = new PhotoChooserTask();photoChooser.Completed += photoChooser_Completed;photoChooser.Show();

private void photoChooser_Completed(object sender, PhotoResult e) { if (e.TaskResult != TaskResult.OK) { return; } BitmapImage image = new BitmapImage(); image.SetSource(e.ChosenPhoto); myimage.Source = image; }

Page 22: 3 - Working With the Phone, Camera and Photos (3t)

22

DONG NAI UNIVERSITY OF TECHNOLOGY

4. Working with the media library

The Media Library APIs in the XNA framework facilitate deeper integration with the user’s photos collection

using Microsoft.Xna.Framework.Media;

Reading photos

Adding new images

Page 23: 3 - Working With the Phone, Camera and Photos (3t)

23

DONG NAI UNIVERSITY OF TECHNOLOGY

4. Working with the media library

This list is populated in the OnNavigatedTo method of the AlbumListPage. All of the photo albums available on the device are found under the RootPictureAlbum of the MediaLibrary.

protected override void OnNavigatedTo(NavigationEventArgs e) { using (MediaLibrary mLib = new MediaLibrary()) { mylistbox.ItemsSource = mLib.RootPictureAlbum.Albums; } }

ID_CAP_MEDIALIB_PHOTO capability

Page 24: 3 - Working With the Phone, Camera and Photos (3t)

24

DONG NAI UNIVERSITY OF TECHNOLOGY

4. Working with the media library

The Pictures property returns all of the photos in the user’s library as a single collection, whereas the RootPictureAlbum property returns a collection of PictureAlbum instances, which correspond to the individual user photo albums:

foreach (PictureAlbum picAlbum in mediaLibrary.RootPictureAlbum.Albums){ foreach (Picture pic in picAlbum.Pictures) { BitmapImage bitMap = new BitmapImage(); bitMap.SetSource(pic.GetImage()); Image img = new Image();//Process Image here img.Source = bitMap; }}

Page 25: 3 - Working With the Phone, Camera and Photos (3t)

25

DONG NAI UNIVERSITY OF TECHNOLOGY

4. Working with the media libraryint pictureCollectionSize;PictureCollection pictureCollection;public void createPictureSlideShow() { slideshowPivot.Items.Clear(); pictureCollection = picAlbum.Pictures; pictureCollectionSize = pictureCollection.Count; for (int i = 0; i < pictureCollectionSize; i++) { PivotItem pitem = new PivotItem(); pitem.Header = "Hình " + (i + 1); BitmapImage bitMap = new BitmapImage(); bitMap.SetSource(pictureCollection[i].GetImage()); Image img = new Image(); img.Source = bitMap; img.Stretch = Stretch.Fill; pitem.Content = img; slideshowPivot.Items.Add(pitem); } }

Page 26: 3 - Working With the Phone, Camera and Photos (3t)

26

DONG NAI UNIVERSITY OF TECHNOLOGY

4. Working with the media library

Adding new images

var mediaLibrary = new MediaLibrary();var targetJpegStream = new MemoryStream();

grayscaleImage.SaveJpeg(targetJpegStream, grayscaleImage.PixelWidth,grayscaleImage.PixelHeight, 0, 100);

targetJpegStream.Seek(0, SeekOrigin.Begin);

string grayscaleFilename = "gray_hinh_moi";mediaLibrary.SavePicture(grayscaleFilename, targetJpegStream);

Page 27: 3 - Working With the Phone, Camera and Photos (3t)

27

DONG NAI UNIVERSITY OF TECHNOLOGY

5. Capturing photos

PhotoCamera in Microsoft.Devices namespace and PhotoCaptureDevice in Windows.Phone.Media.Capture

To build the photo preview and capture experience directly in the app, enabling camera apps that go beyond the built-in functionality.

Capabilities:ID_CAP_ISV_CAMERA ,ID_CAP_MEDIALIB_PHOTO

Page 28: 3 - Working With the Phone, Camera and Photos (3t)

28

DONG NAI UNIVERSITY OF TECHNOLOGY

5. Capturing photos

PhotoCamera class

Using VideoBrush to display Camera

Tab on surface to capture photos

MainPage.xaml

Page 29: 3 - Working With the Phone, Camera and Photos (3t)

29

DONG NAI UNIVERSITY OF TECHNOLOGY

5. Capturing photos

PhotoCamera class

Coding MainPage

using Microsoft.Devices;using Microsoft.Xna.Framework.Media;using System.Windows.Media;

PhotoCamera camera;protected override void OnNavigatedTo(NavigationEventArgs e){ base.OnNavigatedTo(e); camera = new PhotoCamera(CameraType.Primary); camera.Initialized += camera_Initialized; viewFinderVideoBrush.SetSource(camera);}

Page 30: 3 - Working With the Phone, Camera and Photos (3t)

30

DONG NAI UNIVERSITY OF TECHNOLOGY

5. Capturing photos

PhotoCamera class

Coding MainPage

void camera_Initialized(object sender, CameraOperationCompletedEventArgs e){ if (e.Succeeded) { //Press Camera Button on Real Device CameraButtons.ShutterKeyPressed += CameraButtons_ShutterKeyPressed; //Available to capture photo to library camera.CaptureImageAvailable += camera_CaptureImageAvailable; camera.FlashMode = FlashMode.Auto; } }

Page 31: 3 - Working With the Phone, Camera and Photos (3t)

31

DONG NAI UNIVERSITY OF TECHNOLOGY

5. Capturing photos

PhotoCamera class

Coding MainPage

void CameraButtons_ShutterKeyPressed(object sender, EventArgs e) { try { //User pressed the Camera Button on Physical device camera.CaptureImage(); } catch (InvalidOperationException ex) { } }Tap event on the canvas:private void viewFinderCanvas_Tap(object sender, System.Windows.Input.GestureEventArgs e){//Call CaptureImage method here}

Page 32: 3 - Working With the Phone, Camera and Photos (3t)

32

DONG NAI UNIVERSITY OF TECHNOLOGY

5. Capturing photos

PhotoCamera class

Coding MainPage

Auto Save Picture To Camera Roll if Image is Availablevoid camera_CaptureImageAvailable(object sender, ContentReadyEventArgs e) { string path = "picture" + DateTime.Now.ToString("YYYYMMDDHHMMSSmmmm") + ".jpg"; MediaLibrary library = new MediaLibrary(); library.SavePictureToCameraRoll(path, e.ImageStream);

}

Page 33: 3 - Working With the Phone, Camera and Photos (3t)

33

DONG NAI UNIVERSITY OF TECHNOLOGY

5. Capturing photos

PhotoCamera class

Coding MainPage

Auto change Camera Orientation If device is rotatedprotected override void OnOrientationChanged (OrientationChangedEventArgs e) { if (Orientation == PageOrientation.LandscapeRight) { viewFinderVideoBrush.RelativeTransform = new CompositeTransform() { CenterX = 0.5, CenterY = 0.5, Rotation = 180 }; } else { viewFinderVideoBrush.RelativeTransform = new CompositeTransform() { CenterX = 0.5, CenterY = 0.5, Rotation = 0 }; } base.OnOrientationChanged(e); }

Page 34: 3 - Working With the Phone, Camera and Photos (3t)

34

DONG NAI UNIVERSITY OF TECHNOLOGY

6. Extending the Photos Hub

Apps pivot

The simplest form of Photos Hub extensibility is the ability to be included in the apps pivot

<Extensions> <Extension ExtensionName="Photos_Extra_Hub"ConsumerID="{5B04B775-356B-4AA0-AAF8-6491FFEA5632}"TaskID="_default" /></Extensions>

Page 35: 3 - Working With the Phone, Camera and Photos (3t)

35

DONG NAI UNIVERSITY OF TECHNOLOGY

7. Lenses

Lenses are camera apps that can be launched directly from the built-in camera app and which naturally complement it by using the camera sensor to provide a specialized experience

Page 36: 3 - Working With the Phone, Camera and Photos (3t)

36

DONG NAI UNIVERSITY OF TECHNOLOGY

8. Sharing photos

Users don’t want to just take photos; they want to share them, too. With Windows Phone, apps can participate in this sharing flow, whether the user is explicitly sharing a photo through the ShareMediaTask or automatically uploading them to the cloud by using auto-upload

Page 37: 3 - Working With the Phone, Camera and Photos (3t)

37

DONG NAI UNIVERSITY OF TECHNOLOGY

8. Sharing photos

<Capability Name="ID_CAP_MEDIALIB_PHOTO" />

<Extensions> <Extension ExtensionName="Photos_Extra_Share" ConsumerID= "{5B04B775-356B-4AA0-AAF8-6491FFEA5632}" TaskID="_default" /></Extensions>

WMAppManifest.xml

Page 38: 3 - Working With the Phone, Camera and Photos (3t)

38

DONG NAI UNIVERSITY OF TECHNOLOGY

8. Sharing photos

protected override void OnNavigatedTo(NavigationEventArgs e) {IDictionary<string, string> queryStrings = this.NavigationContext.QueryString;if (queryStrings.ContainsKey("FileId")) { MediaLibrary library = new MediaLibrary(); Picture photoFromLibrary = library.GetPictureFromToken(queryStrings["FileId"]); BitmapImage bitmapFromPhoto = new BitmapImage(); bitmapFromPhoto.SetSource(photoFromLibrary.GetPreviewImage()); image1.Source = bitmapFromPhoto; } }

Coding MainPage

Page 39: 3 - Working With the Phone, Camera and Photos (3t)

39

DONG NAI UNIVERSITY OF TECHNOLOGY

END