0

我正在尝试编写一些代码来查看 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;
    }
}
4

1 回答 1

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

解释:
实际上不需要递归来解决这类问题,事实上,正如 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 的行为类似于循环中的计数器,这意味着递归将(最多)有四次调用的深度,就像循环将迭代四次(最多)一样。

于 2013-03-19T18:56:17.607 回答