使用C#对大图像进行Qrcode [英] Qrcode large image using C#

查看:63
本文介绍了使用C#对大图像进行Qrcode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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;
    }



我已经返回此代码但是解码qrcode花费了太多时间。 Plz帮我谢谢



我尝试过:



IBarcodeReader阅读器= new BarcodeReader();



for(int i = 100; i< img.Width; i ++)

{

for(int j = 100; j< img.Height; j ++)

{

imgw = i; imgh = j;

位图bmp =新位图(img,imgw,imgh);

Qrresult = reader.Decode(bmp);

if(Qrresult!= null)

{

QRcodevalue = Qrresult.Text;

bmp.Dispose();

img.Dispose();

返回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;
}

推荐答案

从我在代码中看到的是,您试图通过向读者呈现图像的一部分来查找QR码,并查看它是否能够检测到QR码。如果没有找到你增加一个像素的大小,然后再试一次。

不确定这是最好的方法,但嵌套循环似乎有点麻烦。



使用其中一种视觉工具可能会更好,并尝试使用blob工具或类似工具查找QR码的坐标和大小。

不幸的是,使用免费视觉工具我有点生疏,但你可以使用几个库,例如 Emgu CV:OpenCV在.NET(C#,VB,C ++等) [ ^ ]和 Accord.NET机器学习框架 [ ^ ]



也就是说,您可以尝试改进您拥有的代码更改代码。

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.
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;
}



此代码应该更快,但可能不是您想要的。



另一种选择是创建一个在图像中移动的滑动窗口。

为此你可以使用 Bitmap.Clone方法(Rectangle,PixelFormat)(System.Drawing) [ ^ ]

如果您了解大小QR码预先。



基本上你所做的就是你创建一个固定大小的矩形,你可以从左到右排成行。

当滑动窗口到达右侧时,x坐标重置为0,y坐标增加窗口高度。

这也需要嵌套循环,因此它可能不是更快,除非你可以预见整个图像中的哪个位置最有可能他会出现QR码。

如果是这样,在大多数情况下你会有更快的算法,但不是全部。



我希望你至少有一些想法要继续前进。


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.


我不知道 reader.Decode 的需求是什么,但是我知道在循环中创建一个新的位图是一个坏主意,因为它很耗时。

你需要找到另一个不需要在循环中创建位图的方法。



谈到速度优化时,选择的工具就是剖析器。

剖析器可以告诉你多长时间您花在代码的每个部分并检测瓶颈
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.


这篇关于使用C#对大图像进行Qrcode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