【问题标题】:detecting qr code then rotating document检测二维码然后旋转文档
【发布时间】:2013-02-19 08:07:19
【问题描述】:

各位好帮手,晚上好,

我正在尝试检测我在扫描的 PDF 上的二维码(打印的 pdf,然后扫描二维码)二维码将始终位于文件的一角。在下面的代码中,我克隆了 QR 所在的区域。

帮助: - 查看文档以查看哪个角落有图像(qr) - 找到有二维码的角落后 - 旋转文件,使二维码位于左上角。

代码:

for (int pg = 0; pg < inputDocument.PageCount; pg++)
            {
                QRCodeDecoder decoder = new QRCodeDecoder();
                string workGif = workingFilename.Replace(".pdf", string.Format(".{0}.gif", pg + 1));
                GhostscriptWrapper.GeneratePageThumb(workingFilename, workGif, pg + 1, 300, 300); // size (last two params) does not seem to have any effect


                using (var fullImg = new Bitmap(workGif))
                {

                    Bitmap result = fullImg;
                    //top-left
                    var bandImg1 = result.Clone(new System.Drawing.Rectangle(0, 0, result.Width/2, result.Height/2), fullImg.PixelFormat);
                    //top-right
                    var bandImg2 = result.Clone(new System.Drawing.Rectangle(result.Width / 2, 0, result.Width / 2, result.Height / 2), fullImg.PixelFormat);
                    //bottom-left
                    var bandImg3 = result.Clone(new System.Drawing.Rectangle(0, result.Height / 2, result.Width / 2, result.Height / 2), fullImg.PixelFormat);
                    //bottom-right
                    var bandImg4 = result.Clone(new System.Drawing.Rectangle(result.Width / 2, result.Height / 2, result.Width / 2, result.Height / 2), fullImg.PixelFormat);

//saving images for testing purpose just to see what was saved for each corner. 

                        bandImg1.Save("c:\\bandImg1.gif", System.Drawing.Imaging.ImageFormat.Gif);
                        bandImg2.Save("c:\\bandImg2.gif", System.Drawing.Imaging.ImageFormat.Gif);
                        bandImg3.Save("c:\\bandImg3.gif", System.Drawing.Imaging.ImageFormat.Gif);
                        bandImg4.Save("c:\\bandImg4.gif", System.Drawing.Imaging.ImageFormat.Gif);


                    string QRinfo = Process(bandImg1);//this  should pass in the bandImg depending on the above search finding which corner has a qr image
                    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();
                    }
                }

                //this.Refresh();
                File.Delete(workGif);
            }

处理方法:

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 e.Message;
        }
    }

非常感谢任何帮助。

【问题讨论】:

  • 我认为二维码的重点是方向不重要,算法可以自动检测二维码的存在。雪莉,你应该在整个 pdf 上运行检测。
  • 但是,不需要旋转。您应该只剪掉每个角并在每个角上运行 QR 码。
  • @Aron - 目前我能够检测到二维码 - 目的是这是一个应该以正确格式(直立阅读)呈现的文档,因此如果用户在文档中倒置扫描我可以修复它
  • 这是否意味着您要旋转 PDF 作为输出?
  • @Aron 是的 - 确切地说:D 我一直在尝试寻找可能的方法甚至想法,但不确定从哪里或如何开始,找不到很多关于此的示例。

标签: c# winforms pdf qr-code bitmapimage


【解决方案1】:

我认为您需要将每个 bandImg 传递给 process 方法。并且根据哪个返回一个有效的字符串,你会知道你需要做的旋转。

//top left
var bandImg1 = result.Clone(new Rectangle(0, 0, result.Width / 2, result.Height / 2), result.PixelFormat);
//top right
var bandImg2 = result.Clone(new Rectangle(result.Width / 2, 0, result.Width / 2, result.Height / 2), result.PixelFormat);
//bottom left
var bandImg3 = result.Clone(new Rectangle(0, result.Height / 2, result.Width / 2, result.Height / 2), result.PixelFormat);
//bottom right
var bandImg4 = result.Clone(new Rectangle(result.Width / 2, result.Height / 2, result.Width / 2, result.Height / 2), result.PixelFormat);

Bitmap[] corners = new Bitmap[] { bandImg1, bandImg2, bandImg3, bandImg4 };
string QRinfo = "";

for (int i = 0; i < corners.Length; ++i)
{
    string tmpQRinfo = Process(corners[i]);//this  should pass in the bandImg depending on the above search finding which corner has a qr image

    //check if string is valid, you'll need to figure out how to do this
    if (valid)
    {
        QRinfo = tmpQRinfo;
        switch(i)
        {
            case 0: //already in upper left, do nothing
                break;
            case 1: //upper right corner, so rotate the document -90 which is the same as 270
                fullImg.RotateFlip(RotateFlipType.Rotate270FlipNone);
                break;
            case 2: //lower left corner, so rotate 90
                fullImg.RotateFlip(RotateFlipType.Rotate90FlipNone);
                break;
            case 3: //lower right corner, so rotate 180
                fullImg.RotateFlip(RotateFlipType.Rotate180FlipNone);
                break;
        }
        break; //the QR was found, no need to continue searching
    }
}
MessageBox.Show(QRinfo);

【讨论】:

  • 再次感谢您的帮助!我将继续并开始应用它,希望它会很好。
  • @Amina 我添加了break; //the QR was found, no need to continue searching 行,以便在找到 QR 时停止搜索。找到后无需做额外的工作。
  • 当我这样做时,我在构建时遇到错误:forloop for(int i = 0; i&lt;corners.length; ++i) 错误消息是:cannot convert from 'int' to 'System.Drawing.Bitmap'
  • @Amina 我没有收到此错误。错误在哪里下划线? corners 应该是 Bitmap[] 类型。此外,length 应该是 Length。从我的答案中复制/粘贴可能更容易,而不是把它打出来,只是为了确保它是正确的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多