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

本文关键字:更好 递归 代码 重构 | 更新日期: 2023-09-27 18:04:56

我正试图编写一些代码,将查看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;
    }
}

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

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

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

解释:
实际上,解决这类问题不需要递归,正如Alexei Levenkov在下面的评论中指出的那样,循环要简单明了得多:

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的行为就像循环中的计数器一样,这意味着递归的深度将为四次调用(最多),就像循环将迭代四次(最多)一样。