我使用以下代码从相机捕获图像。但是我不能用这个录制视频
I use the following code to capture image from camera. But I cant record the video by this
CameraCaptureUI capture = new CameraCaptureUI();
capture.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Jpeg;
capture.PhotoSettings.CroppedAspectRatio = new Size(1, 2);
capture.PhotoSettings.MaxResolution=CameraCaptureUIMaxPhotoResolution.HighestAvailable
StorageFile storeFile=await capture.CaptureFileAsync(CameraCaptureUIMode.Photo);
if (storeFile != null)
{
var stream = await storeFile.OpenAsync(FileAccessMode.Read);
BitmapImage bimage = new BitmapImage();
bimage.SetSource(stream);
Image imageitem = new Image();
imageitem.Source = bimage;
my_canvas.Children.Add(imageitem);
只需使用标准样本录制来自Capture photos and video with CameraCaptureUI
的视频
CameraCaptureUI captureUI = new CameraCaptureUI();
captureUI.VideoSettings.Format = CameraCaptureUIVideoFormat.Mp4;
StorageFile videoFile = await captureUI.CaptureFileAsync(CameraCaptureUIMode.Video);
if (videoFile == null)
{
// User cancelled photo capture
return;
}
你也可以用MediaCapture来录制视频,这是我做过的一个项目的摘录(其中一些是我刚刚凭记忆写的,如果需要的话我回家后会更正):
public class CameraController
{
private MediaCapture _mediaCap;
private bool _isInitialised;
public async Task InitialiseWebCam()
{
if (!_isInitialised)
{
var settings = ApplicationData.Current.LocalSettings;
string preferredDeviceName = $"{settings.Values["PreferredDeviceName"]}";
var videoDevices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
DeviceInformation device = videoDevices.FirstOrDefault(x => x.Name == preferredDeviceName);
if (device == null)
device = videoDevices.FirstOrDefault();
if (device == null)
throw new Exception("Cannot find a camera device");
else
{
//initialize the WebCam via MediaCapture object
_mediaCap = new MediaCapture();
var initSettings = new MediaCaptureInitializationSettings { VideoDeviceId = device.Id };
await _mediaCap.InitializeAsync(initSettings);
_mediaCap.Failed += new MediaCaptureFailedEventHandler(MediaCaptureFailed);
_isInitialised = true;
}
}
}
public async StorageFile RecordVideo(TimeSpan duration)
{
if (!_isInitialised)
await InitialiseWebCam();
StorageFile videoFile = await KnownFolders.VideosLibrary.CreateFileAsync(
$"video_{DateTime.Now.ToString("yyyyMMddHHmmss")}.mp4", CreationCollisionOption.GenerateUniqueName);
var mediaEncoding = MediaEncodingProfile.CreateMp4(VideoEncodingQuality.Vga);
await _mediaCap.StartRecordToStorageFileAsync(mediaEncoding, videoFile);
await Task.Delay(duration);
await _mediaCap.StopRecordAsync();
return videoFile;
}
private void MediaCaptureFailed(MediaCapture sender, MediaCaptureFailedEventArgs errorEventArgs)
{
//TODO: Implement this
}
}
CameraCaptureUI capture = new CameraCaptureUI();
capture.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Jpeg;
capture.PhotoSettings.CroppedAspectRatio = new Size(1, 2);
capture.PhotoSettings.MaxResolution=CameraCaptureUIMaxPhotoResolution.HighestAvailable
StorageFile storeFile=await capture.CaptureFileAsync(CameraCaptureUIMode.Photo);
if (storeFile != null)
{
var stream = await storeFile.OpenAsync(FileAccessMode.Read);
BitmapImage bimage = new BitmapImage();
bimage.SetSource(stream);
Image imageitem = new Image();
imageitem.Source = bimage;
my_canvas.Children.Add(imageitem);
只需使用标准样本录制来自Capture photos and video with CameraCaptureUI
的视频CameraCaptureUI captureUI = new CameraCaptureUI();
captureUI.VideoSettings.Format = CameraCaptureUIVideoFormat.Mp4;
StorageFile videoFile = await captureUI.CaptureFileAsync(CameraCaptureUIMode.Video);
if (videoFile == null)
{
// User cancelled photo capture
return;
}
你也可以用MediaCapture来录制视频,这是我做过的一个项目的摘录(其中一些是我刚刚凭记忆写的,如果需要的话我回家后会更正):
public class CameraController
{
private MediaCapture _mediaCap;
private bool _isInitialised;
public async Task InitialiseWebCam()
{
if (!_isInitialised)
{
var settings = ApplicationData.Current.LocalSettings;
string preferredDeviceName = $"{settings.Values["PreferredDeviceName"]}";
var videoDevices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
DeviceInformation device = videoDevices.FirstOrDefault(x => x.Name == preferredDeviceName);
if (device == null)
device = videoDevices.FirstOrDefault();
if (device == null)
throw new Exception("Cannot find a camera device");
else
{
//initialize the WebCam via MediaCapture object
_mediaCap = new MediaCapture();
var initSettings = new MediaCaptureInitializationSettings { VideoDeviceId = device.Id };
await _mediaCap.InitializeAsync(initSettings);
_mediaCap.Failed += new MediaCaptureFailedEventHandler(MediaCaptureFailed);
_isInitialised = true;
}
}
}
public async StorageFile RecordVideo(TimeSpan duration)
{
if (!_isInitialised)
await InitialiseWebCam();
StorageFile videoFile = await KnownFolders.VideosLibrary.CreateFileAsync(
$"video_{DateTime.Now.ToString("yyyyMMddHHmmss")}.mp4", CreationCollisionOption.GenerateUniqueName);
var mediaEncoding = MediaEncodingProfile.CreateMp4(VideoEncodingQuality.Vga);
await _mediaCap.StartRecordToStorageFileAsync(mediaEncoding, videoFile);
await Task.Delay(duration);
await _mediaCap.StopRecordAsync();
return videoFile;
}
private void MediaCaptureFailed(MediaCapture sender, MediaCaptureFailedEventArgs errorEventArgs)
{
//TODO: Implement this
}
}