繁体   English   中英

从相机拍摄的图像中添加文字

[英]Add text to image from camera capture

我正在尝试创建一个Metro应用程序,该应用程序将从平板电脑相机捕获图像。 但是,在捕获时,我想在图像上覆盖一个日期戳。

/// <summary>
    /// This is the click handler for the 'CaptureButton' button.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private async void CapturePhoto_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            rootPage.NotifyUser("", NotifyType.StatusMessage);

            // Using Windows.Media.Capture.CameraCaptureUI API to capture a photo
            CameraCaptureUI dialog = new CameraCaptureUI();
            Size aspectRatio = new Size(16, 9);
            dialog.PhotoSettings.CroppedAspectRatio = aspectRatio;

            StorageFile file = await dialog.CaptureFileAsync(CameraCaptureUIMode.Photo);
            if (file != null)
            {
                BitmapImage bitmapImage = new BitmapImage();
                using (IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.Read))
                {
                    bitmapImage.SetSource(fileStream);
                }

                // add code for text overlay here


                CapturedPhoto.Source = bitmapImage;
                ResetButton.Visibility = Visibility.Visible;

                // Store the file path in Application Data
                appSettings[photoKey] = file.Path;
            }
            else
            {
                rootPage.NotifyUser("No photo captured.", NotifyType.StatusMessage);
            }
        }
        catch (Exception ex)
        {
            rootPage.NotifyUser(ex.Message, NotifyType.ErrorMessage);
        }
    }

通常,在要使用的图像上添加文本。

Graphics g = Graphics.FromImage

但是,似乎System.Drawing已经消失了。 我还能使用什么在捕获的图像上覆盖文本?

Windows.UI.Xaml没有直接的栅格图形API。 您可以从WriteableBitmap或BitmapDecoder获取原始像素,但是没有高级的内置方法可以在其上进行绘制。

有几种选项可将文本绘制到图像上:

  1. 互操作为DirectWrite并传递位图进行操作。
  2. 找到一个可以直接在您的像素缓冲区上绘制的第三方库(我不确定是否可以做到这一点:对于一般绘制,很多人使用WriteableBitmapEx,但是我不相信它可以在Windows Runtime应用程序中执行文本。查看WinRTXamlToolkit)
  3. 使用Image控件中的位图创建一个Grid,使用覆盖图创建一个TextBlock,然后使用RenderTargetBitmap将Grid渲染为位图。
  4. Win2d旨在向Windows Store应用程序公开Direct2d。 这是一项正在进行的工作,但是目前正在实施文本。 参见https://github.com/Microsoft/Win2D

我将从Win2d开始,看看它是否可以满足您的需求。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM