Generate barcode with ZXing.net

落花浮王杯 提交于 2019-12-12 14:23:35

问题


I'm trying to generate a barcode using ZXing.NET for dot net core asp.net application. I can't figure out how to display text with the barcode and documentation seems to be really, really lacking. Does anyone have an idea how to make it work?

This is the code I have (mostly taken from another post on SO):

BarcodeWriterPixelData writer = new BarcodeWriterPixelData()
{
    Format = BarcodeFormat.CODE_128,
    Options = new EncodingOptions
    {
        Height = 400,
        Width = 800,
        PureBarcode = false, // this should indicate that the text should be displayed, in theory. Makes no difference, though.
        Margin = 10
    }
};

var pixelData = writer.Write("test text");

using (var bitmap = new Bitmap(pixelData.Width, pixelData.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb))
{
    using (var ms = new System.IO.MemoryStream())
    {
        var bitmapData = bitmap.LockBits(new Rectangle(0, 0, pixelData.Width, pixelData.Height), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
        try
        {
            System.Runtime.InteropServices.Marshal.Copy(pixelData.Pixels, 0, bitmapData.Scan0, pixelData.Pixels.Length);
        }
        finally
        {
            bitmap.UnlockBits(bitmapData);
        }

        bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
        return File(ms.ToArray(), "image/jpeg");
    }
}

This gets me a barcode, but no content.

Or, suggestions of better/easier to use/better-documented libraries would be appreciated too.


回答1:


You don't need to copy those pixels data to another stream manually. Always prefer to using methods provided by the interface , namely the Save() method.

public void YourActionMethod()
{
    BarcodeWriter writer = new BarcodeWriter(){
        Format = BarcodeFormat.CODE_128,
        Options = new EncodingOptions {
            Height = 400,
            Width = 800,
            PureBarcode = false,
            Margin = 10,
        },
    };

    var bitmap = writer.Write("test text");
    bitmap.Save(HttpContext.Response.Body,System.Drawing.Imaging.ImageFormat.Png);
    return; // there's no need to return a `FileContentResult` by `File(...);`
}

Demo :




回答2:


Not every available renderer implementation supports the output of the content below the barcode (f.e. PixelData renderer doesn't support it). You should use one of the specific implementation for different image libraries. For example the following bindings provider a renderer (and a specific BarcodeWriter) with support for content output: https://www.nuget.org/packages/ZXing.Net.Bindings.CoreCompat.System.Drawing https://www.nuget.org/packages/ZXing.Net.Bindings.Windows.Compatibility https://www.nuget.org/packages/ZXing.Net.Bindings.ZKWeb.System.Drawing https://www.nuget.org/packages/ZXing.Net.Bindings.SkiaSharp



来源:https://stackoverflow.com/questions/54067764/generate-barcode-with-zxing-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!