Skip to content

Commit 2299067

Browse files
committed
refactor: 增加镜头翻转功能
1 parent 9555a5c commit 2299067

File tree

4 files changed

+42
-2
lines changed

4 files changed

+42
-2
lines changed

src/BootstrapBlazor.Server/Components/Samples/Cameras.razor

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
<Button Text="Television 4K (3840 * 2160)" IsDisabled="StopDisabled" OnClickWithoutRender="() => OnApply(3840, 2160)"></Button>
4343
<Button Text="Cinema 4K (4096 * 2160)" IsDisabled="StopDisabled" OnClickWithoutRender="() => OnApply(4096, 2160)"></Button>
4444
<Button Text="8K" IsDisabled="StopDisabled" OnClickWithoutRender="() => OnApply(7680, 4320)"></Button>
45+
<Button Text="Flip" IsDisabled="StopDisabled" OnClickWithoutRender="OnFlip"></Button>
4546
</div>
4647
<ConsoleLogger @ref="Logger" />
4748
<div class="mt-3">

src/BootstrapBlazor.Server/Components/Samples/Cameras.razor.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ private async Task OnClickPreview()
135135

136136
private Task OnApply(int width, int height) => Camera.Resize(width, height);
137137

138+
private Task OnFlip() => Camera.Flip();
139+
138140
private Task OnError(string err)
139141
{
140142
PlayDisabled = false;

src/BootstrapBlazor/Components/Camera/Camera.razor.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,12 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
153153
/// <returns></returns>
154154
public Task Resize(int width, int height) => InvokeVoidAsync("resize", Id, width, height);
155155

156+
/// <summary>
157+
/// 反转摄像头方法
158+
/// </summary>
159+
/// <returns></returns>
160+
public Task Flip() => InvokeVoidAsync("flip", Id);
161+
156162
/// <summary>
157163
/// 初始化设备方法
158164
/// </summary>

src/BootstrapBlazor/Components/Camera/Camera.razor.js

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ const play = (camera, option = {}) => {
4040
...option
4141
}
4242
navigator.mediaDevices.getUserMedia(constrains).then(stream => {
43+
const supportedConstraints = navigator.mediaDevices.getSupportedConstraints();
44+
console.log(supportedConstraints);
45+
4346
camera.video = { deviceId: option.video.deviceId };
4447
camera.video.element = camera.el.querySelector('video')
4548
camera.video.element.srcObject = stream
@@ -142,12 +145,40 @@ export async function resize(id, width, height) {
142145
return
143146
}
144147

145-
const constrains = {
148+
const constraints = {
146149
facingMode: "environment",
147150
width: { ideal: width },
148151
height: { ideal: height }
149152
}
150-
await camera.video.track.applyConstraints(constrains);
153+
await camera.video.track.applyConstraints(constraints);
154+
}
155+
156+
157+
export async function flip(id) {
158+
const camera = Data.get(id)
159+
if (camera === null || camera.video === void 0) {
160+
return
161+
}
162+
163+
const { track } = camera.video;
164+
if (track) {
165+
const constraints = track.getConstraints();
166+
if (constraints.facingMode === void 0) {
167+
constraints.facingMode = { exact: "environment" }
168+
}
169+
const { exact } = constraints.facingMode;
170+
if (exact === void 0) {
171+
constraints.facingMode = { ideal: "environment" }
172+
}
173+
else if (exact === "user") {
174+
constraints.facingMode = { ideal: "environment" }
175+
}
176+
else {
177+
constraints.facingMode = { ideal: "user" }
178+
}
179+
console.log(constraints);
180+
await track.applyConstraints(constraints);
181+
}
151182
}
152183

153184
export function dispose(id) {

0 commit comments

Comments
 (0)