gpt4 book ai didi

c# - ZXing-Core BitMatrix转BitMap

转载 作者:行者123 更新时间:2023-12-05 04:10:45 26 4
gpt4 key购买 nike

我正在使用 ZXing-Core(0.14.0.2) 创建一个条形码,它给我一个 BitMatrix,但是我还没有找到关于如何从 中提取图像部分的文档BitMatrix 并将其用作 BitMap 这样我就可以将其融入更大的图像中。最终我想得到的结果是这样的:

White Image With Barcode at the bottom

我目前的代码是:

img = new Bitmap(300, 375);
drawing = Graphics.FromImage(img);
var barCode = new Code128Writer().encode(packageModel.TrackingId.PrintValue, BarcodeFormat.CODE_128, 280, 70);
src = transform **barCode** to **Drawing.Image**
drawing.DrawImage(src, new Rectangle(10, 255, 280, 70));

更新我现在有了这段代码,但是我收到了一个错误You have to set a renderer instance. 我不明白为什么接口(interface)没有自己实例化这个类。没有可用的文档来揭示这是如何工作的。我无法使用 Write 函数,因为 Rendering 设置为 null,但我不确定如何实例化它。

IBarcodeWriterGeneric<Image> barcodeWriterGeneric = new BarcodeWriterGeneric<Image>
{

Format = BarcodeFormat.CODE_128,
Options = new EncodingOptions
{
Width = 280,
Height = 70
},
Renderer = new PixelData() //HOW DOES THE RENDERER WORK????
};

var test = barcodeWriterGeneric.Write("WORKS");
drawing.DrawImage(test, new Rectangle(10, 255, 280, 70));

最佳答案

创建一个实现 IBarcodeRenderer<TOut> 的渲染器.这个适用于我的 iOS。

var writer = new BarcodeWriter<UIImage>()
{
Format = ZXing.BarcodeFormat.CODE_128,
Options = new EncodingOptions
{
Height = height,
Width = width,
Margin = 0
},
Renderer = new BarcodeRenderer()
};

var image = writer.Write(barcodeValue);



private class BarcodeRenderer : IBarcodeRenderer<UIImage>
{
public UIImage Render(BitMatrix matrix, ZXing.BarcodeFormat format, string content)
{
return RenderMatrix(matrix);
}

public UIImage Render(BitMatrix matrix, ZXing.BarcodeFormat format, string content, EncodingOptions options)
{
return RenderMatrix(matrix);
}
}

/// <summary>
/// Renders the bitmatrix.
/// </summary>
private static UIImage RenderMatrix(BitMatrix matrix)
{
var width = matrix.Width;
var height = matrix.Height;

var black = new CGColor(0f, 0f, 0f);
var white = new CGColor(1.0f, 1.0f, 1.0f);

UIGraphics.BeginImageContext(new CGSize(width, height));
var context = UIGraphics.GetCurrentContext();

for (var x = 0; x < width; x++)
{
for (var y = 0; y < height; y++)
{
context.SetFillColor(matrix[x, y] ? black : white);
context.FillRect(new CGRect(x, y, 1, 1));
}
}

var img = UIGraphics.GetImageFromCurrentImageContext();

UIGraphics.EndImageContext();

return img;
}

关于c# - ZXing-Core BitMatrix转BitMap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44027328/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com