Javascript 用于ASP.NET MVC扫描条形码的ZXing

Javascript 用于ASP.NET MVC扫描条形码的ZXing,javascript,c#,jquery,html,zxing,Javascript,C#,Jquery,Html,Zxing,我正在寻找使用ASP.NET MVC的ZXing库来扫描条形码的方法,我可以在Xamarin.Forms中完成这项工作,现在尝试将相同的代码应用到ASP.NET MVC项目中。在我的Xamarin.表格中,我有以下内容: var options = new MobileBarcodeScanningOptions { TryHarder = true, CameraResolution

我正在寻找使用ASP.NET MVC的ZXing库来扫描条形码的方法,我可以在Xamarin.Forms中完成这项工作,现在尝试将相同的代码应用到ASP.NET MVC项目中。在我的Xamarin.表格中,我有以下内容:

            var options = new MobileBarcodeScanningOptions
            {
                TryHarder = true,
                CameraResolutionSelector = HandleCameraResolutionSelectorDelegate,
                PossibleFormats = new List<BarcodeFormat> { BarcodeFormat.PDF_417 },
            };


            BarcodeScanView.Options = options;

            BarcodeScanView.IsVisible = true;
            BarcodeScanView.IsScanning = true;
在我的xaml文件中,我有这样的zxing元素:

<zxing:ZXingScannerView x:Name="BarcodeScanView" IsVisible="false" HeightRequest="200" OnScanResult="OnScanResult" />
下面是调试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);
        }
publicjsonresult OnScan(字符串图像数据)
{
BitmapImage BitmapImage=新的BitmapImage();
字节[]byteBuffer=Convert.FromBase64String(图像数据);
位图bmp;
使用(var ms=新内存流(byteBuffer))
{
bmp=新位图(毫秒);
}
条形码阅读器=新条形码阅读器();
DecodingOptions=新的DecodingOptions
{
TryHarder=true,
可能格式=新列表{BarcodeFormat.pdf417}
};
reader.Options=选项;
var result=reader.Decode(bmp);
返回Json(result.Text,JsonRequestBehavior.AllowGet);
}
现在这仍然不起作用,但我记得当我第一次在Xamarin中这样做时。在我添加CameraResolutionSelector选项之前,它也不起作用:

var options = new MobileBarcodeScanningOptions
            {
                TryHarder = true,
                CameraResolutionSelector = HandleCameraResolutionSelectorDelegate,
                PossibleFormats = new List<BarcodeFormat> { BarcodeFormat.PDF_417 },
            };
var options=新的MobileBarcode扫描选项
{
TryHarder=true,
CameraResolutionSelector=手持摄像头Resolutions Elegate,
可能格式=新列表{BarcodeFormat.pdf417},
};
以下是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];
        }
public CameraResolution Handle摄像头Resolutions Elegate(列出可用的解决方案)
{
//不知道这是否为null或空
if(availableResolutions==null | | availableResolutions.Count<1)
返回新的CameraResolution(){Width=800,Height=600};
//调试显示列表中的最后一个元素
//表示最高分辨率。这可能更彻底。
返回availableResolutions[availableResolutions.Count-1];
}

所以我开始认为是相机的分辨率导致我的条形码无法扫描……另一方面,当我将条形码格式更改为QR_码并扫描QR码时,它可以工作,但不能使用PDF417条形码……我做错了什么?

我已经回答过了

我有一些例子,比如这个问题中的一个,用一个显然很好的图像重建,zxing不能像预期的那样解码,我无法找出原因

尝试输入
PureBarcode=true
将解决此问题

DecodingOptions options = new DecodingOptions
{
    TryHarder = true,
    PossibleFormats = new List<BarcodeFormat> { BarcodeFormat.PDF_417 },
    PureBarcode = true,
    AutoRotate = true,
    TryInverted = true,
    CameraResolutionSelector = HandleCameraResolutionSelectorDelegate
};

CameraResolution HandleCameraResolutionSelectorDelegate(List<CameraResolution> availableResolutions)
{
    if (availableResolutions == null || availableResolutions.Count < 1)
        return new CameraResolution () { Width = 800, Height = 600 };   
    return availableResolutions [availableResolutions.Count - 1];
}

