2012-10-24 79 views
2

我正尝试使用MessagingToolkit解码C#/ ASP.NET中的图像。我设法使用这个包编码qr,但是当我尝试解码时,使用下面的代码它出现了2个错误(在页面底部)解码图像时出错

我相信这是因为我没有得到上传后图像正确,但有人可以指出我确实出错的地方。谢谢。

protected void CreateCode_OnClick(object sender, EventArgs e) 
    { 
     string path = "C:\\Users\\Wayneio\\Documents\\Visual Studio 2012\\Projects\\BAMSystem\\BAMSystem\\"; 
     if (QRUpload.HasFiles == true) 
     { 
      FileInfo fi = new FileInfo(QRUpload.FileName); 
      string extA = fi.Extension; 
      if (extA == ".jpg" || extA == ".png") 
      { 
       QRCodeDecoder decoder = new QRCodeDecoder(); 
       QRUpload.SaveAs(path + QRUpload.FileName); 

       System.Drawing.Image myImg = System.Drawing.Image.FromFile(path + QRUpload.FileName); 
       decoder.Decode(myImg); 

      } 
      else 
      { 
       error.Text = "You have uploaded a " + extA + " file. Please upload either a PNG or a JPG"; 
      } 
     } 
     else 
     { 
      error.Text = "You have not uploaded an image."; 
     } 
    } 

ERROR1:

Error 2 Argument 1: cannot convert from 'System.Drawing.Image' to 'MessagingToolkit.QRCode.Codec.Data.QRCodeImage' c:\users\wayneio\documents\visual studio 2012\Projects\BAMSystem\BAMSystem\default.aspx.cs 38 35 BAMSystem 

误差2:

Error 1 The best overloaded method match for 'MessagingToolkit.QRCode.Codec.QRCodeDecoder.Decode(MessagingToolkit.QRCode.Codec.Data.QRCodeImage)' has some invalid arguments c:\users\wayneio\documents\visual studio 2012\Projects\BAMSystem\BAMSystem\default.aspx.cs 38 20 BAMSystem 

PS如果任何人有此MessagingToolkit QR包文档,这将是有用

回答

1

解码接受 '位图' 类型图片。

System.Drawing.Bitmap myImg = new System.Drawing.Bitmap(FileName); 
Dictionary<DecodeOptions, object> decodingOptions = new Dictionary<DecodeOptions, object>(); 
List<BarcodeFormat> possibleFormats = new List<BarcodeFormat>(10); 

         possibleFormats.Add(BarcodeFormat.DataMatrix); 
         possibleFormats.Add(BarcodeFormat.QRCode); 
         possibleFormats.Add(BarcodeFormat.PDF417); 
         possibleFormats.Add(BarcodeFormat.Aztec); 
         possibleFormats.Add(BarcodeFormat.UPCE); 
         possibleFormats.Add(BarcodeFormat.UPCA); 
         possibleFormats.Add(BarcodeFormat.Code128); 
         possibleFormats.Add(BarcodeFormat.Code39); 
         possibleFormats.Add(BarcodeFormat.ITF14); 
         possibleFormats.Add(BarcodeFormat.EAN8); 
         possibleFormats.Add(BarcodeFormat.EAN13); 
         possibleFormats.Add(BarcodeFormat.RSS14); 
         possibleFormats.Add(BarcodeFormat.RSSExpanded); 
         possibleFormats.Add(BarcodeFormat.Codabar); 
         possibleFormats.Add(BarcodeFormat.MaxiCode); 

decodingOptions.Add(DecodeOptions.TryHarder, true); 
decodingOptions.Add(DecodeOptions.PossibleFormats, possibleFormats); 
Result decodedResult = decoder.Decode(myImg, decodingOptions); 

      if (decodedResult != null) 
         { 
          //.. success 
         } 

另外也可以省略“decodingOptions”选项参数作为解码器还具有一个过载解码(位图图像​​)。

System.Drawing.Bitmap myImg = new System.Drawing.Bitmap(FileName); 
Result decodedResult = decoder.Decode(myImg); 

      if (decodedResult != null) 
         { 
          //.. success 
         } 

如果你想只QR码解码,

System.Drawing.Bitmap myImg = new System.Drawing.Bitmap(FileName); 
    Dictionary<DecodeOptions, object> decodingOptions = new Dictionary<DecodeOptions, object>(); 
    List<BarcodeFormat> possibleFormats = new List<BarcodeFormat>(); 
    possibleFormats.Add(BarcodeFormat.QRCode);        
    decodingOptions.Add(DecodeOptions.TryHarder, true); 
    decodingOptions.Add(DecodeOptions.PossibleFormats, possibleFormats); 
    Result decodedResult = decoder.Decode(myImg, decodingOptions); 
    if (decodedResult != null) 
     { 
      //.. success 
     } 

您可以找到的文档和代码在这里
http://platform.twit88.com/projects/show/mt-barcode

示例代码..download这里..has演示代码也
http://platform.twit88.com/projects/mt-barcode/files代码项目
http://www.codeproject.com/Articles/20574/Open-Source-QRCode-Library

+0

谢谢,所以这也应该工作,而不是一个列表? decoder.Decode(myImg,BarcodeFormat.QRCode); – Wayneio

+0

否..它将需要列表..在你的情况下,一个元素的列表。你可以删除不需要的条目。但是,如果条形码格式发生变化,则可以使这些条目/格式更好地使解码逻辑更具弹性。此外,您还可以省略“decodeOptions”选项参数,因为解码器也具有过载解码(位图图像​​)。我也会更新答案。 – Amitd