Created
January 28, 2017 07:47
-
-
Save 3t14/edd708580e955b49dfc01a54349862be to your computer and use it in GitHub Desktop.
拡張版
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Plugin.Media; | |
using Xamarin.Forms; | |
namespace HelloCamera | |
{ | |
public class App : Application | |
{ | |
Image photoImage; | |
public App() | |
{ | |
photoImage = new Image | |
{ | |
}; | |
var button = new Button | |
{ | |
Text = "写真撮影する" | |
}; | |
var albumButton = new Button | |
{ | |
Text = "アルバムから選択" | |
}; | |
// The root page of your application | |
var content = new ContentPage | |
{ | |
Title = "HelloCamera", | |
Content = new StackLayout | |
{ | |
VerticalOptions = LayoutOptions.Center, | |
Children = { | |
new Label { | |
HorizontalTextAlignment = TextAlignment.Center, | |
Text = "写真撮影サンプル" | |
}, | |
button, | |
albumButton, | |
photoImage | |
} | |
} | |
}; | |
albumButton.Clicked += async (sender, e) => | |
{ | |
// 写真撮影の処理を入れる | |
await CrossMedia.Current.Initialize(); | |
if (!CrossMedia.Current.IsPickPhotoSupported) | |
{ | |
await content.DisplayAlert( | |
"警告", | |
"アルバムにアクセスできません", | |
"OK"); | |
} | |
var file = await CrossMedia.Current.PickPhotoAsync( | |
new Plugin.Media.Abstractions.PickMediaOptions | |
{ | |
PhotoSize = Plugin.Media.Abstractions.PhotoSize.Custom, | |
CustomPhotoSize = 50, | |
}); | |
// | |
if (file == null) return; | |
// | |
photoImage.Source = ImageSource.FromStream(() => | |
{ | |
var stream = file.GetStream(); | |
file.Dispose(); | |
return stream; | |
}); | |
}; | |
button.Clicked += async (sender, e) => | |
{ | |
// 写真撮影の処理を入れる | |
await CrossMedia.Current.Initialize(); | |
if (!CrossMedia.Current.IsCameraAvailable | |
|| !CrossMedia.Current.IsTakePhotoSupported) | |
{ | |
await content.DisplayAlert( | |
"警告", | |
"カメラデバイスが使えません", | |
"OK"); | |
return; | |
} | |
var file = await CrossMedia.Current.TakePhotoAsync( | |
new Plugin.Media.Abstractions.StoreCameraMediaOptions | |
{ | |
Directory = "Photos", | |
Name = "sample.jpg", | |
PhotoSize = Plugin.Media.Abstractions.PhotoSize.Custom, | |
CustomPhotoSize = 20, | |
DefaultCamera = | |
Plugin.Media.Abstractions.CameraDevice.Rear | |
}); | |
// | |
if (file == null) return; | |
// | |
photoImage.Source = ImageSource.FromStream(() => | |
{ | |
var stream = file.GetStream(); | |
file.Dispose(); | |
return stream; | |
}); | |
}; | |
MainPage = new NavigationPage(content); | |
} | |
protected override void OnStart() | |
{ | |
// Handle when your app starts | |
} | |
protected override void OnSleep() | |
{ | |
// Handle when your app sleeps | |
} | |
protected override void OnResume() | |
{ | |
// Handle when your app resumes | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment