Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

improving algorithm to be faster - scanning image from file

My code here works but takes a couple seconds longer and with larger files it takes longer and i wanted to know if anyone can take a look over what i have and suggest any kind of improvement that will help make this faster.

Purpose:

This is to scan a pdf file and search for the bitmap image of a QR code and it will return the code for it (decode)

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);
    }
like image 659
BB987 Avatar asked Feb 18 '23 09:02

BB987


1 Answers

In the code, which you past everything is ok. The problem must be in QRCodeDecoder.decode function. If you are scanning image pixel by pixel, via Bitmap.GetPixel function, it will waste a lot of time. Better way will be to use unsafe code and convert bitmap to BitmapData.

like image 87
Vahagn Nahapetyan Avatar answered Mar 16 '23 10:03

Vahagn Nahapetyan