来自吐温的翻转位图

时间:2014-12-18 13:14:44

标签: c# bitmap ocr twain scanning

我正在通过Twain扫描页面并使用BitmapRenderertwaindotnet project将页面转换为Bitmap,如post所述。

我的扫描仪允许我扫描正面和反面。 当我只扫描recto页面时,它就像一个魅力:生成的位图是完美的。但是当它扫描recto-verso时,位图会被翻转。有时是垂直的,有时是水平的。

我无法使用Bitmap.RotateFlip()方法,因为效果与每张图片无关,只有在正面反面的情况下才有效。

我已经尝试了Bitmap.FromHbitmap()描述的here或默认构造函数,但它会抛出与GDI +相关的错误。

我很确定问题是在BitmapRenderer类中从指针转换位图的问题。这是代码(为了清晰起见,我没有包含Dispose()方法):

public class BitmapRenderer : IDisposable
{
    private readonly IntPtr _picturePointer;
    private readonly IntPtr _bitmapPointer;
    private readonly IntPtr _pixelInfoPointer;
    private Rectangle _rectangle;
    private readonly BitmapInfoHeader _bitmapInfo;

    /// <summary>
    /// Initializes a new instance of the <see cref="BitmapRenderer"/> class.
    /// </summary>
    /// <param name="picturePointer_">The picture pointer.</param>
    public BitmapRenderer(IntPtr picturePointer_)
    {
        _picturePointer = picturePointer_;
        _bitmapPointer = Kernel32Native.GlobalLock(picturePointer_);

        _bitmapInfo = new BitmapInfoHeader();
        Marshal.PtrToStructure(_bitmapPointer, _bitmapInfo);

        _rectangle = new Rectangle();
        _rectangle.X = _rectangle.Y = 0;
        _rectangle.Width = _bitmapInfo.Width;
        _rectangle.Height = _bitmapInfo.Height;

        if (_bitmapInfo.SizeImage == 0)
        {
            _bitmapInfo.SizeImage = ((((_bitmapInfo.Width*_bitmapInfo.BitCount) + 31) & ~31) >> 3)*
                                    _bitmapInfo.Height;
        }

        // The following code only works on x86
        Debug.Assert(Marshal.SizeOf(typeof (IntPtr)) == 4);

        int pixelInfoPointer = _bitmapInfo.ClrUsed;
        if ((pixelInfoPointer == 0) && (_bitmapInfo.BitCount <= 8))
            pixelInfoPointer = 1 << _bitmapInfo.BitCount;

        pixelInfoPointer = (pixelInfoPointer*4) + _bitmapInfo.Size + _bitmapPointer.ToInt32();

        _pixelInfoPointer = new IntPtr(pixelInfoPointer);
    }

    /// <summary>
    /// Renders to bitmap.
    /// </summary>
    /// <returns></returns>
    public Bitmap RenderToBitmap()
    {
        Bitmap bitmap = new Bitmap(_rectangle.Width, _rectangle.Height);

        using (Graphics graphics = Graphics.FromImage(bitmap))
        {
            IntPtr hdc = graphics.GetHdc();
            try
            {
                Gdi32Native.SetDIBitsToDevice(hdc, 0, 0, _rectangle.Width, _rectangle.Height,
                                              0, 0, 0, _rectangle.Height, _pixelInfoPointer, _bitmapPointer, 0);
            }
            finally
            {
                graphics.ReleaseHdc(hdc);
            }
        }
        bitmap.SetResolution(PpmToDpi(_bitmapInfo.XPelsPerMeter), PpmToDpi(_bitmapInfo.YPelsPerMeter));
        return bitmap;
    }

    private static float PpmToDpi(double pixelsPerMeter_)
    {
        double pixelsPerMillimeter = pixelsPerMeter_/1000.0;
        double dotsPerInch = pixelsPerMillimeter*25.4;
        return (float) Math.Round(dotsPerInch, 2);
    }

我不明白这是从哪里来的,或者如何解决它。

修改

好吧,看来这种情况与转换为位图无关(问题根本不是来自twaindotnet project。)

只有手写页面才会出现。 这是一个自动OCR问题。 有人知道如何禁用手写文件的OCR吗?

0 个答案:

没有答案