com.google.zxing.binarybitmap error with handling

2019-08-31 06:29发布

问题:

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.

Code:

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

Process method that is causing the error when not finding the image.

 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.

回答1:

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


Then in the other code:

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


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.

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.

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.

Note: I changed

string QRinfo = "";

to

string QRinfo = null;