Comments (4)
I timed it, one QR code takes approx. 35 msec, and the image is approx 1MB big. With 2000 of them to run, I get 70 seconds runtime. I think that getting BW 1 bit image directly will cut this time a lot.
The cloning and changing to B/W image takes 2 msec.
from zxing.net.
There is no option available but you can write your own renderer. Take a look at the source code of the BitmapRenderer class. Implement your own BitmapRenderer1bpp, change the line
var bmp = new Bitmap(width, height, PixelFormat.Format24bppRgb);
and this one
var bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);
of the original BitmapRenderer and the different places of the pixel assignments
pixels[index++] = color.B;
pixels[index++] = color.G;
pixels[index++] = color.R;
After that you can use your custom renderer with the BitmapWriter:
var x = ZXing.QrCode.Internal.ErrorCorrectionLevel.H;
var barcodeWriter = new BarcodeWriter
{
Format = barcodeFormat,
Options = new ZXing.QrCode.QrCodeEncodingOptions
{
CharacterSet = "ISO-8859-2",
Width = 600,
Height = 600,
ErrorCorrection = x,
QrVersion = 15
},
Renderer = new BitmapRenderer1bpp()
};
var bitmap = barcodeWriter.Write("ASDASD");
from zxing.net.
Great. Thank you!
from zxing.net.
I had a need for this as well and the class I developed is at bottom. It does not support Options or text content but otherwise seems to work fine. Suggestions for improvement are welcome. Not thoroughly tested and only tested with QR codes. Usage is:
var bcWriter = new BarcodeWriter { Renderer = new BitmapRenderer1bpp() };
BitMatrix bitmatrix = writer.encode(qrText, BarcodeFormat.QR_CODE, width, height, myHints);
Bitmap qrMonoBitmap = bcWriter.Write(bitmatrix);
Too bad there is not native support for monochrome QR because they are vastly smaller and substantially quicker to create. And of course black and white output is usually what's desired, not color.
Here's the custom class:
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using ZXing.Common;
using ZXing.OneD;
namespace ZXing.Rendering
{
/// <summary>
/// Renders a <see cref="BitMatrix" /> to a <see cref="Bitmap" /> image
/// </summary>
public class BitmapRenderer1bpp : IBarcodeRenderer<Bitmap>
{
static BitmapRenderer1bpp()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="BitmapRenderer"/> class.
/// </summary>
public BitmapRenderer1bpp()
{
}
/// <summary>
/// Renders the specified matrix.
/// </summary>
/// <param name="matrix">The matrix.</param>
/// <param name="format">The format.</param>
/// <param name="content">The content.</param>
/// <returns></returns>
public Bitmap Render(BitMatrix matrix, BarcodeFormat format, string content)
{
return Render(matrix, format, content, null);
}
/// <summary>
/// Renders the specified matrix.
/// </summary>
/// <param name="matrix">The matrix.</param>
/// <param name="format">The format.</param>
/// <param name="content">The content.</param>
/// <param name="options">The options.</param>
/// <returns></returns>
virtual public Bitmap Render(BitMatrix matrix, BarcodeFormat format, string content, EncodingOptions options)
{
var width = matrix.Width;
var height = matrix.Height;
// create the bitmap and lock the bits because we need the stride
// which is the width of the image and possible padding bytes
var bmp = new Bitmap(width, height, PixelFormat.Format1bppIndexed);
var bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.WriteOnly, PixelFormat.Format1bppIndexed);
try
{
var pixels = new byte[bmpData.Stride * height];
// going through the lines of the matrix
for (int y = 0; y < matrix.Height; y++)
{
// going through the columns of the current line
for (var x = 0; x < matrix.Width; x++)
{
byte bitValue = (byte)(matrix[x, y] ? 0 : 1);
if (bitValue == 1)
{
pixels[y * bmpData.Stride + (x / 8)] |= (byte)(0x80 >> (x % 8));
}
}
}
//Copy the data from the byte array into BitmapData.Scan0
Marshal.Copy(pixels, 0, bmpData.Scan0, pixels.Length);
}
finally
{
//Unlock the pixels
bmp.UnlockBits(bmpData);
}
return bmp;
}
}
}
from zxing.net.
Related Issues (20)
- How to improve recognition rate if the barcode is too small? HOT 7
- Need example for QR Scanner on .Net core HOT 1
- how to generate a GS1-128 barcode with parentheses HOT 1
- Unable to read QRCode from scan HOT 1
- Unable to Scan the QRCode in Unity Webgl HOT 1
- UTF-8 not properly detected in some cases - german umlauts corrupted (ASSUME_SHIFT_JIS broken with .Net Core) HOT 9
- MSI barcode reading fails
- RobiniaDocs API Explorer
- A different CODE_39 barcode HOT 1
- Add some unsafe methods? HOT 1
- 0.16.6 vs 0.16.9 HOT 1
- Missing BitmapLuminanceSource in the ZXing.Net Nuget Package: HOT 1
- Version out dated while opening in vs22, using version 4.0 HOT 3
- Scannig QR Code with Oppo devices doesn't work HOT 2
- BitArray.xor() should check length of both arrays HOT 3
- A small log bug HOT 1
- 如何设置条形码的尺寸以及密度 HOT 1
- Renderer for net8.0-android? HOT 4
- ZXing.Net Bindings for SkiaSharp Package missing Linux HOT 4
- Upgrade SkiaSharp - current version had Vulnerabilities HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from zxing.net.