Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
IBarcodeReader reader = new BarcodeReader();

    for (int i = 100; i < img.Width; i++)
    {
        for (int j = 100; j < img.Height; j++)
        {
            imgw = i; imgh = j;
            Bitmap bmp = new Bitmap(img, imgw, imgh);
            Qrresult = reader.Decode(bmp);
            if (Qrresult != null)
            {
                QRcodevalue = Qrresult.Text;
                bmp.Dispose();
                img.Dispose();
                return QRcodevalue;
            }
            j = j + 100;
            bmp.Dispose();
        }
        i = i + 100;
    }

I have return this code but it is taking too much time to decode the qrcode. Plz help me thanks

What I have tried:

IBarcodeReader reader = new BarcodeReader();

for (int i = 100; i < img.Width; i++)
{
for (int j = 100; j < img.Height; j++)
{
imgw = i; imgh = j;
Bitmap bmp = new Bitmap(img, imgw, imgh);
Qrresult = reader.Decode(bmp);
if (Qrresult != null)
{
QRcodevalue = Qrresult.Text;
bmp.Dispose();
img.Dispose();
return QRcodevalue;
}
j = j + 100;
bmp.Dispose();
}
i = i + 100;
}
Posted
Updated 7-Aug-16 20:27pm
v2
Comments
Richard MacCutchan 6-Aug-16 3:05am    
What is the question?
Member 12148427 8-Aug-16 1:22am    
Dear Richard,
I want to read the qrcode from image. Image is large size and not only contains qrcode, it contains other objects also. Please Help me...Thanks Subrahmanyam
George Jonsson 6-Aug-16 4:01am    
This is not a question, but a code dump.
What kind of problem do you have and what kind of help do you need?
Member 12148427 8-Aug-16 1:19am    
Thanks George,
I want to read the qrcode from image. Image is large size and not only contains qrcode. Please Help me...Thanks Subrahmanyam

From what I see in the code are you trying to find the QR-code by present a part of the image to the reader and see if it can detect the QR-code. If not found you increase the size one pixel and then try again.
Not sure this is the best method, but having nested loops seems to be a bit cumbersome.

It would probably be better to use one of the vision tools available and try to find the coordinates and the size of the QR-code with a blob-tool or similar.
Unfortunately I am bit rusty using the free vision tool, but there are several libraries you can use, such as Emgu CV: OpenCV in .NET (C#, VB, C++ and more)[^] and Accord.NET Machine Learning Framework[^]

That said, you could try to improve the code you have by changing the code a little.
C#
IBarcodeReader reader = new BarcodeReader();

int imgw = 100; 
int imgh = 100;
while ((imgW < img.Width) && (imgh < img.Height))
{
    Bitmap bmp = new Bitmap(img, imgw, imgh);
    Qrresult = reader.Decode(bmp);
    if (Qrresult != null)
    {
        QRcodevalue = Qrresult.Text;
        bmp.Dispose();
        img.Dispose();
        return QRcodevalue;
    }
    bmp.Dispose();
    
    imgw += 100;
    imgh += 100;
}

This code should be faster, but maybe not what you are looking for.

Another option is to create a sliding window that moves through the image.
For this you can use the Bitmap.Clone Method (Rectangle, PixelFormat) (System.Drawing)[^]
This works if you have knowledge about the size of the QR-code beforehand.

Basically what you do is that you create a rectangle of a fixed size that you move from left to right in rows.
When the the sliding window reaches the right side, the x coordinate is reset to 0 and the y coordinate is incremented by the window height.
This also requires nested loops, so it might not be faster, unless you can foresee where in the whole image it is most likely that the QR-code will appear.
If so, you will have a faster algorithm in most cases, but not all.

I hope you at least got some ideas to move forward.
 
Share this answer
 
I don't know what are the needs for reader.Decode, but I know that creating a new bitmap inside a loop is a bad idea because it is time consuming.
You need to find another method that do not require creating a bitmap in the loops.

When it comes to speed optimization, the tool of choice is the profiler.
the profiler is there to tell you how much time you spend in each part of your code and to detect bottlenecks.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900