您知道您的web应用程序无法将附加到客户端的条形码扫描仪直接链接到服务器端代码吗?我想,我只想在我的ASP.NET MVC项目中使用ZXing库来扫描条形码…ZXing非常新,只是发现在Xamarin.FormsXamarin中很容易使用。Forms将在最终用户的机器上运行,不是网络服务器。你知道客户端代码和服务器端代码之间的区别吗?是的,我知道……我只是想看看是否可以在ASP.NET MVC项目中使用ZXing库即使你可以,库也会在web服务器上执行。因此,您需要在客户端运行某些部分,以获取代码的映像并将其发送到服务器。您需要对客户机-服务器体系结构有一个坚实的理解,将您的问题分解为小任务,并尝试完成每一项任务。如果你在一个单独的任务上陷入困境,那么你可能会有一个有效的问题。“帮助我在ASP.NET MVC中使用此库”不是一个有效的问题。PureBarcode只能在特殊情况下使用。它只适用于条码图像,条码周围只有白色区域。对于来自网络摄像头的真实图像,这通常是不正确的。内部PureBarcode使用非常简单的逻辑来定位代码,该代码仅适用于直接由条形码编写器生成的合成图像。或者,如果您使用不同的检测器,可以使用PureBarcode,从原始图像中剪切出带有条形码的区域,并且仅在解码过程中使用原始图像的这一部分。
var options = new MobileBarcodeScanningOptions
            {
                TryHarder = true,
                CameraResolutionSelector = HandleCameraResolutionSelectorDelegate,
                PossibleFormats = new List<BarcodeFormat> { BarcodeFormat.PDF_417 },
            };
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];
        }
DecodingOptions options = new DecodingOptions
{
    TryHarder = true,
    PossibleFormats = new List<BarcodeFormat> { BarcodeFormat.PDF_417 },
    PureBarcode = true,
    AutoRotate = true,
    TryInverted = true,
    CameraResolutionSelector = HandleCameraResolutionSelectorDelegate
};

CameraResolution HandleCameraResolutionSelectorDelegate(List<CameraResolution> availableResolutions)
{
    if (availableResolutions == null || availableResolutions.Count < 1)
        return new CameraResolution () { Width = 800, Height = 600 };   
    return availableResolutions [availableResolutions.Count - 1];
}
namespace ZXing.Interop.Decoding
{
    /// <summary>
    /// Defines an container for encoder options
    /// </summary>
    [Serializable]
    [ComVisible(true)]
    [Guid("24BE4318-BF09-4542-945D-3A9BF1DF5682")]
    [ClassInterface(ClassInterfaceType.AutoDual)]
    public class DecodingOptions
    {
        internal readonly ZXing.Common.DecodingOptions wrappedDecodingOptions;
        internal readonly BarcodeFormatCollection formatCollection;

        /// <summary>
        /// Gets or sets a flag which cause a deeper look into the bitmap
        /// </summary>
        /// <value>
        ///   <c>true</c> if [try harder]; otherwise, <c>false</c>.
        /// </value>
        public bool TryHarder
        {
            get { return wrappedDecodingOptions.TryHarder; }
            set { wrappedDecodingOptions.TryHarder = value; }
        }

        /// <summary>
        /// Image is a pure monochrome image of a barcode.
        /// </summary>
        /// <value>
        ///   <c>true</c> if monochrome image of a barcode; otherwise, <c>false</c>.
        /// </value>
        public bool PureBarcode
        {
            get { return wrappedDecodingOptions.PureBarcode; }
            set { wrappedDecodingOptions.PureBarcode = value; }
        }

        /// <summary>
        /// Specifies what character encoding to use when decoding, where applicable (type String)
        /// </summary>
        /// <value>
        /// The character set.
        /// </value>
        public string CharacterSet
        {
            get { return wrappedDecodingOptions.CharacterSet; }
            set { wrappedDecodingOptions.CharacterSet = value; }
        }

        /// <summary>
        /// Image is known to be of one of a few possible formats.
        /// Maps to a {@link java.util.List} of {@link BarcodeFormat}s.
        /// </summary>
        /// <value>
        /// The possible formats.
        /// </value>
        public IBarcodeFormatCollection PossibleFormats
        {
            get { return formatCollection; }
        }

