2012-04-24 70 views
1

我在用ZXing 2.0在芒果7.1上生成QR碼時遇到問題。 它應該是非常簡單的,但它不工作。使用ZXing 2.0在Windows Phone 7.1上生成QR碼C#端口號

代碼:

QRCodeWriter writer = new QRCodeWriter(); 
var bMatrix = writer.encode("Hey dude, QR FTW!", BarcodeFormat.QR_CODE, 25, 25); 
var asBitmap = bMatrix.ToBitmap();    
image1.Source = asBitmap; 

此搜索來自XAML。

bMatrix似乎包含我需要的數據,但image1從來沒有顯示出一個東西。

+2

您是否嘗試過簡單地加載圖像(即它絕對是不工作的QR部分)? – 2012-04-24 07:57:40

+1

嗨,喬治。是的,我可以輕鬆地從文件加載圖像並將其顯示在應用程序中。 – ThomasVestergaard 2012-04-24 11:22:27

+1

你可以保存'bMatrix'到一個文件並查看它嗎? (如果是的話,你可以加載保存的文件在你的應用程序中)? – 2012-04-24 11:26:27

回答

3

所以我設法做了一個解決方法。我不確定我的原始代碼是否因ZXing C#端口中的錯誤而無法工作,或者我做錯了什麼。無論如何,這是我所做的顯示QR碼。

image1來自xaml。

QRCodeWriter writer = new QRCodeWriter(); 
var bMatrix = writer.encode("Hey dude! QR FTW!", BarcodeFormat.QR_CODE, width, height); 

WriteableBitmap wbmi = new System.Windows.Media.Imaging.WriteableBitmap(width, height); 

for (int y = 0; y < height; y++) 
{ 
    for (int x = 0; x < width; x++) 
    { 
     int grayValue = bMatrix.Array[y][x] & 0xff; 
     if (grayValue == 0)       
      wbmi.SetPixel(x, y, Color.FromArgb(255, 0, 0,0));              
     else 
      wbmi.SetPixel(x, y, Color.FromArgb(255, 255, 255, 255)); 
    } 
} 
image1.Source = wbmi; 
1

嘗試設置的圖像源是這樣的:

image1 = new ImageBrush { ImageSource = asBitmap ;} 
+2

image1是類型圖像。 ImageBrush不能轉換成Image ...或者編譯器告訴我。 – ThomasVestergaard 2012-04-25 10:44:28

-1

最簡單的辦法:

Uri uri = new Uri("http://www.esponce.com/api/v3/generate?content=" + "your content here" + "&format=png"); 

image1.Source = new BitmapImage(uri); 
+1

除非您的用戶禁用數據或存在網絡問題等。 – 2012-10-12 03:15:56

0

我遇到了同樣的問題。直接將WriteableBitmap分配給Image.Source不起作用。 一些搜索後,我發現了一個解決方法車力量它寫入WritableBitap到使用SaveJpeg方法一個MemoryStream:

using (MemoryStream ms = new MemoryStream()) 
    { 
     asBitmap.SaveJpeg(ms, (int)asBitmap.PixelWidth, (int)asBitmap.PixelHeight, 0, 100); 
     BitmapImage bmp = new BitmapImage(); 
     bmp.SetSource(ms); 
     Image.Source = bmp; 
    } 

這個工作,除非QR碼被顯示在黑暗/淡藍色,而不是黑色/白色。告訴這位朋友,他在Windows手機像素中修正了顏色不是一個字節,而是一個整數。有了這些知識和斑馬線的來源,我改變了ByteMatrix.ToBitmap方法如下:

public WriteableBitmap ToBitmap() 
    { 
     const int BLACK = 0; 
     const int WHITE = -1; 
     sbyte[][] array = Array; 
     int width = Width; 
     int height = Height; 
     var pixels = new byte[width*height]; 

     var bmp = new WriteableBitmap(width, height); 

     for (int y = 0; y < height; y++) 
     { 
      int offset = y*width; 
      for (int x = 0; x < width; x++) 
      { 
       int c = array[y][x] == 0 ? BLACK : WHITE; 
       bmp.SetPixel(x, y, c); 
      } 
     } 

     //Return the bitmap 
     return bmp; 
    } 

這解決了這個問題都沒有,甚至直接分配到WritableBitmap Image.Source。看起來,圖像是正確分配的,但alpha值是透明的,創建jpeg時將其刪除。