媒体拍摄 - phone 拍摄的照片很暗

Media Capture - photos taken by phone are dark

我正在尝试制作当您单击 livetile 时拍照的应用程序。不幸的是,在智能手机上存在一些问题:此应用程序保存的照片全是黑色。我不知道我做错了什么。

事实:

  1. 此 UWP 应用可在 PC 上正常运行,问题仅出现在我的 Lumia 设备上
  2. 相机被正确检测到
  3. 这不是 livetile 的问题:无论我通过点击 livetile 还是点击按钮拍照都不起作用
  4. 并非所有照片都是全黑的。当我拍摄 window(由于阳光而明亮)的照片时,我可以看到它模糊的形状。也许照片拍对了,但不知何故变暗了?

要粘贴的代码太多,所以我决定在 GitHub 上发布整个项目。

[LINK TO GITHUB]

您知道为什么它不起作用吗?这段代码大部分是从一个教程中复制的,所以有问题很奇怪。

尝试使用此代码段(来自 FingerPaint 应用程序)在拍摄图像后更改色相或亮度:

    ImageEncodingProperties imageEncodingProps = ImageEncodingProperties.CreateJpeg();
    InMemoryRandomAccessStream memoryStream = new InMemoryRandomAccessStream();
    await _mediaCapture.CapturePhotoToStreamAsync(imageEncodingProps, memoryStream);

        BitmapDecoder decoder = await BitmapDecoder.CreateAsync(memoryStream);
        PixelDataProvider pixelProvider = await decoder.GetPixelDataAsync();
        byte[] pixels = pixelProvider.DetachPixelData();

        for (int index = 0; index < pixels.Length; index += 4)
        {
            Color color = Color.FromArgb(pixels[index + 3],
                                         pixels[index + 2],
                                         pixels[index + 1],
                                         pixels[index + 0]);
            HSL hsl = new HSL(color);
            hsl = new HSL(hsl.Hue, 1.0, hsl.Lightness);
            color = hsl.Color;

            pixels[index + 0] = color.B;
            pixels[index + 1] = color.G;
            pixels[index + 2] = color.R;
            pixels[index + 3] = color.A;
        }

        WriteableBitmap bitmap = new WriteableBitmap((int)decoder.PixelWidth, 
                                                     (int)decoder.PixelHeight);
        Stream pixelStream = bitmap.PixelBuffer.AsStream();
        await pixelStream.WriteAsync(pixels, 0, pixels.Length);
        bitmap.Invalidate();

        image.Source = bitmap;

我找到了灵魂:

初始化MediaCapture后拍照前,需要创建CaptureElement并开始预览。

var captureElement = new CaptureElement();
captureElement.Source = _mediaCapture;

await _mediaCapture.StartPreviewAsync();

Why you need to start previewing

dark pictures are often due to the lack of video preview. Camera drivers use the preview stream to run their 3A algorithms (auto-whitebalance/focus/exposure).

This error [that is shown when you use StartPreviewAsync without CaptureElement] occurs because currently StartPreviewAsync requires a sink to output frames to. This can be fixed by creating a capture element in xaml to display the frames.