简体   繁体   English

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

[英]Add text to image from camera capture

I'm attempting to create a metro app that will capture an image from the tablet camera. 我正在尝试创建一个Metro应用程序,该应用程序将从平板电脑相机捕获图像。 However at time of capture I want to overlay a date stamp on the image. 但是,在捕获时,我想在图像上覆盖一个日期戳。

/// <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);
        }
    }

Normally to add text on an image I'd use. 通常,在要使用的图像上添加文本。

Graphics g = Graphics.FromImage

However it seems that System.Drawing is gone. 但是,似乎System.Drawing已经消失了。 What else can I use to overlay text on the captured image? 我还能使用什么在捕获的图像上覆盖文本?

Windows.UI.Xaml doesn't have a direct raster graphics api. Windows.UI.Xaml没有直接的栅格图形API。 You can get the raw pixels from a WriteableBitmap or from a BitmapDecoder, but there is no high level way in-box to draw on them. 您可以从WriteableBitmap或BitmapDecoder获取原始像素,但是没有高级的内置方法可以在其上进行绘制。

There are several options to draw text onto the image: 有几种选项可将文本绘制到图像上:

  1. Interop to DirectWrite and pass the bitmap in for manipulation. 互操作为DirectWrite并传递位图进行操作。
  2. Find a 3rd party library that will draw directly on your pixel buffer (I'm not sure if any can do this: for general drawing many people use WriteableBitmapEx, but I don't believe it can do text in a Windows Runtime app. Also check out the WinRTXamlToolkit ) 找到一个可以直接在您的像素缓冲区上绘制的第三方库(我不确定是否可以做到这一点:对于一般绘制,很多人使用WriteableBitmapEx,但是我不相信它可以在Windows Runtime应用程序中执行文本。查看WinRTXamlToolkit)
  3. Create a Grid with your bitmap in an Image control and a TextBlock with your overlay then use RenderTargetBitmap to render the Grid to a bitmap. 使用Image控件中的位图创建一个Grid,使用覆盖图创建一个TextBlock,然后使用RenderTargetBitmap将Grid渲染为位图。
  4. Win2d is designed to expose Direct2d to Windows Store apps. Win2d旨在向Windows Store应用程序公开Direct2d。 It is a work in progress, but text is currently implemented. 这是一项正在进行的工作,但是目前正在实施文本。 See https://github.com/Microsoft/Win2D 参见https://github.com/Microsoft/Win2D

I'd start with Win2d to see if it can do what you need. 我将从Win2d开始,看看它是否可以满足您的需求。

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

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