0

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

代码:

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

0 回答 0