【问题标题】:Taking photo in C++/CX with Windows Phone 8.1使用 Windows Phone 8.1 在 C++/CX 中拍照
【发布时间】:2015-10-31 23:11:25
【问题描述】:

我正在使用 C++/CX(在 Visual Studio 2013 中)开发适用于 Windows phone 8.1 系统的应用程序。我在诺基亚 Lumia 930 上测试我的应用程序。我的应用程序必须拍照才能对它们进行一些图像处理。因此,我想拥有一个byte*char*unsigned char* 来满足我的任何需求。所以我尝试使用::Windows::Media::Capture::MediaCapture::CapturePhotoToStreamAsync 将照片的内容放入流中,然后使用::Windows::Storage::Streams::DataReader 获取照片的实际字节。但我一直没有成功。更确切地说,问题出在下面的代码中。

IRandomAccessStream ^ras = ref new InMemoryRandomAccessStream();
IAsyncAction ^ac = cap->CapturePhotoToStreamAsync(ImageEncodingProperties::CreateBmp(),ras);
ac->Completed = ref new AsyncActionCompletedHandler(
[=] (IAsyncAction ^async_op,AsyncStatus status) mutable
{
    debug->Text = L"finished";
});

其中cap 是指向似乎已正确初始化的 MediaCapture 的托管指针。

debug TextBlock 永远不会显示finished。当我获得ac 的状态时,它似乎永远被Started 阻止,它永远不会到达ErrorCompleted。我不理解为什么。所以现在我实现了一个使用::Windows::Media::Capture::MediaCapture::CapturePhotoToStorageFileAsync 的肮脏解决方法。但是将照片存储在文件上的位图中,然后将其读回以将图像恢复到内存中,这一点并不令人满意。

我发现很多关于使用 Windows Phone 8.1 拍照的方法,但它们都使用 C# 并且通过将 '.' 替换为 '::' 来复制粘贴这些解决方案不起作用。我的意思是(至少对我而言)将 C# 代码改编为 C++ 代码并不容易。

所以我的问题是:

  1. 有没有一种快速和/或干净的方法来获取指向图像像素的旧 C 指针?

  2. 为什么CapturePhotoToStreamAsync 阻止状态为Started

感谢您的回答和您的时间。

【问题讨论】:

  • 不是“随便”;正确标记您的问题
  • 好的,现在标记正确。
  • 这不是C++/CLI,语言扩展叫做C++/CX。它是纯本机代码。使用sample code
  • 好吧,我把 CLI 改成 Cx

标签: windows-phone-8.1 c++-cx


【解决方案1】:

查看 Microsoft Github 页面上的基本相机示例应用程序:https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/CameraStarterKit

在拍照之前您需要做很多事情(初始化、配置、开始预览等),示例将引导您完成。

这是我在上面链接的示例中缩短的 sn-p:

/// <summary>
/// Takes a photo to a StorageFile and adds rotation metadata to it
/// </summary>
/// <returns></returns>
task<void> MainPage::TakePhotoAsync()
{   
    auto inputStream = ref new Streams::InMemoryRandomAccessStream();

    // Take the picture
    WriteLine("Taking photo...");
    return create_task(_mediaCapture->CapturePhotoToStreamAsync(Windows::Media::MediaProperties::ImageEncodingProperties::CreateJpeg(), inputStream))
        .then([this, inputStream]()
    {
        WriteLine("Photo taken!");

        auto photoOrientation = ConvertOrientationToPhotoOrientation(GetCameraOrientation());
        return ReencodeAndSavePhotoAsync(inputStream, photoOrientation);
    }).then([this](task<void> previousTask)
    {
        try
        {
            previousTask.get();
        }
        catch (Exception^ ex)
        {
            WriteException(ex);
        }
    });
}

【讨论】:

  • 您好,我刚刚尝试了您的解决方案,但它对我不起作用。我有我在问题中描述的行为。 CapturePhotoToStreamAsync 似乎被 Status == Started 阻止。它永远不会转到ErrorCompleted,因此创建的任务永远不会完成。我想我在拍照之前做了所有需要做的事情,因为当我使用 CapturePhotoToStorageFileAsync 而不是 CapturePhotoToStreamAsync 时,一切正常。你知道为什么CapturePhotoToStreamAsync 会这样阻塞而CapturePhotoToStorageFileAsync 正在工作吗?谢谢
  • 把你的ImageEncodingProperties::CreateBmp()改成ImageEncodingProperties::CreateJpeg(),就像我的sn-p一样。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多