Maui Blazor 使用攝像頭實(shí)現(xiàn)代碼
由于Maui Blazor中界面是由WebView渲染,所以再使用Android的攝像頭時(shí)無法去獲取,因?yàn)樵臄z像頭需要綁定界面組件
所以我找到了其他的實(shí)現(xiàn)方式,通過WebView使用js調(diào)用設(shè)備攝像頭 支持多平臺(tái)兼容目前測試了Android 和PC 由于沒有ioc和mac無法測試 大概率是兼容可能需要?jiǎng)討B(tài)申請權(quán)限
1. 添加js方法
我們再wwwroot中創(chuàng)建一個(gè)helper.js
的文件并且添加以下倆個(gè)js函數(shù)
再index.html
中添加<script src="helper.js"></script>
引入js
/** * 設(shè)置元素的src * @param {any} canvasId canvasId的dom id * @param {any} videoId video的dom id * @param {any} srcId img的dom id * @param {any} widht 設(shè)置截圖寬度 * @param {any} height 設(shè)置截圖高度 */ function setImgSrc(dest,videoId, srcId, widht, height) { let video = document.getElementById(videoId); let canvas = document.getElementById(canvasId); console.log(video.clientHeight, video.clientWidth); canvas.getContext('2d').drawImage(video, 0, 0, widht, height); canvas.toBlob(function (blob) { var src = document.getElementById(srcId); src.setAttribute("height", height) src.setAttribute("width", widht); src.setAttribute("src", URL.createObjectURL(blob)) }, "image/jpeg", 0.95); } /** * 初始化攝像頭 * @param {any} src */ function startVideo(src) { if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) { navigator.mediaDevices.getUserMedia({ video: true }).then(function (stream) { let video = document.getElementById(src); if ("srcObject" in video) { video.srcObject = stream; } else { video.src = window.URL.createObjectURL(stream); } video.onloadedmetadata = function (e) { video.play(); }; //mirror image video.style.webkitTransform = "scaleX(-1)"; video.style.transform = "scaleX(-1)"; }); } }
然后各個(gè)平臺(tái)的兼容
android:
Platforms/Android/AndroidManifest.xml文件內(nèi)容
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"> <application android:allowBackup="true" android:supportsRtl="true"></application> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.INTERNET" /> <!--相機(jī)權(quán)限--> <uses-permission android:name="android.permission.CAMERA" android:required="false"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <!--相機(jī)功能--> <uses-feature android:name="android.hardware.camera" /> <!--音頻錄制權(quán)限--> <uses-permission android:name="android.permission.RECORD_AUDIO" /> <!--位置權(quán)限--> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <!-- Needed only if your app targets Android 5.0 (API level 21) or higher. --> <uses-feature android:name="android.hardware.location.gps" /> <queries> <intent> <action android:name="android.intent.action.VIEW" /> <data android:scheme="http"/> </intent> <intent> <action android:name="android.intent.action.VIEW" /> <data android:scheme="https"/> </intent> </queries> </manifest>
Platforms/Android/MainActivity.cs文件內(nèi)容
[Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)] public class MainActivity : MauiAppCompatActivity { protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); Platform.Init(this, savedInstanceState); // 申請所需權(quán)限 也可以再使用的時(shí)候去申請 ActivityCompat.RequestPermissions(this, new[] { Manifest.Permission.Camera, Manifest.Permission.RecordAudio, Manifest.Permission.ModifyAudioSettings }, 0); } }
MauiWebChromeClient.cs文件內(nèi)容
#if ANDROID using Android.Webkit; using Microsoft.AspNetCore.Components.WebView.Maui; namespace MainSample; public class MauiWebChromeClient : WebChromeClient { public override void OnPermissionRequest(PermissionRequest request) { request.Grant(request.GetResources()); } } public class MauiBlazorWebViewHandler : BlazorWebViewHandler { protected override void ConnectHandler(Android.Webkit.WebView platformView) { platformView.SetWebChromeClient(new MauiWebChromeClient()); base.ConnectHandler(platformView); } } #endif
在MauiProgram.cs
中添加以下代碼;如果沒有下面代碼會(huì)出現(xiàn)沒有攝像頭權(quán)限
具體在這個(gè) issue體現(xiàn)
#if ANDROID builder.ConfigureMauiHandlers(handlers => { handlers.AddHandler<IBlazorWebView, MauiBlazorWebViewHandler>(); }); #endif
以上是android的適配代碼 pc不需要設(shè)置額外代碼 ios和mac不清楚
然后編寫界面
@page "/" @*界面路由*@ @inject IJSRuntime JSRuntime @*注入jsRuntime*@ @if(OpenCameraStatus) @*在攝像頭沒有被打開的情況不顯示video*@ { <video id="@VideoId" width="@widht" height="@height" /> <canvas class="d-none" id="@CanvasId" width="@widht" height="@height" /> } <button @onclick="" style="margin:8px">打開攝像頭</button> @*因?yàn)閿z像頭必須是用戶手動(dòng)觸發(fā)如果界面是滑動(dòng)進(jìn)入無法直接調(diào)用方法打開攝像頭所以添加按鈕觸發(fā)*@ <button style="margin:8px">截圖</button> @*對視頻流截取一張圖*@ <img id="@ImgId" /> @code{ private string CanvasId; private string ImgId; private string VideoId; private bool OpenCameraStatus; private int widht = 320; private int height = 500; private async Task OpenCamera() { if (!OpenCameraStatus) { // 由于打開攝像頭的條件必須是用戶手動(dòng)觸發(fā)如果滑動(dòng)切換到界面是無法觸發(fā)的 await JSRuntime.InvokeVoidAsync("startVideo", "videoFeed"); OpenCameraStatus = true; StateHasChanged(); } } protected override async Task OnInitializedAsync() { // 初始化id ImgId = Guid.NewGuid().ToString("N"); CanvasId = Guid.NewGuid().ToString("N"); VideoId = Guid.NewGuid().ToString("N"); await base.OnInitializedAsync(); } private async Task Screenshot() { await JSRuntime.InvokeAsync<String>("setImgSrc", CanvasId,VideoId, ImgId, widht, height); } }
然后可以運(yùn)行程序就可以看到我們的效果了
到此這篇關(guān)于Maui Blazor 使用攝像頭實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Maui Blazor 攝像頭內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Typora?0.11.18免費(fèi)版本安裝使用教程(親測可用)
Typora是一款非常使用的筆記工具,對于程序員非常友好,在2021年11月23日,Typora?正式發(fā)布?1.0?版本,進(jìn)入了付費(fèi)時(shí)代,Typora免費(fèi)版本0.11.18(最后的免費(fèi)版),本文給大家分享Typora免費(fèi)獲取方法及安裝使用教程,感興趣的朋友參考下吧2022-07-07科學(xué)知識(shí):同步、異步、阻塞和非阻塞區(qū)別
這篇文章主要介紹了科學(xué)知識(shí):同步、異步、阻塞和非阻塞區(qū)別,本文分別講解了這些概念,需要的朋友可以參考下2015-05-05gradle+shell實(shí)現(xiàn)自動(dòng)系統(tǒng)簽名
這篇文章主要介紹了gradle+shell實(shí)現(xiàn)自動(dòng)系統(tǒng)簽名的相關(guān)資料,需要的朋友可以參考下2019-08-08十六進(jìn)制、十進(jìn)制、八進(jìn)制、二進(jìn)制常用進(jìn)制轉(zhuǎn)換
進(jìn)制就是進(jìn)制位,常用的進(jìn)制包括:二進(jìn)制、八進(jìn)制、十進(jìn)制與十六進(jìn)制,區(qū)別在于數(shù)運(yùn)算時(shí)是逢幾進(jìn)一位。比如二進(jìn)制是逢2進(jìn)一位,十進(jìn)制也就是我們常用的0-9是逢10進(jìn)一位。這篇文章主要介紹了十六進(jìn)制、十進(jìn)制、八進(jìn)制、二進(jìn)制常用進(jìn)制轉(zhuǎn)換,需要的朋友可以參考下2022-12-12Elasticsearch索引的分片分配Recovery使用講解
這篇文章主要為大家介紹了Elasticsearch索引的分片分配Recovery使用講解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-04-04