master
wanggang 4 years ago
parent ac83679e8e
commit e0b0273b4d

@ -30,12 +30,9 @@
{
this.statusStrip1 = new System.Windows.Forms.StatusStrip();
this.toolStripStatusLabel1 = new System.Windows.Forms.ToolStripStatusLabel();
this.menuStrip1 = new System.Windows.Forms.MenuStrip();
this.ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.webView21 = new Microsoft.Web.WebView2.WinForms.WebView2();
this.folderBrowserDialog1 = new System.Windows.Forms.FolderBrowserDialog();
this.statusStrip1.SuspendLayout();
this.menuStrip1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.webView21)).BeginInit();
this.SuspendLayout();
//
@ -55,30 +52,14 @@
this.toolStripStatusLabel1.Size = new System.Drawing.Size(131, 17);
this.toolStripStatusLabel1.Text = "toolStripStatusLabel1";
//
// menuStrip1
//
this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.ToolStripMenuItem});
this.menuStrip1.Location = new System.Drawing.Point(0, 0);
this.menuStrip1.Name = "menuStrip1";
this.menuStrip1.Size = new System.Drawing.Size(800, 25);
this.menuStrip1.TabIndex = 1;
this.menuStrip1.Text = "menuStrip1";
//
// 配置ToolStripMenuItem
//
this.ToolStripMenuItem.Name = "配置ToolStripMenuItem";
this.ToolStripMenuItem.Size = new System.Drawing.Size(44, 21);
this.ToolStripMenuItem.Text = "配置";
//
// webView21
//
this.webView21.CreationProperties = null;
this.webView21.DefaultBackgroundColor = System.Drawing.Color.White;
this.webView21.Dock = System.Windows.Forms.DockStyle.Fill;
this.webView21.Location = new System.Drawing.Point(0, 25);
this.webView21.Location = new System.Drawing.Point(0, 0);
this.webView21.Name = "webView21";
this.webView21.Size = new System.Drawing.Size(800, 403);
this.webView21.Size = new System.Drawing.Size(800, 428);
this.webView21.TabIndex = 2;
this.webView21.ZoomFactor = 1D;
//
@ -89,15 +70,11 @@
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.webView21);
this.Controls.Add(this.statusStrip1);
this.Controls.Add(this.menuStrip1);
this.MainMenuStrip = this.menuStrip1;
this.Name = "MainForm";
this.Text = "Form1";
this.Shown += new System.EventHandler(this.MainForm_Shown);
this.statusStrip1.ResumeLayout(false);
this.statusStrip1.PerformLayout();
this.menuStrip1.ResumeLayout(false);
this.menuStrip1.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.webView21)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
@ -108,8 +85,6 @@
private StatusStrip statusStrip1;
private ToolStripStatusLabel toolStripStatusLabel1;
private MenuStrip menuStrip1;
private ToolStripMenuItem ToolStripMenuItem;
private Microsoft.Web.WebView2.WinForms.WebView2 webView21;
private FolderBrowserDialog folderBrowserDialog1;
}

