Skip to content

Commit 2a1cea6

Browse files
authored
feat(HikVision): add CapturePicture function (#865)
* refactor: 更改正确码为 100 * feat(HikVision): 增加抓图功能 * feat: 增加抓图并下载功能 * feat: 增加下载功能 * chore: bump version 10.0.6
1 parent 6c05499 commit 2a1cea6

4 files changed

Lines changed: 118 additions & 9 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.5</Version>
4+
<Version>10.0.6</Version>
55
</PropertyGroup>
66

77
<PropertyGroup>

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

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ public async Task<bool> OpenSound()
280280
if (IsLogin && IsRealPlaying)
281281
{
282282
var code = await InvokeAsync<int>("openSound", Id);
283-
ret = code == 0;
283+
ret = code == 100;
284284
}
285285
return ret;
286286
}
@@ -295,7 +295,7 @@ public async Task<bool> CloseSound()
295295
if (IsLogin && IsRealPlaying)
296296
{
297297
var code = await InvokeAsync<int>("closeSound", Id);
298-
ret = code == 0;
298+
ret = code == 100;
299299
}
300300
return ret;
301301
}
@@ -311,8 +311,60 @@ public async Task<bool> SetVolume(int value)
311311
if (IsLogin && IsRealPlaying)
312312
{
313313
var code = await InvokeAsync<int>("setVolume", Id, Math.Max(0, Math.Min(100, value)));
314-
ret = code == 0;
314+
ret = code == 100;
315315
}
316316
return ret;
317317
}
318+
319+
/// <summary>
320+
/// 抓图方法返回 Url
321+
/// </summary>
322+
/// <returns></returns>
323+
public async Task CapturePictureAndDownload()
324+
{
325+
if (IsLogin && IsRealPlaying)
326+
{
327+
await InvokeVoidAsync("capturePictureAndDownload", Id);
328+
}
329+
}
330+
331+
private TaskCompletionSource<IJSStreamReference?>? _captureTaskCompletionSource = null;
332+
333+
/// <summary>
334+
/// 抓图方法返回 Url
335+
/// </summary>
336+
/// <returns></returns>
337+
public async Task<IJSStreamReference?> CapturePicture(CancellationToken token = default)
338+
{
339+
IJSStreamReference? ret = null;
340+
if (IsLogin && IsRealPlaying)
341+
{
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+
}
353+
}
354+
return ret;
355+
}
356+
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+
}
318370
}

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: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ export async function openSound(id) {
355355
return 101;
356356
}
357357

358-
let code = 0;
358+
let code = 100;
359359
try {
360360
await WebVideoCtrl.I_OpenSound(iWndIndex);
361361
}
@@ -374,7 +374,7 @@ export async function closeSound(id) {
374374
return 101;
375375
}
376376

377-
let code = 0;
377+
let code = 100;
378378
try {
379379
await WebVideoCtrl.I_CloseSound(iWndIndex);
380380
}
@@ -398,7 +398,7 @@ export async function setVolume(id, value) {
398398
v = 50;
399399
}
400400

401-
let code = 0;
401+
let code = 100;
402402
try {
403403
await WebVideoCtrl.I_SetVolume(Math.min(100, Math.max(0, v)));
404404
}
@@ -409,6 +409,63 @@ 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 { realPlaying } = vision;
415+
416+
if (realPlaying !== true) {
417+
return "";
418+
}
419+
420+
try {
421+
const base64 = await WebVideoCtrl.I_CapturePicData();
422+
if (base64) {
423+
const bytes = base64ToArray(base64);
424+
return DotNet.createJSStreamReference(bytes.buffer);
425+
}
426+
}
427+
catch (ex) {
428+
return null;
429+
}
430+
}
431+
432+
const base64ToArray = base64String => {
433+
const base64Data = base64String.split(',')[1] || base64String;
434+
const binaryString = atob(base64Data);
435+
const length = binaryString.length;
436+
const bytes = new Uint8Array(length);
437+
438+
for (let i = 0; i < length; i++) {
439+
bytes[i] = binaryString.charCodeAt(i);
440+
}
441+
442+
return bytes;
443+
}
444+
445+
export async function capturePictureAndDownload(id) {
446+
const vision = Data.get(id);
447+
const { realPlaying } = vision;
448+
449+
if (realPlaying !== true) {
450+
return;
451+
}
452+
453+
try {
454+
const base64 = await WebVideoCtrl.I_CapturePicData();
455+
if (base64) {
456+
const anchorElement = document.createElement('a');
457+
anchorElement.href = `data:image/jpg;base64,${base64}`;
458+
anchorElement.download = `capture_${new Date().getTime()}.jpg`;
459+
document.body.appendChild(anchorElement);
460+
anchorElement.click();
461+
document.body.removeChild(anchorElement);
462+
}
463+
}
464+
catch (ex) {
465+
466+
}
467+
}
468+
412469
export function dispose(id) {
413470
const vision = Data.get(id);
414471
const { realPlaying, logined, observer } = vision;

0 commit comments

Comments
 (0)