媒体捕获 - 手机拍摄的照片很暗

时间:2016-05-04 15:03:40

标签: c# windows win-universal-app windows-10 windows-10-universal

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

事实:

  1. 此UWP应用程序正常在PC上运行,问题仅出现在我的Lumia设备上
  2. 正确检测到相机
  3. 这不是问题:无论我通过点击活页或点击按钮拍照,它都不起作用
  4. 并非所有照片都是全黑的。当我拍摄一张窗户(由于阳光而明亮)的照片时,我可以看到它模糊的形状。也许照片拍得正确,但是它们有点暗淡了?
  5. 要粘贴的代码太多,所以我决定在GitHub上发布整个项目。

    [LINK TO GITHUB]

    你有什么想法,为什么它不起作用?这段代码主要是从一个教程中复制过来的,所以很奇怪这个问题。

2 个答案:

答案 0 :(得分:2)

我找到了灵魂:

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

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

await _mediaCapture.StartPreviewAsync();

Why you need to start previewing

  

黑暗图片通常是由于缺乏视频预览。相机驱动程序使用预览流来运行其3A算法(自动白平衡/聚焦/曝光)。

Why you need to create Capture Element

  

此错误[在使用不带CaptureElement的StartPreviewAsync时显示]因为当前的StartPreviewAsync需要接收器来输出帧。这可以通过在xaml中创建捕获元素来显示帧来修复。

答案 1 :(得分:0)

尝试使用此代码段(来自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;
相关问题