Skip to content

Commit 1015de5

Browse files
committed
feat(HikVision): 增加抓图功能
1 parent 03e8977 commit 1015de5

3 files changed

Lines changed: 72 additions & 2 deletions

File tree

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,4 +315,44 @@ public async Task<bool> SetVolume(int value)
315315
}
316316
return ret;
317317
}
318+
319+
private TaskCompletionSource<IJSStreamReference?>? _captureTaskCompletionSource = null;
320+
321+
/// <summary>
322+
/// 抓图方法返回 Url
323+
/// </summary>
324+
/// <returns></returns>
325+
public async Task<IJSStreamReference?> CapturePicture(CancellationToken token = default)
326+
{
327+
IJSStreamReference? ret = null;
328+
if (IsLogin && IsRealPlaying)
329+
{
330+
_captureTaskCompletionSource = new();
331+
332+
try
333+
{
334+
await InvokeVoidAsync("capturePicture", token, Id);
335+
ret = await _captureTaskCompletionSource.Task;
336+
}
337+
catch (Exception ex)
338+
{
339+
_captureTaskCompletionSource.SetException(ex);
340+
}
341+
}
342+
return ret;
343+
}
344+
345+
/// <summary>
346+
/// 抓图返回文件流方法 由 Javascript 调用
347+
/// </summary>
348+
/// <param name="stream"></param>
349+
/// <returns></returns>
350+
[JSInvokable]
351+
public async Task TriggerReceivePictureStream(IJSStreamReference stream)
352+
{
353+
if (_captureTaskCompletionSource != null)
354+
{
355+
_captureTaskCompletionSource.SetResult(stream);
356+
}
357+
}
318358
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { init as initVision, login as loginVision, logout, startRealPlay, stopRealPlay, openSound, closeSound, setVolume, dispose as disposeVision } from '../hikvision.js';
1+
import { init as initVision, login as loginVision, logout, startRealPlay, stopRealPlay, openSound, closeSound, setVolume, capturePicture, capturePictureAndDownload, dispose as disposeVision } from '../hikvision.js';
22
import Data from '../../BootstrapBlazor/modules/data.js';
33

44
export async function init(id, invoke) {
@@ -29,7 +29,7 @@ export async function login(id, ip, port, userName, password, loginType) {
2929
return logined;
3030
}
3131

32-
export { logout, startRealPlay, stopRealPlay, openSound, closeSound, setVolume }
32+
export { logout, startRealPlay, stopRealPlay, openSound, closeSound, setVolume, capturePicture, capturePictureAndDownload }
3333

3434
export function dispose(id) {
3535
disposeVision(id);

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

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

412+
export async function capturePicture(id) {
413+
const vision = Data.get(id);
414+
const { iWndIndex, realPlaying, invoke } = vision;
415+
416+
if (realPlaying !== true) {
417+
return "";
418+
}
419+
420+
try {
421+
const base64 = await WebVideoCtrl.I_CapturePicData();
422+
const bytes = base64ToArray(base64);
423+
return DotNet.createJSStreamReference(bytes.buffer);
424+
}
425+
catch (ex) {
426+
return null;
427+
}
428+
}
429+
430+
const base64ToArray = base64String => {
431+
const base64Data = base64String.split(',')[1] || base64String;
432+
const binaryString = atob(base64Data);
433+
const length = binaryString.length;
434+
const bytes = new Uint8Array(length);
435+
436+
for (let i = 0; i < length; i++) {
437+
bytes[i] = binaryString.charCodeAt(i);
438+
}
439+
440+
return bytes;
441+
}
412442
export function dispose(id) {
413443
const vision = Data.get(id);
414444
const { realPlaying, logined, observer } = vision;

0 commit comments

Comments
 (0)