2013-01-31 62 views
0
using Gma.QrCodeNet.Encoding; 
    using Gma.QrCodeNet.Encoding.Windows.Controls; 

    private QrEncoder Encoder = new QrEncoder(ErrorCorrectionLevel.H); 
    private Renderer renderer = new Renderer(15); 

    BitMatrix qr = Encoder.Encode("12345").Matrix; 
    Size size = Renderer.Measure(qr.Width); 

    Bitmap result = new Bitmap(size.Width, size.Width); 
    try 
    { 
     using (Graphics graphics = Graphics.FromImage(result)) 
     renderer.Draw(graphics,qr); 
     return result; 
    } 
    catch 
    { 
     result.Dispose(); 
     throw; 
    } 

我可以用我上面得到的东西成功生成QR码。但是,它始终使它们在代码的各个方面都有空白。有没有简单的方法来删除空白,或者,甚至更好地生成代码没有它?如何使用QrCode.net库生成无空白的QR码?

+0

我假设您知道QR码周围的白色区域在技术上属于代码的一部分(“安静区域”),删除它可能会导致成功识别和读取代码的问题读者? – Iridium

回答

0

如果库不直接支持它,那么你总是可以添加一个步骤来裁剪结果位图。

using (Graphics graphics = Graphics.FromImage(result)) 
    renderer.Draw(graphics,qr); 

Rectangle cropRectangle = Rectangle(
    borderWidth,borderHeight,result.Width-borderWidth, 
    result.Height-borderHeight);  
return result.Clone(cropRectangle, bmp.PixelFormat);