@ -1,7 +1,6 @@
using Microsoft.Web.WebView2.Core;
using Microsoft.Web.WebView2.WinForms;
using System.Security.Cryptography;
using System.Text;
using System.Text.Encodings.Web;
using System.Text.Json;
@ -59,24 +58,39 @@ namespace PhotoCollector
webView.CoreWebView2.PermissionRequested += CoreWebView2_PermissionRequested;
webView.CoreWebView2.Settings.UserAgent = $"{webView.CoreWebView2.Settings.UserAgent}|{AppContext.BaseDirectory}";
webView.CoreWebView2.WebMessageReceived += CoreWebView2_WebMessageReceived;
AddEventHandler();
webView.CoreWebView2.AddHostObjectToScript("dotnet", new WebView2Interop());
webView.CoreWebView2.Navigate("https://localhost:7235/");
}
private void CoreWebView2_WebMessageReceived(object? sender, CoreWebView2WebMessageReceivedEventArgs e)
private void AddEventHandler()
{
var messages = JsonSerializer.Deserialize<Dictionary<string, string>>(e.WebMessageAsJson);
var command = messages["command"].ToString();
if (command == "folder")
WebView2Interop.FuncList.Add("selectPath", o =>
{
this.folderBrowserDialog1.SelectedPath = messages["path"];
var code = 0;
var message = "操作成功";
var path = o["path"];
this.folderBrowserDialog1.SelectedPath = path;
if (this.folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
this.webView21.ExecuteScriptAsync($"onfolderSelected({JsonSerializer.Serialize(folderBrowserDialog1.SelectedPath, jsonSerializerOptions)})");
path = folderBrowserDialog1.SelectedPath;
}
}
else if (command == "read")
else
{
code = 1;
message = "未选择任何目录";
path = String.Empty;
}
return JsonSerializer.Serialize(new
{
path,
code,
message
}, jsonSerializerOptions);
});
WebView2Interop.FuncList.Add("readFile", o =>
{
var file = messages["file"];
var file = o["file"];
var base64 = string.Empty;
var code = 0;
var message = string.Empty;
@ -106,21 +120,44 @@ namespace PhotoCollector
code = 1;
message = $"Îļþ²»´æÔÚ:{file}";
}
var args = JsonSerializer.Serialize(new
return JsonSerializer.Serialize(new
{
file,
base64,
code,
message
}, jsonSerializerOptions);
this.webView21.ExecuteScriptAsync($"onFileReceived({args})");
}
else if (command == "save")
});
WebView2Interop.FuncList.Add("saveFile", o =>
{
var code = 0;
var message = "操作成功";
try
{
var file = o["file"];
File.WriteAllBytes(file, Convert.FromBase64String(o["base64"].Split(',')[1]));
File.WriteAllText(file + ".meta", FileMd5(file));
}
catch (Exception ex)
{
code = 1;
message = ex.Message;
}
return JsonSerializer.Serialize(new
{
code,
message
}, jsonSerializerOptions);
});
}
private void CoreWebView2_WebMessageReceived(object? sender, CoreWebView2WebMessageReceivedEventArgs e)
{
var dict = JsonSerializer.Deserialize<Dictionary<string, string>>(e.WebMessageAsJson);
var command = dict["command"];
if (command == "setStatus")
{
var file = messages["file"];
File.WriteAllBytes(file, Convert.FromBase64String(messages["base64"].Split(',')[1]));
File.WriteAllText(file + ".meta", FileMd5(file));
this.webView21.ExecuteScriptAsync($"alert({JsonSerializer.Serialize(messages["file"])})");
this.toolStripStatusLabel1.Text = dict["message"];
}
}
@ -144,11 +181,15 @@ namespace PhotoCollector
}
try
{
//this.Text = o ? "读卡器已连接" : "读卡器未连接";
var msg = o ? "读卡器已连接" : "读卡器未连接";
if (this.toolStripStatusLabel1.Text != msg)
{
this.toolStripStatusLabel1.Text = msg;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
this.toolStripStatusLabel1.Text = ex.Message;
}
}));
});
@ -164,7 +205,7 @@ namespace PhotoCollector
{
this.toolStripStatusLabel1.Text = o.Name;
var param = JsonSerializer.Serialize(o, jsonSerializerOptions);
this.webView21.ExecuteScriptAsync($"onRead({param})");
this.webView21.ExecuteScriptAsync($"onIdCardRead({param})");
}
catch (Exception ex)
{

@ -60,9 +60,6 @@
<metadata name="statusStrip1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
<metadata name="menuStrip1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>138, 17</value>
</metadata>
<metadata name="folderBrowserDialog1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>258, 17</value>
</metadata>

@ -0,0 +1,15 @@
using System.Text.Json;
namespace PhotoCollector
{
public class WebView2Interop
{
public static Dictionary<string, Func<Dictionary<string, string>, string>> FuncList = new Dictionary<string, Func<Dictionary<string, string>, string>>();
public string Action(string message)
{
var json = JsonSerializer.Deserialize<Dictionary<string, string>>(message);
return FuncList[json["command"]].Invoke(json);
}
}
}

@ -51,8 +51,8 @@
<select id="select"></select>
<button id="button">拍照</button>
<button id="folderBrowser">选择目录</button>
<button id="readImage">读取图片</button>
<button id="saveImage">保存图片</button>
<button id="readFile">读取图片</button>
<button id="saveFile">保存图片</button>
<hr />
<div class="videoWrapper">
<video id="webcam"></video>
@ -65,31 +65,50 @@
<img id="idCard" alt="" />
<script>
var appBasePath = navigator.userAgent.split('|')[1];
function onRead(card) {
window.chrome.webview.postMessage({ command: "setStatus", message: `app at ${appBasePath}` });
function onIdCardRead(card) {
document.getElementById('idCard').src = card.image;
}
function onfolderSelected(path) {
alert(path);
}
function onFileReceived(args) {
if (args.code) {
alert(args.message);
return;
}
document.getElementById('idCard').src = `data:image/webp;base64,${args.base64}`;
}
document.getElementById("folderBrowser").onclick = function () {
window.chrome.webview.postMessage({ command: 'folder', path: appBasePath });
var response = window.chrome.webview.hostObjects.sync.dotnet.Action(JSON.stringify({
command: "selectPath",
path: appBasePath
}));
var result = JSON.parse(response);
if (result.code) {
alert(result.message);
}
else {
alert(result.path);
}
}
document.getElementById("saveImage").onclick = function () {
window.chrome.webview.postMessage({
command: 'save',
document.getElementById("saveFile").onclick = function () {
var response = window.chrome.webview.hostObjects.sync.dotnet.Action(JSON.stringify({
command: 'saveFile',
file: appBasePath + "test.webp",
base64: canvas.toDataURL('image/webp')
});
}
document.getElementById("readImage").onclick = function () {
window.chrome.webview.postMessage({ command: 'read', file: appBasePath + "test.webp" });
}));
var result = JSON.parse(response);
if (result.code) {
alert(result.message);
}
};
document.getElementById("readFile").onclick = function () {
var response = window.chrome.webview.hostObjects.sync.dotnet.Action(JSON.stringify({
command: 'readFile',
file: appBasePath + "test.webp"
}));
var result = JSON.parse(response);
if (result.code) {
alert(result.message);
}
else {
document.getElementById('idCard').src = `data:image/webp;base64,${result.base64}`;
}
}
</script>
<script>

Loading…
Cancel
Save