File tree Expand file tree Collapse file tree 4 files changed +39
-13
lines changed Expand file tree Collapse file tree 4 files changed +39
-13
lines changed Original file line number Diff line number Diff line change @@ -7,12 +7,10 @@ namespace BootstrapBlazor.Components;
77
88class DefaultMediaDevices ( IJSRuntime jsRuntime ) : IMediaDevices
99{
10- private DotNetObjectReference < DefaultMediaDevices > ? _interop = null ;
1110 private JSModule ? _module = null ;
1211
1312 private async Task < JSModule > LoadModule ( )
1413 {
15- _interop ??= DotNetObjectReference . Create ( this ) ;
1614 _module ??= await jsRuntime . LoadModuleByName ( "media" ) ;
1715 return _module ;
1816 }
@@ -40,4 +38,10 @@ public async Task Capture()
4038 var module = await LoadModule ( ) ;
4139 await module . InvokeVoidAsync ( "capture" ) ;
4240 }
41+
42+ public async Task < string ? > GetPreviewUrl ( )
43+ {
44+ var module = await LoadModule ( ) ;
45+ return await module . InvokeAsync < string ? > ( "getPreviewUrl" ) ;
46+ }
4347}
Original file line number Diff line number Diff line change @@ -49,6 +49,6 @@ public Task Preview()
4949
5050 public Task < string ? > GetPreviewUrl ( )
5151 {
52- throw new NotImplementedException ( ) ;
52+ return deviceService . GetPreviewUrl ( ) ;
5353 }
5454}
Original file line number Diff line number Diff line change @@ -35,4 +35,10 @@ public interface IMediaDevices
3535 /// </summary>
3636 /// <returns></returns>
3737 Task Capture ( ) ;
38+
39+ /// <summary>
40+ /// Gets the preview URL of the captured image.
41+ /// </summary>
42+ /// <returns></returns>
43+ Task < string ? > GetPreviewUrl ( ) ;
3844}
Original file line number Diff line number Diff line change @@ -59,16 +59,6 @@ export async function close(videoSelector) {
5959 media . stream = null ;
6060}
6161
62- const closeStream = stream => {
63- if ( stream ) {
64- const tracks = stream . getTracks ( ) ;
65-
66- tracks . forEach ( track => {
67- track . stop ( ) ;
68- } ) ;
69- }
70- }
71-
7262export async function capture ( videoSelector ) {
7363 const video = document . querySelector ( videoSelector ) ;
7464 if ( video ) {
@@ -89,3 +79,29 @@ export async function capture(videoSelector) {
8979 }
9080 }
9181}
82+
83+ export async function getPreviewUrl ( ) {
84+ let url = null ;
85+ const media = registerBootstrapBlazorModule ( "MediaDevices" ) ;
86+ const { stream } = media ;
87+ if ( stream ) {
88+ const tracks = stream . getVideoTracks ( ) ;
89+ if ( tracks ) {
90+ const track = tracks [ 0 ] ;
91+ const capture = new ImageCapture ( track ) ;
92+ const blob = await capture . takePhoto ( ) ;
93+ url = URL . createObjectURL ( blob ) ;
94+ }
95+ }
96+ return url ;
97+ }
98+
99+ const closeStream = stream => {
100+ if ( stream ) {
101+ const tracks = stream . getTracks ( ) ;
102+
103+ tracks . forEach ( track => {
104+ track . stop ( ) ;
105+ } ) ;
106+ }
107+ }
You can’t perform that action at this time.
0 commit comments