检查位图以进行图像验证c#

时间:2013-03-08 09:09:13

标签: c# winforms bitmapimage

我正在检查文档的四个角以检查是否有图像。我怎么能用以下代码做到这一点。我得到一个文档的所有四个角但我想得到哪个角有一个图像(qr代码),然后将该角传递给该方法。

代码:

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

                    Bitmap[] corners = new Bitmap[] { bandImg1, bandImg2, bandImg3, bandImg4 };
                    string QRinfo = "";
                    for (int i = 0; i < corners.Length; ++i)
                    {
                        string tempQRinfo = Process(corners[i]);
                        if (tempQRinfo == null)
                        {
                            QRinfo = tempQRinfo;
                            switch (i)
                            {
                                case 0: break; //upper left
                                case 1: fullImg.RotateFlip(RotateFlipType.Rotate270FlipNone); break;
                                case 2: fullImg.RotateFlip(RotateFlipType.Rotate90FlipNone); break;
                                case 3: fullImg.RotateFlip(RotateFlipType.Rotate180FlipNone); break;
                            }
                            break;
                        }
                    }

流程方法

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

0 个答案:

没有答案