0

私はビットマップ画像を読み取ってドキュメントの特定の領域の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コードがある四隅すべてでドキュメントを検索し、上記のように回転させたいと思います。

4

1 に答える 1

0

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;
于 2013-03-08T17:27:59.830 に答える