Skip to content

Commit 3acbb27

Browse files
committed
feat: 实现 GetPreviewUrl 方法
1 parent 38f07b9 commit 3acbb27

File tree

4 files changed

+39
-13
lines changed

4 files changed

+39
-13
lines changed

src/BootstrapBlazor/Services/MediaDevices/DefaultMediaDevices.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@ namespace BootstrapBlazor.Components;
77

88
class 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
}

src/BootstrapBlazor/Services/MediaDevices/DefaultVideoDevice.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff 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
}

src/BootstrapBlazor/Services/MediaDevices/IMediaDevices.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff 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
}

src/BootstrapBlazor/wwwroot/modules/media.js

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff 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-
7262
export 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+
}

0 commit comments

Comments
 (0)