com.google.zxing.binarybitmap处理位图图像时出错

本文关键字:图像 出错 位图 处理 google zxing binarybitmap com | 更新日期: 2023-09-27 18:02:48

我有一种方法,可以读取位图图像来解码文档中某个区域的qr码(查看qr码的四个角(因为我的代码是这样的,它总是会碰到错误消息,我知道它找不到位图,但我想接受这个错误,并以执行剩余代码的方式进行翻译,即旋转文档再次查找qr位图图像。

代码:

Bitmap[] corners = new Bitmap[] { bandImg1, bandImg2, bandImg3, bandImg4 };
                    string QRinfo = "";
                    for (int i = 0; i < corners.Length; ++i)
                    {
                        string tempQRinfo = Process(corners[i]);
                        if (tempQRinfo == null)
                        {
                            QRinfo = tempQRinfo;
                            switch (i)
                            {
                                case 0: break; //upper left
                                case 1: fullImg.RotateFlip(RotateFlipType.Rotate270FlipNone); break;
                                case 2: fullImg.RotateFlip(RotateFlipType.Rotate90FlipNone); break;
                                case 3: fullImg.RotateFlip(RotateFlipType.Rotate180FlipNone); break;
                            }
                            break;
                        }
                    }

在找不到图像时导致错误的处理方法

 public string Process(Bitmap bitmap)
    {
        var reader = new com.google.zxing.qrcode.QRCodeReader();
        try
        {
            LuminanceSource source = new RGBLuminanceSource(bitmap, bitmap.Width, bitmap.Height);
            var binarizer = new HybridBinarizer(source);
            var binBitmap = new BinaryBitmap(binarizer);
            return reader.decode(binBitmap).Text;
        }
        catch (Exception e)
        {
            return e.Message;
        }
    }

帮助:我想翻译错误消息,让文档在包含qr码的所有四个角落进行搜索,然后如上所示旋转。

com.google.zxing.binarybitmap处理位图图像时出错

Process方法中:

public string Process(Bitmap bitmap)
{
    var reader = new com.google.zxing.qrcode.QRCodeReader();
    try
    {
        LuminanceSource source = new RGBLuminanceSource(bitmap, bitmap.Width, bitmap.Height);
        var binarizer = new HybridBinarizer(source);
        var binBitmap = new BinaryBitmap(binarizer);
        return reader.decode(binBitmap).Text;
    }
    catch (Exception e)
    {
        //catch the exception and return null instead
        return null;
    }
}


然后在其他代码中:

Bitmap[] corners = new Bitmap[] { bandImg1, bandImg2, bandImg3, bandImg4 };
string QRinfo = null;
for (int i = 0; i < corners.Length; ++i)
{
    string tempQRinfo = Process(corners[i]);
    if (tempQRinfo != null) //if the string is NOT null, then we found the QR. If it is null, then the for loop will continue searching if it has more corners to look at
    {
        QRinfo = tempQRinfo;
        switch (i)
        {
            case 0: break; //upper left
            case 1: fullImg.RotateFlip(RotateFlipType.Rotate270FlipNone); break;
            case 2: fullImg.RotateFlip(RotateFlipType.Rotate90FlipNone); break;
            case 3: fullImg.RotateFlip(RotateFlipType.Rotate180FlipNone); break;
        }
        break;
    }
}
if(QRinfo == null)
{
    //we never found the QR!
    MessageBox.Show("Error! No QR found!");
}
else
{
    //here is the QR we found!
    MessageBox.Show(QRinfo);
}


所以这一切所做的就是把你的图像角落放在一个数组中。假定保存QR信息的字符串被设置为null。数组被循环,每个角都被传递给Process方法,以查看该角是否是带有QR码的角。在Process方法中,QR读取器尝试读取图像。如果抛出错误,则捕获该错误,并且该方法返回null。如果没有错误,则该方法返回正确的字符串。一旦Process方法完成,我们将检查返回的字符串以确保它不是null。如果不是null,则找到了QR,我们将QR字符串提供给QRinfo,并根据哪个角落有QR图像来旋转fullImg。如果字符串是null,则它将继续在图像中循环,直到找到一个具有QR的图像,或者没有剩余的图像。

循环完成后,我们检查QRinfo。如果它仍然是null,那么在任何角落都从未发现QR。如果不是null,则找到一个QR,并显示QR字符串。

所以,这就是你想要的。它会吞下错误,并不断寻找二维码,这样你就可以旋转和显示二维码。

注意:我更改了

string QRinfo = "";


string QRinfo = null;