        /// <summary>
        /// if Code39 could be detected try to use extended mode for full ASCII character set
        /// </summary>
        public bool UseCode39ExtendedMode
        {
            get { return wrappedDecodingOptions.UseCode39ExtendedMode; }
            set { wrappedDecodingOptions.UseCode39ExtendedMode = value; }
        }

        /// <summary>
        /// Don't fail if a Code39 is detected but can't be decoded in extended mode.
        /// Return the raw Code39 result instead. Maps to <see cref="bool" />.
        /// </summary>
        public bool UseCode39RelaxedExtendedMode
        {
            get { return wrappedDecodingOptions.UseCode39RelaxedExtendedMode; }
            set { wrappedDecodingOptions.UseCode39RelaxedExtendedMode = value; }
        }

        /// <summary>
        /// Assume Code 39 codes employ a check digit. Maps to <see cref="bool" />.
        /// </summary>
        /// <value>
        ///   <c>true</c> if it should assume a Code 39 check digit; otherwise, <c>false</c>.
        /// </value>
        public bool AssumeCode39CheckDigit
        {
            get { return wrappedDecodingOptions.AssumeCode39CheckDigit; }
            set { wrappedDecodingOptions.AssumeCode39CheckDigit = value; }
        }

        /// <summary>
        /// If true, return the start and end digits in a Codabar barcode instead of stripping them. They
        /// are alpha, whereas the rest are numeric. By default, they are stripped, but this causes them
        /// to not be. Doesn't matter what it maps to; use <see cref="bool" />.
        /// </summary>
        public bool ReturnCodabarStartEnd
        {
            get { return wrappedDecodingOptions.ReturnCodabarStartEnd; }
            set { wrappedDecodingOptions.ReturnCodabarStartEnd = value; }
        }

        /// <summary>
        /// Assume the barcode is being processed as a GS1 barcode, and modify behavior as needed.
        /// For example this affects FNC1 handling for Code 128 (aka GS1-128).
        /// </summary>
        /// <value>
        ///   <c>true</c> if it should assume GS1; otherwise, <c>false</c>.
        /// </value>
        public bool AssumeGS1
        {
            get { return wrappedDecodingOptions.AssumeGS1; }
            set { wrappedDecodingOptions.AssumeGS1 = value; }
        }

        /// <summary>
        /// Assume MSI codes employ a check digit. Maps to <see cref="bool" />.
        /// </summary>
        /// <value>
        ///   <c>true</c> if it should assume a MSI check digit; otherwise, <c>false</c>.
        /// </value>
        public bool AssumeMSICheckDigit
        {
            get { return wrappedDecodingOptions.AssumeMSICheckDigit; }
            set { wrappedDecodingOptions.AssumeMSICheckDigit = value; }

        }

        /// <summary>
        /// Allowed lengths of encoded data -- reject anything else. Maps to an int[].
        /// </summary>
        public int[] AllowedLengths
        {
            get { return wrappedDecodingOptions.AllowedLengths; }
            set { wrappedDecodingOptions.AllowedLengths = value; }
        }

        /// <summary>
        /// Allowed extension lengths for EAN or UPC barcodes. Other formats will ignore this.
        /// Maps to an int[] of the allowed extension lengths, for example [2], [5], or [2, 5].
        /// If it is optional to have an extension, do not set this hint. If this is set,
        /// and a UPC or EAN barcode is found but an extension is not, then no result will be returned
        /// at all.
        /// </summary>
        public int[] AllowedEANExtensions
        {
            get { return wrappedDecodingOptions.AllowedEANExtensions; }
            set { wrappedDecodingOptions.AllowedEANExtensions = value; }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="DecodingOptions"/> class.
        /// </summary>
        public DecodingOptions()
        {
            wrappedDecodingOptions = new ZXing.Common.DecodingOptions();
            formatCollection = new BarcodeFormatCollection(wrappedDecodingOptions);
        }

        internal DecodingOptions(ZXing.Common.DecodingOptions other)
        {
            wrappedDecodingOptions = other;
            formatCollection = new BarcodeFormatCollection(wrappedDecodingOptions);
        }
    }