【发布时间】:2023-08-17 20:04:01
【问题描述】:
我正在寻找使用 ZXing 库和 ASP.NET MVC 来扫描条形码,我能够在 Xamarin.Forms 中执行此操作,现在尝试将相同的代码应用于 ASP.NET MVC 项目。在我的 Xamarin.Forms 中,我有以下内容:
var options = new MobileBarcodeScanningOptions
{
TryHarder = true,
CameraResolutionSelector = HandleCameraResolutionSelectorDelegate,
PossibleFormats = new List<BarcodeFormat> { BarcodeFormat.PDF_417 },
};
BarcodeScanView.Options = options;
BarcodeScanView.IsVisible = true;
BarcodeScanView.IsScanning = true;
我会有一个像这样的 onScan 方法:
public async void OnScanResult(Result result)
{
}
在我的 xaml 文件中,我有这样的 zxing 元素:
<zxing:ZXingScannerView x:Name="BarcodeScanView" IsVisible="false" HeightRequest="200" OnScanResult="OnScanResult" />
所以我的问题是在 ASP.NET 中与 this 等效的是什么,与此 zxing 元素等效的是什么?
请帮忙!
更新
我已经走的是相机使用jQuery和ZXing.NET调试PDF417条码的路线:
这是我的 HTML:
<video id="video" width="800" height="800"></video>
<canvas id="canvas" width="800" height="800"></canvas>
以及用于相机的 jQuery 和调用 .NET 方法来调试条形码的代码:
var video = document.getElementById('video');
if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
navigator.mediaDevices.getUserMedia({ video: true }).then(function (stream) {
video.srcObject = stream;
video.play();
});
}
$("#video").on("playing", function () {
setInterval(function () { scanBarcode() }, 500);
});
function scanBarcode() {
var video = document.getElementById('video');
var canvas = document.getElementById('canvas');
var canvas_context = canvas.getContext('2d');
canvas_context.drawImage(video, 0, 0, 640, 480);
var image = document.getElementById("canvas").toDataURL("image/png");
image = image.replace('data:image/png;base64,', '');
$.post("Home/OnScan", { imageData: image }, function (data, status) {
console.log(data);
});
}
这是我调试 PDF417 条码的 .NET 方法:
public JsonResult OnScan(string imageData)
{
BitmapImage bitmapImage = new BitmapImage();
byte[] byteBuffer = Convert.FromBase64String(imageData);
Bitmap bmp;
using (var ms = new MemoryStream(byteBuffer))
{
bmp = new Bitmap(ms);
}
BarcodeReader reader = new BarcodeReader();
DecodingOptions options = new DecodingOptions
{
TryHarder = true,
PossibleFormats = new List<BarcodeFormat> { BarcodeFormat.PDF_417 }
};
reader.Options = options;
var result = reader.Decode(bmp);
return Json(result.Text, JsonRequestBehavior.AllowGet);
}
现在这仍然不起作用,但我记得当我第一次在 Xamarin.Forms 中执行此操作时,它也不起作用,直到我添加 CameraResolutionSelector 选项:
var options = new MobileBarcodeScanningOptions
{
TryHarder = true,
CameraResolutionSelector = HandleCameraResolutionSelectorDelegate,
PossibleFormats = new List<BarcodeFormat> { BarcodeFormat.PDF_417 },
};
这里是 HandleCameraResolutionSelectorDelegate 方法:
public CameraResolution HandleCameraResolutionSelectorDelegate(List<CameraResolution> availableResolutions)
{
//Don't know if this will ever be null or empty
if (availableResolutions == null || availableResolutions.Count < 1)
return new CameraResolution() { Width = 800, Height = 600 };
//Debugging revealed that the last element in the list
//expresses the highest resolution. This could probably be more thorough.
return availableResolutions[availableResolutions.Count - 1];
}
所以我开始认为是相机的分辨率导致我的条码无法扫描....另一方面,当我将 BarcodeFormat 更改为 QR_CODE 并扫描 QR 码时它可以工作,但不适用于 PDF417 条码...我做错了什么?
【问题讨论】:
-
您知道您的 Web 应用程序无法将附加到您的客户端的条形码扫描仪直接链接到服务器端代码吗?
-
我想,我只是想使用 ZXing 库来扫描我的 ASP.NET MVC 项目中的条形码....对 ZXing 很陌生,只是发现它在 Xamarin.Forms 中很容易使用
-
Xamarin.Forms 将在最终用户的计算机上运行,而不是在 Web 服务器上运行。你明白客户端代码和服务器端代码的区别吗?
-
是的,我愿意....我只是看看是否可以在 ASP.NET MVC 项目中使用 ZXing 库
-
即使可以,库也会在网络服务器上执行。因此,您需要在客户端运行一些部分,以获取条码的图像并将其发送到服务器。您需要对客户端服务器架构有深入的了解,将您的问题分解为小任务,并尝试完成每个任务。如果你被困在一个单独的任务上,那么你可能有一个有效的问题。 “帮我在 ASP.NET MVC 中使用这个库”不是一个有效的问题。
标签: javascript c# jquery html zxing