簡體   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