标题:请教一下VFP 怎么定义Zxing.dll?
只看楼主
shonken
Rank: 2
等 级:论坛游民
帖 子:95
专家分:21
注 册:2017-1-15
结帖率:86.67%
 问题点数:0 回复次数:0 
请教一下VFP 怎么定义Zxing.dll?
网上找了段VB的,但ZXING.DLL的方法是多层的(ZXing.QrCode.QRCodeWriter),不知道怎么定义和调用,请各位大大指点一下,谢谢~
程序代码:
public sealed class QRCodeWriter : Writer
{
    private const int QUIET_ZONE_SIZE = 4;

    /// <summary>
    /// Encode a barcode using the default settings.
    /// </summary>
    /// <param name="contents">The contents to encode in the barcode</param>
    /// <param name="format">The barcode format to generate</param>
    /// <param name="width">The preferred width in pixels</param>
    /// <param name="height">The preferred height in pixels</param>
    /// <returns>
    /// The generated barcode as a Matrix of unsigned bytes (0 == black, 255 == white)
    /// </returns>
    public BitMatrix encode(string contents, BarcodeFormat format, int width, int height)
    {
        return encode(contents, format, width, height, null);
    }

    /// <summary>
    /// </summary>
    /// <param name="contents">The contents to encode in the barcode</param>
    /// <param name="format">The barcode format to generate</param>
    /// <param name="width">The preferred width in pixels</param>
    /// <param name="height">The preferred height in pixels</param>
    /// <param name="hints">Additional parameters to supply to the encoder</param>
    /// <returns>
    /// The generated barcode as a Matrix of unsigned bytes (0 == black, 255 == white)
    /// </returns>
    public BitMatrix encode(string contents, BarcodeFormat format, int width, int height, IDictionary<EncodeHintType, object> hints)
    {
        if (string.IsNullOrEmpty(contents))
        {
            throw new ArgumentException("Found empty contents");
        }
        if (format != BarcodeFormat.QR_CODE)
        {
            throw new ArgumentException("Can only encode QR_CODE, but got " + format);
        }
        if (width < 0 || height < 0)
        {
            throw new ArgumentException("Requested dimensions are too small: " + width + "x" + height);
        }
        ErrorCorrectionLevel errorCorrectionLevel = ErrorCorrectionLevel.L;
        int quietZone = 4;
        if (hints != null)
        {
            if (hints.ContainsKey(EncodeHintType.ERROR_CORRECTION))
            {
                object requestedECLevel = hints[EncodeHintType.ERROR_CORRECTION];
                if (requestedECLevel != null)
                {
                    errorCorrectionLevel = (requestedECLevel as ErrorCorrectionLevel);
                    if (errorCorrectionLevel == null)
                    {
                        switch (requestedECLevel.ToString().ToUpper())
                        {
                        case "L":
                            errorCorrectionLevel = ErrorCorrectionLevel.L;
                            break;
                        case "M":
                            errorCorrectionLevel = ErrorCorrectionLevel.M;
                            break;
                        case "Q":
                            errorCorrectionLevel = ErrorCorrectionLevel.Q;
                            break;
                        case "H":
                            errorCorrectionLevel = ErrorCorrectionLevel.H;
                            break;
                        default:
                            errorCorrectionLevel = ErrorCorrectionLevel.L;
                            break;
                        }
                    }
                }
            }
            if (hints.ContainsKey(EncodeHintType.MARGIN))
            {
                object quietZoneInt = hints[EncodeHintType.MARGIN];
                if (quietZoneInt != null)
                {
                    quietZone = Convert.ToInt32(quietZoneInt.ToString());
                }
            }
        }
        return renderResult(Encoder.encode(contents, errorCorrectionLevel, hints), width, height, quietZone);
    }

