Skip to content

Commit 26c6a17

Browse files
committed
refactor: 抓图增加设置文件名功能
1 parent 819dbcf commit 26c6a17

2 files changed

Lines changed: 23 additions & 41 deletions

File tree

src/components/BootstrapBlazor.HikVision/Components/HikVisionWebPlugin.razor.cs

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -334,54 +334,30 @@ public async Task<bool> SetVolume(int value)
334334
/// 抓图方法并且下载方法
335335
/// </summary>
336336
/// <returns></returns>
337-
public async Task CapturePictureAndDownload()
337+
public async Task<bool> CapturePictureAndDownload(string? fileName = null, CancellationToken token = default)
338338
{
339+
var ret = false;
339340
if (IsLogin && IsRealPlaying)
340341
{
341-
await InvokeVoidAsync("capturePictureAndDownload", Id);
342+
ret = await InvokeAsync<bool>("capturePictureAndDownload", token, Id, fileName);
342343
}
344+
return ret;
343345
}
344346

345-
private TaskCompletionSource<IJSStreamReference?>? _captureTaskCompletionSource = null;
346-
347347
/// <summary>
348348
/// 抓图方法返回 <see cref="IJSStreamReference"/> 实例
349349
/// </summary>
350350
/// <returns></returns>
351-
public async Task<IJSStreamReference?> CapturePicture(CancellationToken token = default)
351+
public async Task<bool> CapturePicture(string? fileName = null, CancellationToken token = default)
352352
{
353-
IJSStreamReference? ret = null;
353+
var ret = false;
354354
if (IsLogin && IsRealPlaying)
355355
{
356-
_captureTaskCompletionSource = new();
357-
358-
try
359-
{
360-
await InvokeVoidAsync("capturePicture", token, Id);
361-
ret = await _captureTaskCompletionSource.Task;
362-
}
363-
catch (Exception ex)
364-
{
365-
_captureTaskCompletionSource.SetException(ex);
366-
}
356+
ret = await InvokeAsync<bool>("capturePicture", token, Id, fileName);
367357
}
368358
return ret;
369359
}
370360

371-
/// <summary>
372-
/// 抓图返回文件流方法 由 Javascript 调用
373-
/// </summary>
374-
/// <param name="stream"></param>
375-
/// <returns></returns>
376-
[JSInvokable]
377-
public async Task TriggerReceivePictureStream(IJSStreamReference stream)
378-
{
379-
if (_captureTaskCompletionSource != null)
380-
{
381-
_captureTaskCompletionSource.SetResult(stream);
382-
}
383-
}
384-
385361
/// <summary>
386362
/// 开始录像方法
387363
/// </summary>

src/components/BootstrapBlazor.HikVision/wwwroot/hikvision.js

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -409,24 +409,24 @@ export async function setVolume(id, value) {
409409
return code;
410410
}
411411

412-
export async function capturePicture(id) {
412+
export function capturePicture(id, szFileName) {
413413
const vision = Data.get(id);
414414
const { realPlaying } = vision;
415415

416416
if (realPlaying !== true) {
417-
return "";
417+
return false;
418418
}
419419

420420
try {
421-
const base64 = await WebVideoCtrl.I_CapturePicData();
422-
if (base64) {
423-
const bytes = base64ToArray(base64);
424-
return DotNet.createJSStreamReference(bytes.buffer);
421+
if (!szFileName) {
422+
szFileName = `capture_${new Date().getTime()}`;
425423
}
424+
WebVideoCtrl.I_CapturePic(szFileName);
425+
return true;
426426
}
427427
catch (ex) {
428428
console.log(ex);
429-
return null;
429+
return false;
430430
}
431431
}
432432

@@ -443,28 +443,34 @@ const base64ToArray = base64String => {
443443
return bytes;
444444
}
445445

446-
export async function capturePictureAndDownload(id) {
446+
export async function capturePictureAndDownload(id, szFileName) {
447447
const vision = Data.get(id);
448448
const { realPlaying } = vision;
449449

450450
if (realPlaying !== true) {
451-
return;
451+
return false;
452452
}
453453

454+
let ret = false;
454455
try {
455456
const base64 = await WebVideoCtrl.I_CapturePicData();
456457
if (base64) {
458+
if (!szFileName) {
459+
szFileName = `capture_${new Date().getTime()}.jpg`
460+
}
457461
const anchorElement = document.createElement('a');
458462
anchorElement.href = `data:image/jpg;base64,${base64}`;
459-
anchorElement.download = `capture_${new Date().getTime()}.jpg`;
463+
anchorElement.download = szFileName;
460464
document.body.appendChild(anchorElement);
461465
anchorElement.click();
462466
document.body.removeChild(anchorElement);
467+
ret = true;
463468
}
464469
}
465470
catch (ex) {
466471
console.log(ex);
467472
}
473+
return ret;
468474
}
469475

470476
export async function startRecord(id) {

0 commit comments

Comments
 (0)