检测QR码,然后旋转文件 [英] detecting qr code then rotating document

查看:197
本文介绍了检测QR码,然后旋转文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

晚上好一切美好的帮手,

Good evening all wonderful helpers,

我试图检测到我有一个扫描的PDF QR码(使用QR打印的PDF,然后扫描)的QR码将始终位于该文件的一个角落。在这里,在我下面的代码我复制其中的QR所在的区域。

I am trying to detect a QR code that I have on a scanned PDF(printed pdf with qr then scanned) The QR code will always be located on a corner of the file. Here in my following code I clone the area of which the QR is located.

帮助的:
- 望过去文件,看看哪个角落有一个形象(QR)
- 后发现有QR码的角落 - 旋转,因此,QR码将位于左上角的文件。

Help on: - looking over the document to see which corner has an image (qr) - after finding the corner that has the qr code - rotate the file so that the qr code would be located on the top left corner.

代码:

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



任何帮助极大的赞赏。

Any help greatly appreciated.

推荐答案

我认为你需要每个 bandImg 传递到工艺方法。并根据其中一个返回一个有效的字符串,你就会知道你需要做的旋转。

I think you need to pass each bandImg into the process method. And depending on which one returns a valid string, you'll know the rotation you need to do.

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

这篇关于检测QR码,然后旋转文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