    private static BitMatrix renderResult(QRCode code, int width, int height, int quietZone)
    {
        ByteMatrix input = code.Matrix;
        if (input == null)
        {
            throw new InvalidOperationException();
        }
        int inputWidth = input.Width;
        int inputHeight = input.Height;
        int qrWidth = inputWidth + (quietZone << 1);
        int qrHeight = inputHeight + (quietZone << 1);
        int outputWidth = Math.Max(width, qrWidth);
        int outputHeight = Math.Max(height, qrHeight);
        int multiple = Math.Min(outputWidth / qrWidth, outputHeight / qrHeight);
        int leftPadding = (outputWidth - inputWidth * multiple) / 2;
        int num = (outputHeight - inputHeight * multiple) / 2;
        BitMatrix output = new BitMatrix(outputWidth, outputHeight);
        int inputY = 0;
        int outputY = num;
        while (inputY < inputHeight)
        {
            int inputX = 0;
            int outputX = leftPadding;
            while (inputX < inputWidth)
            {
                if (input[inputX, inputY] == 1)
                {
                    output.setRegion(outputX, outputY, multiple, multiple);
                }
                inputX++;
                outputX += multiple;
            }
            inputY++;
            outputY += multiple;
        }
        return output;
    }
}



程序代码:
Shared Function MakeQR(ByVal qrtext As String, Optional ByVal width As Integer = 800, Optional ByVal height As Integer = 800, Optional ByVal margin As Integer = 1) As Bitmap
    Dim writer As New ZXing.BarcodeWriter
    writer.Format = ZXing.BarcodeFormat.QR_CODE

    Dim opt As New ZXing.QrCode.QrCodeEncodingOptions
    opt.DisableECI = True '设置为True才可以调整编码
    opt.CharacterSet = "UTF-8" '文本编码,建议设置为UTF-8
    opt.Width = width    '宽度
    opt.Height = height    '高度
    opt.Margin = margin    '边距,貌似不是像素格式,因此不宜设置过大

    writer.Options = opt

    Return writer.Write(qrtext)
End Function

Shared Function MakeQR(ByVal qrtext As String, ByVal logo As Bitmap, Optional ByVal width As Integer = 800, Optional ByVal height As Integer = 800, Optional ByVal margin As Integer = 1) As Bitmap
    If logo Is Nothing Then
        Return MakeQR(qrtext, width, height, margin)
    End If

    Dim writer As New ZXing.MultiFormatWriter
    Dim hint As New Dictionary(Of ZXing.EncodeHintType, Object)()
    hint.Add(ZXing.EncodeHintType.CHARACTER_SET, "UTF-8")
    hint.Add(ZXing.EncodeHintType.MARGIN, margin)
    hint.Add(ZXing.EncodeHintType.ERROR_CORRECTION, ZXing.QrCode.Internal.ErrorCorrectionLevel.H)

    ' 生成二维码
    Dim bm As  = writer.encode(qrtext, ZXing.BarcodeFormat.QR_CODE, width, height, hint)
    Dim barcodeWriter = New ZXing.BarcodeWriter()
    Dim bmp As Bitmap = barcodeWriter.Write(bm)

    '获取二维码实际尺寸(去掉二维码两边空白后的实际尺寸)
    Dim rectangle As Integer() = bm.getEnclosingRectangle()

    '计算插入图片的大小和位置
    Dim middleW As Integer = Math.Min((rectangle(2) / 3.5), logo.Width)
    Dim middleH As Integer = Math.Min((rectangle(3) / 3.5), logo.Height)
    Dim middleL As Integer = (bmp.Width - middleW) / 2
    Dim middleT As Integer = (bmp.Height - middleH) / 2

    '将img转换成bmp格式,否则后面无法创建Graphics对象
    Dim bmpimg As New Bitmap(bmp.Width, bmp.Height, Imaging.PixelFormat.Format32bppArgb)

    Using g As Graphics = Graphics.FromImage(bmpimg)
        g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
        g.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
         = (bmp, 0, 0)
    End Using

    '将二维码插入图片
    Using myGraphic As Graphics = Graphics.FromImage(bmpimg)
        '白底
        myGraphic.FillRectangle(Brushes.White, middleL, middleT, middleW, middleH)
        myGraphic.DrawImage(logo, middleL, middleT, middleW, middleH)
    End Using

    bmp.Dispose()

    Return bmpimg

End Function

Shared Function ReadQR(ByVal bmp As Bitmap) As String
    Dim reader As New ZXing.BarcodeReader
    reader.Options.CharacterSet = "UTF-8"

    Dim ret As ZXing.Result = reader.Decode(bmp)
    If ret Is Nothing Then
        Return Nothing
    Else
        Return ret.Text
    End If
End Function


[此贴子已经被作者于2018-12-22 21:01编辑过]

搜索更多相关主题的帖子: int the new Dim Integer 
2018-12-22 20:59



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-492225-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.078586 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved