| Name | Message | Date |
|---|---|---|
| 📄 CameraPageControl.axaml | 3 hours ago | |
| 📄 CameraPageControl.axaml.cs | 3 hours ago |
📄
app/src/Controls/CameraPageControl.axaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using Avalonia; using Avalonia.Controls; using Avalonia.Interactivity; using BookScanner.App.Services; using SkiaSharp; namespace BookScanner.App.Controls; public partial class CameraPageControl : UserControl { public static readonly DirectProperty<CameraPageControl, CameraPageSide> SideProperty = AvaloniaProperty.RegisterDirect<CameraPageControl, CameraPageSide>( nameof(Side), o => o.Side, (o, v) => o.Side = v ); public CameraPageSide Side { get; set { image.HorizontalAlignment = value is CameraPageSide.Left ? Avalonia.Layout.HorizontalAlignment.Right : Avalonia.Layout.HorizontalAlignment.Left; SetAndRaise(SideProperty, ref field, value); } } public SKBitmap? Image { get; private set; } private int rotation = 0; private SKBitmap? rotatedBitmap; private CameraInfo? subscribedCamera; public CameraPageControl() { InitializeComponent(); cameraSelector.ItemsSource = CameraService.Cameras; } private void OnCameraSelectionChange(object? sender, SelectionChangedEventArgs args) { if (subscribedCamera is not null) { CameraService.Unsubscribe(subscribedCamera, OnFrame); } if (cameraSelector.SelectedItem is not CameraInfo cameraInfo) { return; } subscribedCamera = cameraInfo; CameraService.Subscribe(subscribedCamera, OnFrame); } private void OnFrame(SKBitmap bitmap) { rotatedBitmap?.Dispose(); rotatedBitmap = null; if (rotation is not 0) { var (width, height) = rotation is 2 ? (bitmap.Width, bitmap.Height) : (bitmap.Height, bitmap.Width); rotatedBitmap = new SKBitmap(width, height); using var surface = new SKCanvas(rotatedBitmap); surface.RotateDegrees(90 * rotation, width / 2, height / 2); surface.DrawBitmap(bitmap, -(bitmap.Width - width) / 2, -(bitmap.Height - height) / 2); bitmap = rotatedBitmap; } Image = bitmap; Dispatcher.Invoke(() => { image.Source = bitmap; }); } protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e) { if (subscribedCamera is not null) { CameraService.Unsubscribe(subscribedCamera, OnFrame); } base.OnDetachedFromVisualTree(e); } private void OnRotateClick(object? sender, RoutedEventArgs args) { rotation = (rotation + 1) % 4; } } public enum CameraPageSide { Left, Right, }