Skip to content

Commit 97fd5a5

Browse files
authored
feat(HikVision): add CapturePicture method (#869)
* feat(HikVision): add IsStartRecord parameter * chore: bump version 10.0.9 * refactor: 抓图增加设置文件名功能
1 parent 81271e6 commit 97fd5a5

3 files changed

Lines changed: 43 additions & 42 deletions

File tree

src/components/BootstrapBlazor.HikVision/BootstrapBlazor.HikVision.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Razor">
22

33
<PropertyGroup>
4-
<Version>10.0.7</Version>
4+
<Version>10.0.9</Version>
55
</PropertyGroup>
66

77
<PropertyGroup>

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

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,16 @@ public partial class HikVisionWebPlugin
115115
/// </summary>
116116
public bool IsRealPlaying { get; private set; }
117117

118+
/// <summary>
119+
/// 获得 是否已经打开声音
120+
/// </summary>
121+
public bool IsOpenSound { get; private set; }
122+
123+
/// <summary>
124+
/// 获得 是否开始录像
125+
/// </summary>
126+
public bool IsStartRecord { get; private set; }
127+
118128
/// <summary>
119129
/// <inheritdoc/>
120130
/// </summary>
@@ -164,6 +174,7 @@ public async Task Logout()
164174
{
165175
await InvokeVoidAsync("logout", Id);
166176
}
177+
IsStartRecord = false;
167178
IsRealPlaying = false;
168179
IsLogin = false;
169180
await TriggerLogout();
@@ -221,6 +232,7 @@ public async Task StopRealPlay()
221232
{
222233
await InvokeVoidAsync("stopRealPlay", Id);
223234
IsRealPlaying = false;
235+
IsStartRecord = false;
224236
await TriggerStopRealPlay();
225237
}
226238
}
@@ -281,6 +293,7 @@ public async Task<bool> OpenSound()
281293
{
282294
var code = await InvokeAsync<int>("openSound", Id);
283295
ret = code == 100;
296+
IsOpenSound = true;
284297
}
285298
return ret;
286299
}
@@ -296,6 +309,7 @@ public async Task<bool> CloseSound()
296309
{
297310
var code = await InvokeAsync<int>("closeSound", Id);
298311
ret = code == 100;
312+
IsOpenSound = false;
299313
}
300314
return ret;
301315
}
@@ -320,54 +334,30 @@ public async Task<bool> SetVolume(int value)
320334
/// 抓图方法并且下载方法
321335
/// </summary>
322336
/// <returns></returns>
323-
public async Task CapturePictureAndDownload()
337+
public async Task<bool> CapturePictureAndDownload(string? fileName = null, CancellationToken token = default)
324338
{
339+
var ret = false;
325340
if (IsLogin && IsRealPlaying)
326341
{
327-
await InvokeVoidAsync("capturePictureAndDownload", Id);
342+
ret = await InvokeAsync<bool>("capturePictureAndDownload", token, Id, fileName);
328343
}
344+
return ret;
329345
}
330346

331-
private TaskCompletionSource<IJSStreamReference?>? _captureTaskCompletionSource = null;
332-
333347
/// <summary>
334348
/// 抓图方法返回 <see cref="IJSStreamReference"/> 实例
335349
/// </summary>
336350
/// <returns></returns>
337-
public async Task<IJSStreamReference?> CapturePicture(CancellationToken token = default)
351+
public async Task<bool> CapturePicture(string? fileName = null, CancellationToken token = default)
338352
{
339-
IJSStreamReference? ret = null;
353+
var ret = false;
340354
if (IsLogin && IsRealPlaying)
341355
{
342-
_captureTaskCompletionSource = new();
343-
344-
try
345-
{
346-
await InvokeVoidAsync("capturePicture", token, Id);
347-
ret = await _captureTaskCompletionSource.Task;
348-
}
349-
catch (Exception ex)
350-
{
351-
_captureTaskCompletionSource.SetException(ex);
352-
}
356+
ret = await InvokeAsync<bool>("capturePicture", token, Id, fileName);
353357
}
354358
return ret;
355359
}
356360

357-
/// <summary>
358-
/// 抓图返回文件流方法 由 Javascript 调用
359-
/// </summary>
360-
/// <param name="stream"></param>
361-
/// <returns></returns>
362-
[JSInvokable]
363-
public async Task TriggerReceivePictureStream(IJSStreamReference stream)
364-
{
365-
if (_captureTaskCompletionSource != null)
366-
{
367-
_captureTaskCompletionSource.SetResult(stream);
368-
}
369-
}
370-
371361
/// <summary>
372362
/// 开始录像方法
373363
/// </summary>
@@ -378,6 +368,7 @@ public async Task<bool> StartRecord()
378368
if (IsLogin && IsRealPlaying)
379369
{
380370
ret = await InvokeAsync<bool>("startRecord", Id);
371+
IsStartRecord = ret;
381372
}
382373
return ret;
383374
}
@@ -392,6 +383,10 @@ public async Task<bool> StopRecord()
392383
if (IsLogin && IsRealPlaying)
393384
{
394385
ret = await InvokeAsync<bool>("stopRecord", Id);
386+
if (ret)
387+
{
388+
IsStartRecord = false;
389+
}
395390
}
396391
return ret;
397392
}

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)