-1

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

我的意思的一个例子:

我的意思的一个例子

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

4

1 回答 1

0

以下是获取 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;
        }
    }
于 2020-01-19T10:53:19.753 回答