使用 ZXing.Net 的 C#:解码 QR 码
C# with ZXing.Net: Decoding the QR code
我是 C# 的新手,使用 ZXing.NetQR 码 解码时遇到问题。应用程序启动时没有错误,但结果字符串中什么也没有。我认为问题可能出在 RGBLuminanceSource().
private static byte[] ToByteArray(Image img)
{
byte[] byteArray = new byte[0];
using (MemoryStream stream = new MemoryStream())
{
img.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
stream.Close();
byteArray = stream.ToArray();
}
return byteArray;
}
private void button1_Click(object sender, EventArgs e)
{
*** SOME OTHER CODE HERE ***
Bitmap BitmapImage = new Bitmap(@"D:.png");
QRCodeReader reader = new QRCodeReader();
LuminanceSource source = new RGBLuminanceSource(ToByteArray(BitmapImage), BitmapImage.Width, BitmapImage.Height);
var binarizer = new HybridBinarizer(source);
var binBitmap = new BinaryBitmap(binarizer);
string result = reader.decode(binBitmap).Text;
*** SOME OTHER CODE HERE ***
}
调用函数即可。另外,将 ... 替换为您的处理
public Result decode(Uri uri)
{
Bitmap image;
try
{
image = (Bitmap) Bitmap.FromFile(uri.LocalPath);
}
catch (Exception)
{
throw new FileNotFoundException("Resource not found: " + uri);
}
using (image)
{
LuminanceSource source;
source = new BitmapLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Result result = new MultiFormatReader().decode(bitmap);
if (result != null)
{
... code found
}
else
{
... no code found
}
return result;
}
}
我是 C# 的新手,使用 ZXing.NetQR 码 解码时遇到问题。应用程序启动时没有错误,但结果字符串中什么也没有。我认为问题可能出在 RGBLuminanceSource().
private static byte[] ToByteArray(Image img)
{
byte[] byteArray = new byte[0];
using (MemoryStream stream = new MemoryStream())
{
img.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
stream.Close();
byteArray = stream.ToArray();
}
return byteArray;
}
private void button1_Click(object sender, EventArgs e)
{
*** SOME OTHER CODE HERE ***
Bitmap BitmapImage = new Bitmap(@"D:.png");
QRCodeReader reader = new QRCodeReader();
LuminanceSource source = new RGBLuminanceSource(ToByteArray(BitmapImage), BitmapImage.Width, BitmapImage.Height);
var binarizer = new HybridBinarizer(source);
var binBitmap = new BinaryBitmap(binarizer);
string result = reader.decode(binBitmap).Text;
*** SOME OTHER CODE HERE ***
}
调用函数即可。另外,将 ... 替换为您的处理
public Result decode(Uri uri)
{
Bitmap image;
try
{
image = (Bitmap) Bitmap.FromFile(uri.LocalPath);
}
catch (Exception)
{
throw new FileNotFoundException("Resource not found: " + uri);
}
using (image)
{
LuminanceSource source;
source = new BitmapLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Result result = new MultiFormatReader().decode(bitmap);
if (result != null)
{
... code found
}
else
{
... no code found
}
return result;
}
}