com.google.zxing.binarybitmap 处理位图图像时出错 [英] com.google.zxing.binarybitmap error with handling bitmap image

查看:41
本文介绍了com.google.zxing.binarybitmap 处理位图图像时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have this method that reads the bitmap image to decode a qr code in a certain area in the document ( looking into four corners for qr code) Because of how i have my code it is always hitting the error message which i know that it cannot find the bitmap but i want to take this error and translate in a way that executes my remaining code which is to rotate the document and look again for the qr bitmap image.

代码:

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

帮助:我想翻译错误消息,让文档搜索在所有四个角落都有二维码,然后如上图所示旋转.

HELP WITH: I want to translate the error message to have the document search in all four corners where it has the qr code and then rotate as shown above.

推荐答案

Process方法中:

In the Process method:

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 的图像或没有图像为止.


So what this all does is it puts your corners of the image in an array. A string that is supposed to hold the QR info is set to null. The array is looped over, and each corner is passed to the Process method, to see if that corner is the one with the QR code. In the Process method, the QR reader tries to read the image. If an error is thrown, it is caught, and the method returns null. If there is no error, then the method returns the correct string. Once the Process method is done, we check the returned string to make sure it is not null. If is NOT null, the QR was found and we give the QR string to QRinfo, and the fullImg is rotated based on what corner had the QR image. If the string IS null, then it will continue looping through the images until either one with a QR is found, or there are no images left.

循环完成后,我们检查QRinfo.如果它仍然是 null,那么在任何角落都找不到 QR.如果不是null,则找到一个二维码,我们显示二维码字符串.

Once the looping is done, we check QRinfo. If it is still null then a QR was never found in any of the corners. If is not null, a QR was found and we show the QR string.

所以,这符合您的要求.它吞下错误,并不断寻找 QR,以便您可以旋转和显示 QR.

So, this does what you are asking for. It swallows the error, and keeps looking for the QR so your rotation and displaying of the QR can happen.

注意:我改变了

string QRinfo = "";


string QRinfo = null;

这篇关于com.google.zxing.binarybitmap 处理位图图像时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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