1

我正在尝试为扫描文档的每个角落(左上和右上,左下和右下)获取图像。下面是我尝试实现的方法,但是当我查看保存的图像时,它们都是不同的部分,但仅位于左上角,而不是整个文档。关于如何改变这一点的任何建议?

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

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

----- 根据下面的答案更新了代码 ------

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

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

工艺方法:

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

1 回答 1

1

我的猜测是,在计算矩形坐标时,您需要考虑result或的宽度和高度fullImg,而不是像您这样硬编码值。像这样的东西:

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

您现在所做的可能不起作用,因为您正在对值进行硬编码。例如,bandImg1您使用(0,0,375,375)的矩形基本上是说“从左上角开始,向右走 375 像素,向下走 375 像素”。相反,您想使用(0, 0, result.Width / 2, result.Height / 2)“从左上角开始,穿过图像的一半,然后沿着图像向下走一半”,这将为您提供左上角。

于 2013-03-08T06:04:06.667 回答