Quantcast
Channel: Saraff.Twain.NET
Viewing all articles
Browse latest Browse all 419

Updated Wiki: Options for Transferring Data

$
0
0

Options for Transferring Data

There are three modes defined by TWAIN for transferring data:
  • Native
  • Disk File
  • Buffered Memory
A DS is required to support Native and Buffered Memory transfers.
Get a list of supported transfer modes as follows:
Twain32.Enumeration _xferMech=this._twain32.Capabilities.XferMech.Get();

Native Mode Transfer

Native mode is the default mode. There is one potential limitation that can occur in a Native mode
transfer. That is, there may not be an adequately large block of RAM available to hold the image.
Enable native mode transfer as follows:
this._twain32.Capabilities.XferMech.Set(TwSX.Native);

Image transmission is implemented as follows: for each of the received image is raised EndXfer event, and after completion of scanning is raised AcquireCompleted event. In the AcquireCompleted event handler can be used the GetImage method and the ImageCount property for retrieving all scanned images.
privatevoid _twain32_EndXfer(object sender,Twain32.EndXferEventArgs e) {
    try {
        using(var _image = e.Image) {
            _image.Save("filename.tif",ImageFormat.Tiff);
        }
   } catch(Exception ex) {
        MessageBox.Show(ex.Message,ex.GetType().Name,MessageBoxButtons.OK,MessageBoxIcon.Error);
    }
}

privatevoid _twain32_AcquireCompleted(object sender,EventArgs e) {
    try {
        for(int i=0; i<this._twain32.ImageCount; i++) {
            using(var _image=this._twain32.GetImage(i)){
                _image.Save(Path.GetTempFileName());
            }
        }
    } catch(Exception ex) {
        MessageBox.Show(ex.Message,ex.GetType().Name,MessageBoxButtons.OK,MessageBoxIcon.Error);
    }
}

Disk File Mode Transfer

Enable disk file mode transfer as follows:
this._twain32.Capabilities.XferMech.Set(TwSX.File);

Image transmission is implemented as follows: for each image is raised SetupFileXferEvent event before obtaining, and is raised FileXferEvent event after receiving. After completion of scanning is raised AcquireCompleted event. But the ImageCount property value will be zero.
privatevoid _twain32_SetupFileXferEvent(object sender,Twain32.SetupFileXferEventArgs e) {
    try {
        e.FileName=string.Format(@"FileXferTransfer_{0}.{1}",DateTime.Now.ToString("yyyyMMddHHmmss"),this._twain32.Capabilities.ImageFileFormat.GetCurrent().ToString().ToLower());
    } catch(Exception ex) {
        MessageBox.Show(ex.Message,ex.GetType().Name,MessageBoxButtons.OK,MessageBoxIcon.Error);
    }
}

privatevoid _twain32_FileXferEvent(object sender,Twain32.FileXferEventArgs e) {
    try {
        this.CurrentBitmap=Image.FromFile(e.ImageFileXfer.FileName) as Bitmap;
    } catch(Exception ex) {
        MessageBox.Show(ex.Message,ex.GetType().Name,MessageBoxButtons.OK,MessageBoxIcon.Error);
    }
}

To select the required image file format used ImageFileFormat capability.
this._twain32.Capabilities.ImageFileFormat.Set(TwFF.Tiff);

Specified value should be supported by the DS.

Buffered Memory Mode Transfer

Enable buffered memory mode transfer as follows:
this._twain32.Capabilities.XferMech.Set(TwSX.Memory);

Image transmission is implemented as follows: for each image is raised SetupMemXferEvent event before obtaining, and is raised MemXferEvent event during receiving. After completion of scanning is raised AcquireCompleted event. But the ImageCount property value will be zero.
privatevoid _twain32_SetupMemXferEvent(object sender,Twain32.SetupMemXferEventArgs e) {
    try {
        this._stream=File.Create(string.Format("MemXferTransfer_{0}.ppm",DateTime.Now.ToString("yyyyMMddHHmmss")),(int)e.BufferSize);
        var _writer=new BinaryWriter(this._stream);
        switch(e.ImageInfo.SamplesPerPixel) {
            case 1:
                if(e.ImageInfo.PixelType==TwPixelType.Palette) {
                    _writer.Write(Encoding.ASCII.GetBytes("P6\n"));
                    this._palette=this._twain32.Palette.Get();
                } else {
                    _writer.Write(Encoding.ASCII.GetBytes("P5\n"));
                }
                break;
            case 3:
                _writer.Write(Encoding.ASCII.GetBytes("P6\n"));
                break;
            default:
                _writer.Write(Encoding.ASCII.GetBytes("PX\n"));
                break;
        }
        _writer.Write(Encoding.ASCII.GetBytes(string.Format("# (C) SARAFF SOFTWARE 2013.\n{0} {1}\n{2}\n",e.ImageInfo.ImageWidth,e.ImageInfo.ImageLength,byte.MaxValue)));
    } catch(Exception ex) {
        MessageBox.Show(ex.Message,ex.GetType().Name,MessageBoxButtons.OK,MessageBoxIcon.Error);
    }
}

privatevoid _twain32_MemXferEvent(object sender,Twain32.MemXferEventArgs e) {
    try {
        int _bytesPerPixel=e.ImageInfo.BitsPerPixel>>3;
        for(int i=0,_rowOffset=0; i<e.ImageMemXfer.Rows; i++,_rowOffset+=(int)e.ImageMemXfer.BytesPerRow) {
            for(int ii=0,_colOffset=0; ii<e.ImageMemXfer.Columns; ii++,_colOffset+=_bytesPerPixel) {
                switch(e.ImageInfo.BitsPerPixel) {
                    case 1:
                        for(int _mask=1; (_mask&0xff)!=0&&ii<e.ImageMemXfer.Columns; _mask<<=1,ii++) {
                            this._stream.WriteByte((e.ImageMemXfer.ImageData[_rowOffset+_colOffset]&_mask)!=0?byte.MaxValue:byte.MinValue);
                        }
                        _colOffset++;
                        ii--;
                        break;
                    case 8:
                    case 24:
                        if(e.ImageInfo.PixelType==TwPixelType.Palette) {
                            Color _color=this._palette.Colors[e.ImageMemXfer.ImageData[_rowOffset+_colOffset]];
                            this._stream.Write(newbyte[] { _color.R,_color.G,_color.B },0,3);
                        } else {
                            this._stream.Write(e.ImageMemXfer.ImageData,_rowOffset+_colOffset,_bytesPerPixel);
                        }
                        break;
                }
            }
        }
    } catch(Exception ex) {
        MessageBox.Show(ex.Message,ex.GetType().Name,MessageBoxButtons.OK,MessageBoxIcon.Error);
    }
}

Buffered Memory Mode Transfer With File Format

Enable buffered memory mode transfer with file format as follows:
this._twain32.Capabilities.XferMech.Set(TwSX.MemFile);

Image transmission is implemented as follows: for each image is raised SetupMemXferEvent event before obtaining, and is raised MemXferEvent event during receiving. After completion of scanning is raised AcquireCompleted event. But the ImageCount property value will be zero.
To select the required image file format used ImageFileFormat capability.
this._twain32.Capabilities.ImageFileFormat.Set(TwFF.Tiff);

Specified value should be supported by the DS.

Viewing all articles
Browse latest Browse all 419

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>