【发布时间】:2013-02-14 18:35:18
【问题描述】:
我的代码在这里可以工作,但需要几秒钟的时间,而对于较大的文件,需要的时间更长,我想知道是否有人可以查看我所拥有的内容并提出任何有助于加快速度的改进。
目的:
这是扫描一个pdf文件并搜索一个二维码的位图图像,它会返回它的代码(解码)
private void ScanQRPdf(string imagePath)
{
foreach (var item in Directory.GetFiles(imagePath))
{
if (Path.GetExtension(item).ToLower() == ".png")
{
Bitmap b = new Bitmap(imagePath);
try
{
QRCodeDecoder decoder = new QRCodeDecoder();
String decodedString = decoder.decode(new QRCodeBitmapImage(b));
rtbpdfData.Text += decodedString + "\n";
}
catch (Exception ex)
{
}
}
}
}
static void AddQRTag(PdfSharp.Drawing.XGraphics gfx, int xPosition, int yPosition, string QRdata, string HRdata)
{
gfx.DrawRectangle(XBrushes.White, xPosition, yPosition, xPosition + 55, yPosition + 85);
PdfSharp.Drawing.XImage xImage =
PdfSharp.Drawing.XImage.FromGdiPlusImage(BuildQR(QRdata.ToUpper(), 3,
QRCodeEncoder.ENCODE_MODE.ALPHA_NUMERIC, 2, QRCodeEncoder.ERROR_CORRECTION.M));
gfx.DrawImage(xImage, xPosition + 5, yPosition + 5, xImage.PixelWidth * .8, xImage.PixelWidth * .8);
XFont font = new XFont("OCR B", 6);
XTextFormatter tf = new XTextFormatter(gfx);
tf.Alignment = XParagraphAlignment.Left;
XRect layout = new XRect(xPosition + 5, yPosition + 55, 55, 30);
tf.DrawString(HRdata.ToUpper(), font, XBrushes.Black, layout, XStringFormats.TopLeft);
}
【问题讨论】:
-
慢代码大概在
QRCodeDecoder类。在这里您无能为力。 -
要问自己的问题:解码器会处理您的
Bitmaps 吗? -
1 建议:删除
catch{}。 -
你能把链接发到
QRCodeDecoder源代码吗? (我假设您使用的是 O/S 库) -
获取分析器。它会告诉你瓶颈在哪里。猜测通常是浪费时间。
标签: c# algorithm image-processing bitmap