当前位置: 首页 > article >正文

vb.net+zxing.net随机彩色二维码、条形码

需要zxing库支持ZXing.NET Generate QR Code & Barcode in C# Alternatives | IronBarcode

效果图:

思路:先生成1个单位的二维码,然后再通过像素填充颜色,颜色数组要通过洗牌算法

洗牌算法

Dim shuffledCards As New List(Of Color)

 Sub GenColor()
     shuffledCards.Clear()

     Dim cards() = {Color.Red, Color.Blue, Color.Green,  Color.Black, Color.Brown}

     For i = 0 To 40
         shuffledCards.AddRange(ShuffleArray(cards))
     Next

 End Sub

 ' Fisher-Yates洗牌算法实现  
 Function ShuffleArray(ByVal array() As Color) As Color()
     Dim currentIndex As Integer = array.Length
     Dim random As New Random()

     ' 当还剩有元素未洗牌时  
     While currentIndex > 0
         ' 选取一个0到currentIndex之间的随机索引  
         Dim randomIndex As Integer = random.Next(currentIndex)
         currentIndex -= 1

         ' 交换当前元素和随机索引处的元素  
         Dim temp As Color = array(currentIndex)
         array(currentIndex) = array(randomIndex)
         array(randomIndex) = temp
     End While

     ' 返回洗牌后的数组  
     Return array
 End Function

色块识别、填充算法(二维码的生成):

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim qr = New Bitmap(GenByZXingNet_Color(Content.Text))
    If CBRandColor.Checked = True Then
        GenColor()
    Dim k As Integer
        For i = 0 To qr.Height - 1
            For j = 0 To qr.Width - 1
                Dim c = qr.GetPixel(j, i)
                If CInt(c.R) + CInt(c.G) + CInt(c.B) = 0 Then
                    qr.SetPixel(j, i, shuffledCards(k))
                End If
                k += 1
                If k > shuffledCards.Count - 1 Then
                    k = 0
                End If
            Next
        Next
    End If
    Dim zk As Integer = CInt(ZoomK.Value)
    Dim NewQr = New Bitmap(qr.Width * zk, qr.Height * zk)
    For i = 0 To qr.Height - 1
        For j = 0 To qr.Width - 1
            Dim c = qr.GetPixel(j, i)
            Dim g = Graphics.FromImage(NewQr)
            g.FillRectangle(New SolidBrush(c), New Rectangle(j * zk, i * zk, zk, zk))
        Next
    Next
    DestImg.Image = NewQr
End Sub
Public Shared Function GenByZXingNet_Color(ByVal msg As String, ByVal Optional codeSizeInPixels As Integer = 250) As Bitmap
    Dim writer As BarcodeWriter = New BarcodeWriter()
    'writer.Renderer = New ZXing.Rendering.BitmapRenderer With {
    '        .Background = Color.White,
    '       .Foreground = Color.Black
    '   }
    writer.Format = BarcodeFormat.QR_CODE

    'writer.Options.Hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8")
    'writer.Options.Hints.Add(EncodeHintType.ERROR_CORRECTION, ZXing.QrCode.Internal.ErrorCorrectionLevel.H)
    writer.Options.Height = 1
    writer.Options.Width = 1
    writer.Options.Margin = 0
    Dim img As Bitmap = writer.Write(msg)
    Return img
End Function

条形码的生成

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    Dim options = New ZXing.Common.EncodingOptions()

    'options.Height = 120
    'options.Width = 200
    Dim Writer = New ZXing.BarcodeWriter()

    'writer.Options = options
    Writer.Format = ZXing.BarcodeFormat.CODE_128

    Dim qr = Writer.Write(Content.Text)


    DestImg.Image = qr
End Sub

保存

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Dim save As New SaveFileDialog
    save.Filter = "PNG File|*.png|JPG File|*.jpg|BMP File|*.bmp|All File|*.*"
    save.Title = "选择保存位置"
    save.FileName = Content.Text
    If save.ShowDialog() = DialogResult.OK Then
        DestImg.Image.Save(save.FileName)
    End If

End Sub


http://www.kler.cn/a/272330.html

相关文章:

  • N个utils(sql)
  • MySQL面试题2025 每日20道【其四】
  • 使用Torchvision框架实现对象检测:从Faster-RCNN模型到自定义数据集,训练模型,完成目标检测任务。
  • IoTDB 常见问题 QA 第四期
  • Qt QML专栏目录结构
  • 算法(蓝桥杯)贪心算法5——删数问题的解题思路
  • 【学习笔记】云原生的关键技术初步
  • Puppet 代码入门:清单和模块
  • 2024 年(第 12 届)“泰迪杯”数据挖掘挑战赛——B 题:基于多模态特征融合的图像文本检索完整思路与源代码分享
  • 【Java初阶(一)】初识Java
  • 测试人员Bug书写规范
  • 【递归搜索回溯专栏】专题二:二叉树中的深搜----二叉树剪枝
  • 24计算机考研调剂 | 武汉科技大学
  • 安全架构设计
  • 静默安装OGG21.3微服务版本FOR ORACLE版本
  • [java基础揉碎]断点调试
  • 多核(CPU)系统中并行计算Atomic原子操作与缓存一致性(memory/cache coherency)
  • C++/CLI学习笔记5(快速打通c++与c#相互调用的桥梁)
  • Linux基础开发工具之yum与vim
  • golang常用库之-golang常用库之-ladon包 | 基于策略的访问控制
  • C++ 接口的实现,及作用通俗理解方式
  • DevOps 环境预测测试中的机器学习
  • MySQL主从数据库简单搭建
  • 谈谈对数据库索引的认识
  • 高可用的机房监控运维管理系统,实时把握机房脉搏
  • java----网络编程(一)