Исходный код вики Camera Preview (Android)
Редактировал(а) Alexandr Fokin 2022/08/04 20:45
Последние авторы
author | version | line-number | content |
---|---|---|---|
1 | Получение трансляции изображения с камеры устройства Android, с конвертацией кадров в JPEG. | ||
2 | |||
3 | |||
4 | {{code language="c#"}} | ||
5 | using Android.Hardware; | ||
6 | using Android.Hardware.Camera2; | ||
7 | using Android.Views; | ||
8 | using YuvImage = Android.Graphics.YuvImage; | ||
9 | |||
10 | internal class AndroidCameraProvider | ||
11 | { | ||
12 | public VideoCaptureContainer CaptureVideo( | ||
13 | Action<byte[]> onFrameReceive | ||
14 | ) | ||
15 | { | ||
16 | var capture = new VideoCaptureContainer(onFrameReceive); | ||
17 | return capture; | ||
18 | } | ||
19 | |||
20 | |||
21 | #region Types | ||
22 | |||
23 | public class VideoCaptureContainer | ||
24 | : CameraDal.IVideoCaptureContainer | ||
25 | { | ||
26 | //private readonly Action<byte[]> OnFrameReceive; | ||
27 | private readonly Camera Camera; | ||
28 | private readonly Handler Handler; | ||
29 | |||
30 | |||
31 | internal VideoCaptureContainer( | ||
32 | Action<byte[]> onFrameReceive | ||
33 | ) | ||
34 | { | ||
35 | if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Lollipop) | ||
36 | { | ||
37 | throw new NotSupportedException(); | ||
38 | } | ||
39 | |||
40 | |||
41 | var numberOfCameras = Camera.NumberOfCameras; | ||
42 | var info = Enumerable.Range(0, numberOfCameras) | ||
43 | .Select( | ||
44 | e => | ||
45 | { | ||
46 | var info = new CameraInfo(); | ||
47 | Camera.GetCameraInfo(e, info); | ||
48 | return (Id: e, Info: info); | ||
49 | } | ||
50 | ) | ||
51 | .ToDictionary(e => e.Id, e => e); | ||
52 | |||
53 | var cameraId = info.Values | ||
54 | .First(e => e.Info.Facing == CameraFacing.Back); | ||
55 | //OnFrameReceive = onFrameReceive; | ||
56 | |||
57 | Camera = Camera.Open(cameraId.Id); | ||
58 | var cameraParams = Camera.GetParameters(); | ||
59 | |||
60 | //Format | ||
61 | var defaultFormat = cameraParams.PreviewFormat; | ||
62 | var supportedFormats = cameraParams.SupportedPreviewFormats | ||
63 | .Select(e => (Android.Graphics.ImageFormatType)(int)e) | ||
64 | .ToHashSet(); | ||
65 | if (!supportedFormats.Contains(Android.Graphics.ImageFormatType.Nv21)) | ||
66 | { | ||
67 | throw new Exception($"Application Format not supported on device. {Android.Graphics.ImageFormatType.Nv21}"); | ||
68 | } | ||
69 | cameraParams.PreviewFormat = Android.Graphics.ImageFormatType.Nv21; | ||
70 | |||
71 | //FrameRate | ||
72 | var defaultRate = cameraParams.PreviewFrameRate; | ||
73 | var supportedRates = cameraParams.SupportedPreviewFrameRates | ||
74 | .Select(e => (int)e) | ||
75 | .ToHashSet(); | ||
76 | //cameraParams.PreviewFrameRate = supportedRates.Min(); | ||
77 | //cameraParams.PreviewFrameRate = 5; | ||
78 | |||
79 | //Size | ||
80 | var defaultSize = cameraParams.PreviewSize; | ||
81 | |||
82 | Handler = new Handler( | ||
83 | size: (Width: defaultSize.Width, Height: defaultSize.Height), | ||
84 | sourceFormat: cameraParams.PreviewFormat, | ||
85 | onReceive: onFrameReceive | ||
86 | ); | ||
87 | |||
88 | Camera.SetPreviewCallback(Handler); | ||
89 | Camera.SetParameters(cameraParams); | ||
90 | |||
91 | Camera.StartPreview(); | ||
92 | } | ||
93 | |||
94 | #region Dispose | ||
95 | |||
96 | private bool disposed = false; | ||
97 | |||
98 | // реализация интерфейса IDisposable. | ||
99 | public void Dispose() | ||
100 | { | ||
101 | Dispose(true); | ||
102 | // подавляем финализацию | ||
103 | GC.SuppressFinalize(this); | ||
104 | } | ||
105 | |||
106 | protected void Dispose(bool disposing) | ||
107 | { | ||
108 | if (!disposed) | ||
109 | { | ||
110 | if (disposing) | ||
111 | { | ||
112 | // Освобождаем управляемые ресурсы | ||
113 | } | ||
114 | // освобождаем неуправляемые объекты | ||
115 | //Handler.OnReceive -= OnFrameReceive; | ||
116 | |||
117 | Camera.StopPreview(); | ||
118 | //Camera.Release(); | ||
119 | |||
120 | Camera.Dispose(); | ||
121 | //Handler.Dispose(); | ||
122 | |||
123 | disposed = true; | ||
124 | } | ||
125 | } | ||
126 | |||
127 | // Деструктор | ||
128 | ~VideoCaptureContainer() | ||
129 | { | ||
130 | Dispose(false); | ||
131 | } | ||
132 | |||
133 | #endregion | ||
134 | } | ||
135 | |||
136 | private class Handler | ||
137 | : Java.Lang.Object, | ||
138 | Camera.IPreviewCallback | ||
139 | { | ||
140 | private readonly Rect rectangle; | ||
141 | private readonly Android.Graphics.ImageFormatType SourceFormat; | ||
142 | private readonly Action<byte[]> OnReceive; | ||
143 | |||
144 | |||
145 | public Handler( | ||
146 | (int Width, int Height) size, | ||
147 | Android.Graphics.ImageFormatType sourceFormat, | ||
148 | Action<byte[]> onReceive | ||
149 | ) | ||
150 | { | ||
151 | rectangle = new Rect() | ||
152 | { | ||
153 | Bottom = size.Height, | ||
154 | Top = 0, | ||
155 | Left = 0, | ||
156 | Right = size.Width | ||
157 | }; | ||
158 | SourceFormat = sourceFormat; | ||
159 | OnReceive = onReceive; | ||
160 | } | ||
161 | |||
162 | |||
163 | public void OnPreviewFrame( | ||
164 | byte[] data, | ||
165 | Camera camera | ||
166 | ) | ||
167 | { | ||
168 | if (OnReceive == null) | ||
169 | { | ||
170 | return; | ||
171 | } | ||
172 | |||
173 | //Конвертация в нужный формат | ||
174 | YuvImage image = new YuvImage( | ||
175 | yuv: data, | ||
176 | format: SourceFormat, | ||
177 | width: (int)rectangle.Right, | ||
178 | height: (int)rectangle.Bottom, | ||
179 | strides: null | ||
180 | ); | ||
181 | using (var stream = new MemoryStream()) | ||
182 | { | ||
183 | image.CompressToJpeg(rectangle, 100, stream); | ||
184 | data = stream.ToArray(); | ||
185 | } | ||
186 | |||
187 | OnReceive(data); | ||
188 | } | ||
189 | } | ||
190 | |||
191 | #endregion | ||
192 | } | ||
193 | {{/code}} | ||
194 | |||
195 | ---- | ||
196 | |||
197 | [[Камера>>Разработка.JVM.Java.Frameworks and Apps.Android.Механизмы.Камера.WebHome]] | ||
198 | |||
199 | How get permission for camera in android.(Specifically Marshmallow) | ||
200 | [[https:~~/~~/stackoverflow.com/questions/38552144/how-get-permission-for-camera-in-android-specifically-marshmallow>>https://stackoverflow.com/questions/38552144/how-get-permission-for-camera-in-android-specifically-marshmallow]] | ||
201 | |||
202 | What is the image format in the Preview Screen when Camera is enable | ||
203 | [[https:~~/~~/stackoverflow.com/questions/23557520/what-is-the-image-format-in-the-preview-screen-when-camera-is-enable>>https://stackoverflow.com/questions/23557520/what-is-the-image-format-in-the-preview-screen-when-camera-is-enable]] | ||
204 | |||
205 | Android: How to save a preview frame as jpeg image? | ||
206 | [[https:~~/~~/stackoverflow.com/questions/1032258/android-how-to-save-a-preview-frame-as-jpeg-image>>https://stackoverflow.com/questions/1032258/android-how-to-save-a-preview-frame-as-jpeg-image]] | ||
207 | |||
208 | jamesathey/FastAndroidCamera | ||
209 | [[https:~~/~~/github.com/jamesathey/FastAndroidCamera>>https://github.com/jamesathey/FastAndroidCamera]] | ||
210 | |||
211 | How To Display A Stream From The Camera Using A TextureView In Xamarin Android App Using Visual Studio 2015 | ||
212 | [[https:~~/~~/www.c-sharpcorner.com/article/how-to-display-a-stream-from-the-camera-using-a-textureview-in-xamarin-android-a/>>https://www.c-sharpcorner.com/article/how-to-display-a-stream-from-the-camera-using-a-textureview-in-xamarin-android-a/]] |