创建循环而不是重复if语句

时间:2013-03-20 01:45:22

标签: c# for-loop while-loop

我无法绕过这个并想要对此有所了解。所以我正在阅读扫描的PDF文档,其中包含QR码,该QR码始终位于文档的左上角。

由于扫描文件可能会改变文档的方向,我正在检查文档的左上角是否有QR码,如果没有,我将旋转文档并再次检查左角。这样做的目的是因为QR码位于左上角,因此文档的格式符合我的要求。

如何更改以下代码,以便获取QR码的文档检查 - 如果未找到则再次轮流检查整个文档并继续直到找到QR代码。我也应该在循环中旋转90而不是90 - 180 - 270。

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, result.Width, result.Height), fullImg.PixelFormat);
        if (Process(bandImg) == null)
        {
            fullImg.RotateFlip(RotateFlipType.Rotate90FlipNone);
            bandImg = fullImg.Clone(new System.Drawing.Rectangle(0, 0, result.Width, result.Height), fullImg.PixelFormat);

            if (Process(bandImg) == null)
            {
                fullImg.RotateFlip(RotateFlipType.Rotate180FlipNone);
                bandImg = fullImg.Clone(new System.Drawing.Rectangle(0, 0, result.Width, result.Height), fullImg.PixelFormat);
            }
         }
    }
    bandImg.Save(@"C:\NewImageTest.png");
    string QRinfo = Process(bandImg);
    MessageBox.Show(QRinfo);
}

流程方法 我在此方法中传递图像以检查是否有要读取的QR码。

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

3 个答案:

答案 0 :(得分:1)

这样的东西不适合你吗?文档只有四种可能的方向,因此您必须最多循环四次。每个循环将图像旋转90度。一旦确定QR码位于左上角,您就可以break退出循环。然后你可以处理二维码或用它做你想做的任何事情。

public void Do(string workGif)
{
    // ...
    string qrInfo;
    using (var fullImg = new Bitmap(workGif))
    {
        for (int i = 0; i < 4; i++)
        {
            // Does the image contain a QR code?
            qrInfo = Process(fullImg);
            if (qrInfo = null)
                // No QR code found. Rotate the image.
                fullImg.RotateFlip(RotateFlipType.Rotate90FlipNone);
            else
                // QR code found. Break out of the loop.
                break;
        }
        if (qrInfo == null)
        {
            throw new InvalidOperationException(
                "The document contains no QR code.");
        }
    }
    MessageBox.Show(qrInfo);
    // ...
}

您可以将获取源图像角点图像的代码移动到Process方法。

private Image GetCornerImage(Image sourceImage)
{
    return sourceImage.Clone(new Rectangle(0, 0, 375, 375), sourceImage.PixelFormat);
}

public string Process(Bitmap bitmap)
{
    var cornerImg = GetCornerImage(bitmap);

    var reader = new com.google.zxing.qrcode.QRCodeReader();
    LuminanceSource source = new RGBLuminanceSource(
        cornerImg, cornerImg.Width, cornerImg.Height);
    var binarizer = new HybridBinarizer(source);
    var binBitmap = new BinaryBitmap(binarizer);
    return reader.decode(binBitmap).Text;
}

答案 1 :(得分:1)

这应该可以正常工作;

using (var fullImg = new Bitmap(workGif))
{
    var bandImg = fullImg.Clone(new System.Drawing.Rectangle(0, 0, 375, 375), fullImg.PixelFormat);
    int i = 0;
    while(Process(bandImg) == null)
    {
        if (i == 1)
            fullImg.RotateFlip(RotateFlipType.Rotate270FlipNone);
        else if (i == 2)
            fullImg.RotateFlip(RotateFlipType.Rotate90FlipNone);
        else if (i== 3)
            fullImg.RotateFlip(RotateFlipType.Rotate180FlipNone);

            /*
                 Another way in which Rotation Degree can be done
                 First time it rotate by 270, then by 180 & then by 90
                 int i must be initialized with 1
                 int degree_to_rotate = 360 - ((4 - i) * 90)
            */

        bandImg = fullImg.Clone(new System.Drawing.Rectangle(0, 0, result.Width, result.Height), fullImg.PixelFormat);
        i++;
    }
    bandImg.Save(@"C:\NewImageTest.png");
    string QRinfo = Process(bandImg);
    MessageBox.Show(QRinfo);
}

答案 2 :(得分:0)

如果在每次旋转中进行相同的检查,则没有理由不使用循环。只要确保你跟踪所执行的旋转次数,否则你将陷入无限循环。