2013-03-20 78 views
1

我遇到了麻烦,在这个问题上缠绕我的头,并希望对此进行一些输入。所以我正在阅读扫描的PDF文档,该文档中有一个QR码,它始终位于文档的左上角。创建一个循环,而不是重复if语句

由于扫描文件可能会更改文档的方向,因此我正在检查文档的左上角以查看它是否具有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; 
    } 
} 
+0

什么是结果?它从哪里来的? – cdhowie 2013-03-20 01:48:07

+0

@cdhowie错过了放入 - 结果是整个文件页面的图像。 – Masriyah 2013-03-20 01:50:38

回答

1

没有这样的东西为你工作吗?文档只有四种可能的方向,所以你最多必须循环四次。每个循环将图像旋转90度。确定QR码位于左上角后,您可以将break排除在外。然后,您可以处理QR码或做任何你想要的东西。

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

我刚刚尝试过这一点,一切都很有意义,但我意识到我的'var bandImg'只是看着特定的角落,旋转时它将旋转该角落而不是整个页面,所以我在forloop中更改为'fullImage.RotateFlip。 ..'但测试后,它只适用于当qr代码已经在适当的格式(左上角) – Masriyah 2013-03-20 02:04:20

+0

也为'if(HasProperQRCode(bandImg))'我实际上使用'过程(bandImg)'我发布了它的代码以上。如果存在,我使用它来读取qr码。 – Masriyah 2013-03-20 02:07:38

+0

@Amina我改变了代码以反映你想要的。 – Virtlink 2013-03-20 02:27:39

0

如果您在每次旋转时都进行相同的检查,则没有理由不使用循环。只要确保你记录了执行的旋转次数,否则你将陷入无限循环。

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

好吧,以便我明白 - 过程(bandImg)是用于检查并查看是否存在要读取的qr代码的方法。如果没有,那么页面应该被旋转并且新的循环页面再次在Process()中传递以进行检查。如果while(Process(bandImg)!= null),那么它打破循环,如果它为空则不旋转。它应该是'while(Process(bandImg)== null)'? – Masriyah 2013-03-20 02:12:18

+0

@Amina - 对不起,错误的代码着急,是的,它应该是'==' – 2013-03-20 02:14:35

+0

由于一些奇怪的原因,它适用于一个文件,其中qr代码已经在正确的位置(左上角),如果我传入文件qr代码在右下角,它给了我一个错误 – Masriyah 2013-03-20 03:06:54