重构代码以获得更好的递归

时间:2013-03-19 18:36:45

标签: c# bitmap

我正在尝试编写一些代码,这些代码将查看pdf文件并调用Process方法(在pdf中查找qr代码图像),如果没有找到则会旋转文件并再次运行Process方法。目前我不认为我在旋转后实际检查文件但是以原始格式检查相同的原始文件。如何将旋转的图像正确传递到Process方法中:

  using (var fullImg = new Bitmap(workGif))
                {
                    var bandImg = fullImg.Clone(new System.Drawing.Rectangle(0, 0, 375, 375), fullImg.PixelFormat);

                    Bitmap result = fullImg;
                    if (Process(bandImg) == null)
                    {
                          fullImg.RotateFlip(RotateFlipType.Rotate270FlipNone);
                          bandImg = fullImg.Clone(new System.Drawing.Rectangle(0, 0, 375, 375), fullImg.PixelFormat);
                        if (Process(bandImg) == null)
                        {
                             fullImg.RotateFlip(RotateFlipType.Rotate90FlipNone);
                             bandImg = fullImg.Clone(new System.Drawing.Rectangle(0, 0, 375, 375), fullImg.PixelFormat);
                            if (Process(bandImg) == null)
                            {
                                fullImg.RotateFlip(RotateFlipType.Rotate180FlipNone);
                                bandImg = fullImg.Clone(new System.Drawing.Rectangle(0, 0, 375, 375), fullImg.PixelFormat);
                                //Process(bandImg);
                                string QRinfo = Process(bandImg);
                                MessageBox.Show(QRinfo);
                                string[] qcode = QRinfo.Split('/');
                                string gid = qcode[qcode.Count() - 1];
                                Guid pgGuid = new Guid(gid);
                                var ar = dc.Assessments.FirstOrDefault(c => c.ID == pgGuid);
                                if (ar != null)
                                {
                                    var p = inputDocument.Pages[pg];
                                    string opdName = FILESTORELOCATION + pgGuid.ToString() + ".pdf";
                                    PdfDocument opd = new PdfDocument(opdName);
                                    opd.Pages.Add(p);
                                    opd.Close();

                                    ar.StoragePath = opdName;
                                    ar.LastUploadedDT = DateTime.UtcNow;
                                    ar.UploadedByUserID = uploadingUser;
                                    dc.SubmitChanges();
                                }
                            }
                        }
                    }    

处理方法:

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

1 个答案:

答案 0 :(得分:1)

如果你想要一个递归解决方案,请检查这个(当然未经测试的代码):

string ReadQrCode(Bitmap img, int n = 0) {
    if(n == 4) return null;

    var bandImg = img.Clone(new System.Drawing.Rectangle(0, 0, 375, 375), 
        img.PixelFormat);
    string text = Process(bandImg);

    if(text == null) {
        img.RotateFlip(RotateFlipType.Rotate90FlipNone);
        text = ReadQrCode(img, n + 1);
    }

    return Text;
}

示例:

string qrCode;
using (var fullImg = new Bitmap(workGif)) {        
    qrCode = ReadQrCode(bandImg);
}

<强>说明
实际上,不需要递归来解决这类问题,实际上,正如阿列克谢·列文科夫在下面的评论中指出的那样,循环更容易和清晰:

string text = null;
for(int i = 0; i < 4; i++) {
    var bandImg = img.Clone(new System.Drawing.Rectangle(0, 0, 375, 375), 
            img.PixelFormat);
    text = Process(bandImg)

    if(text != null) break;
    else img.RotateFlip(RotateFlipType.Rotate90FlipNone);
}

在递归解决方案中,n的行为类似于循环中的计数器,这意味着递归将具有四个调用的深度(最多),就像循环将迭代四次(最多)一样。

相关问题