【发布时间】:2020-09-09 11:46:43
【问题描述】:
目标:
我的目标是读取质量较差的条码。如果条形码质量最高,则代码可以正常工作。
图片:
代码:
裁剪条码:
private Point LocationXY;
private Point LocationX1Y1;
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
LocationXY = e.Location;
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
LocationX1Y1 = e.Location;
pictureBox1.Invalidate();
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
LocationX1Y1 = e.Location;
pictureBox1.Invalidate();
pictureBox2.Invalidate();
}
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Rectangle currentSelection = GetRect();
if (currentSelection != Rectangle.Empty)
{
e.Graphics.DrawRectangle(Pens.Red, currentSelection);
}
}
private void pictureBox2_Paint(object sender, PaintEventArgs e)
{
var src = GetRect();
if (src == Rectangle.Empty) return;
var des = new Rectangle(0, 0, src.Width, src.Height);
e.Graphics.DrawImage(pictureBox1.Image,
des, src, GraphicsUnit.Pixel);
}
private Rectangle GetRect()
{
return new Rectangle(
Math.Min(LocationXY.X, LocationX1Y1.X),
Math.Min(LocationXY.Y, LocationX1Y1.Y),
Math.Abs(LocationXY.X - LocationX1Y1.X),
Math.Abs(LocationXY.Y - LocationX1Y1.Y)
);
}
private Bitmap GetCroppedImage()
{
var des = GetRect();
if (des == Rectangle.Empty) return null;
var b = new Bitmap(des.Width, des.Height);
using (var g = Graphics.FromImage(b))
{
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.SmoothingMode = SmoothingMode.HighQuality;
g.DrawImage(pictureBox1.Image, new Rectangle(0, 0, des.Width, des.Height), des, GraphicsUnit.Pixel);
}
return b;
}
保存图片并能返回图片的类:
public class BarcodeImage
{
private static Image _image;
public static Image _Image
{
get
{
return _image;
}
set
{
_image = value;
}
}
}
这里我点击一个按钮将图像保存到类并检查条形码:
private void checkBarcode_Click(object sender, EventArgs e)
{
BarcodeImage._Image = GetCroppedImage();
ZXing.BarcodeReader reader = new ZXing.BarcodeReader();
var result = reader.Decode((Bitmap)BarcodeImage._Image);
if (result != null)
{
MessageBox.Show(result.ToString());
}
}
使用代码:
这里我突出显示条形码并将其保存到picturebox2。
接下来点击checkBarcode_Click,它应该会显示条形码值 - 但事实并非如此。因为画质。
我已经用高质量的条形码图像对其进行了测试,效果很好!
问题:
如何提高裁剪图像的质量并将条形码值返回给我?
编辑:2020 年 5 月 29 日
我在这个社区提出了一个新问题,看看我是否可以增加裁剪图像的大小。但这并没有改善任何事情:
https://stackoverflow.com/a/62068397/12485722
高质量条码:
图片是从特定程序中取出来的。
我将图像打印在屏幕上,它们必须是这样的:
但它没有检测到条形码。
这是在程序上使用 2 倍放大的图像:
很遗憾,我无法使用 x2 放大选项,因为某些数据会在图像上移动,无法提供准确的最终图像。最重要的是,条形码被识别!
【问题讨论】:
-
评论不用于扩展讨论;这个对话是moved to chat。