簡體   English   中英

Xamarin 旋轉攝像頭拍攝的圖像

[英]Xamarin Rotate Image Captured With Camera

我試圖在skiasharp中獲得一個圖像,該圖像左旋轉90度以居中並完全適合canvas。 我嘗試了2種方法。 我自己的自定義方式,以及另一種似乎很流行的解決方案,但也許我不明白它是如何正常工作的?

  1. 我自己的方式。

這是代碼:

SKSurface surf = e.Surface;
SKCanvas canvas = surf.Canvas;
SKSize size = canvasView.CanvasSize;
canvas.Clear();

SKRect rect = SKRect.Create(0.0f, 0.0f, size.Height, size.Width);
canvas.RotateDegrees(85);
canvas.DrawBitmap(m_bm, rect);

“m_bm”是在單獨的 function 中檢索到的 bitmap。 即 function 是:

// Let user take a picture.
var result = await MediaPicker.CapturePhotoAsync(new MediaPickerOptions
{
   Title = "Take a picture"
});

// Save stream.
var stream = await result.OpenReadAsync();

// Create the bitmap.
m_bm = SKBitmap.Decode(stream);

// Set to true because the image will be prepared soon.
m_displayedImage = true;

我只放了 85 而不是 90,因為我想在視覺上看到它越來越近,但是當我這樣做時,它會離開屏幕。 我來自游戲編程背景,所以這通常可以通過獲取我們正在使用的任何東西的寬度(比如游戲中的玩家)並將其添加到 x position 和繁榮來解決。 但是對於 Xamarin,沒有用。 這是我自己的嘗試。 然后我當然上網尋求幫助,並給了我一個不同的實現。

  1. 流行的解決方案

請參閱此處了解這個流行的解決方案,這是對這個用戶問題的第一個答案。 我使用的代碼略有不同,因為我沒有看到用戶 function 返回圖像的意義。 這里是:

        // Save stream.
        var stream = await result.OpenReadAsync();

        using (var bitmap = SKBitmap.Decode(stream))
        {
            var rotated = new SKBitmap(bitmap.Height, bitmap.Width);

            using (var surface = new SKCanvas(rotated))
            {
                surface.Clear();
                surface.Translate(rotated.Height, rotated.Width);
                surface.RotateDegrees(90);
                surface.DrawBitmap(bitmap, 0, 0);
            }
        }

我正在用 canvas 繪制 bitmap 並且我認為這會起作用,因為在其他代碼示例中測試它確實做到了,所以我肯定沒有正確旋轉或什么?

@Cheesebaron 在回復原帖時給我的鏈接最終成功了。 但是出現了一個新問題,但我會自己用谷歌搜索。 這是我自己的代碼:

namespace BugApp
{


public partial class MainPage : ContentPage
{
    // Save bitmaps for later use.
    static SKBitmap m_bm;
    static SKBitmap m_editedBm;

    // Boolean for displaying the image captured with the camera.
    bool m_displayedImage;
    
    public MainPage()
    {
        // Set to explicit default values to always be in control of the assignments.
        m_bm = null;
        m_editedBm = null;

        // No picture has been taken yet.
        m_displayedImage = false;

        InitializeComponent();
    }

    // Assigned to the button in the xaml page.
    private async void SnapPicture(Object sender, System.EventArgs e)
    {
        // Let user take a picture.
        var result = await MediaPicker.CapturePhotoAsync(new MediaPickerOptions
        {
            Title = "Take a picture"
        });

        // Save stream.
        var stream = await result.OpenReadAsync();

        // Create the bitmap.
        m_bm = SKBitmap.Decode(stream);

        // Get the rotated image.
        m_editedBm = Rotate();

        // Set to true because the image will be prepared soon.
        m_displayedImage = true;
    }

    public static SKBitmap Rotate()
    {
        using (var bitmap = m_bm)
        {
            var rotated = new SKBitmap(bitmap.Height, bitmap.Width);

            using (var surface = new SKCanvas(rotated))
            {
                surface.Translate(bitmap.Width, 0);
                surface.RotateDegrees(90);
                surface.DrawBitmap(bitmap, 0, 0);
            }

            return rotated;
        }
    }

    private void OnCanvasViewPaintSurface(object sender, SKPaintSurfaceEventArgs e)
    {
        if(m_bm != null && m_displayedImage == true)
        {
            e.Surface.Canvas.Clear();

            // Draw in a new rect space.
            e.Surface.Canvas.DrawBitmap(m_editedBm, new SKRect(0.0f, 0.0f, 300.0f, 300.0f));

            // ---Testing.
            // e.Surface.Canvas.DrawBitmap(m_editedBm, new SKRect(112, 238, 184, 310), new SKRect(0, 0, 9, 9));
            
            // Avoid having this function launch again for now.
            m_displayedImage = false;
        }
    }
}

}

重要的代碼的主要部分是旋轉 function ,這里是這個: Link

感謝所有回復的人。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM