1

我很难解决这个问题,并希望对此有所了解。因此,我正在阅读一份扫描的 PDF 文档,其中包含一个二维码,该二维码始终位于文档的左上角。

由于扫描文件可能会改变文档的方向,我正在检查文档的左上角是否有二维码,如果没有,我将旋转文档并再次检查左上角。这样做的目的是因为 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);
}

处理方法 我在这个方法中通过图片来查看是否有二维码可以读取。

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

3 回答 3

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;
}
于 2013-03-20T01:49:49.567 回答
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);
}
于 2013-03-20T01:53:02.563 回答
0

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

于 2013-03-20T01:49:24.270 回答