iT邦幫忙

1

[筆記系列]C# Zxing Decoding Barcode 上傳Barcode圖片解析 (asp.net MVC作法)

  • 分享至 

  • xImage
  •  
using ZXing;

/// <summary>
/// 解析出來之Data
/// </summary>
public class ZxingBarcodeData
{
    public BarcodeFormat BarcodeFormat { get; set; }

    public int NumBits { get; set; }

    public byte[] RawBytes { get; set; }

    public IDictionary<ResultMetadataType, object> ResultMetadata { get; set; }

    public ResultPoint[] ResultPoints { get; set; }

    public string Text { get; set; }

    public long Timestamp { get; set; }
}

/// <summary>
    /// 圖片上傳解析Barcode
    /// </summary>
    /// <param name="files"></param>
    /// <returns></returns>
    public ActionResult ReadBarcode(IEnumerable<HttpPostedFileBase> files)
    {
        List<ZxingBarcodeData> zxingBarcodeDatas = new List<ZxingBarcodeData>();

        if (files.Count() > 0)
        {
            foreach (var file in files)
            {
                if (file != null)
                {
                    // 1.create a barcode reader instance
                    IBarcodeReader barcodeReader = new BarcodeReader();

                    // 2.load a bitmap
                    Bitmap bitmap = new Bitmap(file.InputStream);

                    // 3.detect and decode the barcode inside the bitmap
                    Result[] barcodeResults = barcodeReader.DecodeMultiple(bitmap);

                    // 4.do something with the result => 封裝zxingBarcodeDatas
                    if (barcodeResults.Count() > 0)
                    {
                        foreach (Result result in barcodeResults)
                        {
                            ZxingBarcodeData zxingBarcodeData = new ZxingBarcodeData
                            {
                                BarcodeFormat = result.BarcodeFormat,
                                NumBits = result.NumBits,
                                RawBytes = result.RawBytes,
                                ResultMetadata = result.ResultMetadata,
                                ResultPoints = result.ResultPoints,
                                Text = result.Text,
                                Timestamp = result.Timestamp
                            };
                            zxingBarcodeDatas.Add(zxingBarcodeData);
                        }
                    }
                }
            }
        }
        return View(zxingBarcodeDatas);
    }

使用Zxing.net
https://github.com/micjahn/ZXing.Net


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言