【问题标题】:Is there a way with ZXing.net to get an integer or boolean array?ZXing.net 有没有办法获取整数或布尔数组?
【发布时间】:2023-03-21 05:55:01
【问题描述】:

我想在我的 CAM 软件中生成 QR 码(或 DM 码)。对于这个任务,我不需要位图,而是需要像二维整数数组那样的坐标。

我的意思的一个例子:

我试过 ZXing.net 但我只能创建位图。 ZXing.net 有没有办法获取整数或布尔数组?或者是否存在其他可以这样编码的库?

【问题讨论】:

    标签: c# qr-code zxing.net


    【解决方案1】:

    以下是获取 BitMatrix 或 2D int 数组而不是位图的三种方法。 在我看来,第二个是最好的。但这取决于您的需求。

        // first way to get a BitMatrix instead of a bitmap
        var writer1 = new ZXing.QrCode.QRCodeWriter();
        var bitmatrix = writer1.encode(
            "<content>",
            BarcodeFormat.QR_CODE,
            1,
            1,
            new System.Collections.Generic.Dictionary<EncodeHintType, object>());
    
        // second way to get a BitMatrix instead of a bitmap
        var writer2 = new ZXing.BarcodeWriter<BitMatrix>
        {
            Format = BarcodeFormat.QR_CODE,
            Renderer = new BitMatrixRenderer()
        };
        var bitmatrix2 = writer2.Write("content");
    
        // third way to get a 2D int array instead of a bitmap
        var writer3 = new ZXing.BarcodeWriter<int[,]>
        {
            Format = BarcodeFormat.QR_CODE,
            Renderer = new BitArray2DRenderer()
        };
        var bitarray2D = writer3.Write("content");
    
        public class BitMatrixRenderer : IBarcodeRenderer<BitMatrix>
        {
            public BitMatrix Render(BitMatrix matrix, BarcodeFormat format, string content)
            {
                return matrix;
            }
    
            public BitMatrix Render(BitMatrix matrix, BarcodeFormat format, string content, EncodingOptions options)
            {
                return matrix;
            }
        }
    
        public class BitArray2DRenderer : IBarcodeRenderer<int[,]>
        {
            public int[,] Render(BitMatrix matrix, BarcodeFormat format, string content)
            {
                return Render(matrix, format, content, null);
            }
    
            public int[,] Render(BitMatrix matrix, BarcodeFormat format, string content, EncodingOptions options)
            {
                var result = new int[matrix.Width, matrix.Height];
                for (var y = 0; y < matrix.Height; y++)
                {
                    for (var x = 0; x < matrix.Width; x++)
                    {
                        result[x, y] = matrix[x, y] ? 1 : 0;
                    }
                }
    
                return result;
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2015-04-01
      • 2020-08-26
      • 1970-01-01
      • 2020-04-09
      • 2017-02-07
      • 2014-03-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多