samples/scanning: Updated the WiaTest to work with the latest version of Wia.ec ...
[sdk] / samples / scanning / wiaTest / wiaTest.ec
1 import "wia"
2
3 class ScanningTest : Window
4 {
5    text = "WIA Scanning Test";
6    background = activeBorder;
7    borderStyle = fixed;
8    hasMinimize = true;
9    hasClose = true;
10    size = { 360, 500 };
11
12    File scannedImage;
13    Bitmap scannedBitmap;
14
15    WiaItem scanner;
16    
17    ~ScanningTest()
18    {
19       delete scannedImage;
20       delete scannedBitmap;
21    }
22
23    void SetImage()
24    {
25       Bitmap bmp;
26       if(scannedImage)
27       {
28          char s[20];
29          sprintf(s, "File://%08X", scannedImage);
30          imagePreview.image = { s };
31       }
32       else
33          imagePreview.image = null;
34
35       bmp = imagePreview.image.bitmap;
36       if(bmp)
37       {
38          float ratio = (float)bmp.height / bmp.width;
39          if(ratio > 424.0f / 320.0f)
40          {
41             int w = 424 * bmp.width / bmp.height;
42             imagePreview.size = { w, 424 };
43          }
44          else
45          {
46             int h = 320 * bmp.height / bmp.width;
47             imagePreview.size = { 320, h };
48          }
49       }
50    }
51
52    bool OnPostCreate()
53    {
54       SetImage();
55       scanner = GetScanner(true);
56       return true;
57    }
58
59    void OnDestroy()
60    {
61       delete scanner;
62    }
63    Picture imagePreview { this, text = "Image Preview", borderStyle = deep, size = { 320, 424 }, anchor = { right = 16, top = 16 } };
64    Button scan
65    {
66       this, text = "Scan", size = { 60, 21 }, anchor = { bottom = 8 };
67
68       bool NotifyClicked(Button button, int x, int y, Modifiers mods)
69       {
70          List<Bitmap> scanned;
71          if(!scanner)
72             scanner = GetScanner(true);
73          if(scanner && (scanned = scanner.GetBitmaps()))
74          {
75             delete scannedBitmap;
76             delete scannedImage; // Delete the previous image file if any
77
78             for(i : scanned)
79             {
80                // Here 'i' is a Bitmap that we could just display ourselves, but we use a pseudo file so we can use the
81                // Picture control which works with a BitmapResource (which needs a file name)
82                char s[20];
83                scannedImage = TempFile { };
84                scannedBitmap = i;
85
86                sprintf(s, "File://%08X", scannedImage);  // 'File://' lets you use a File pointer as a file name
87                i.Save(s, "bmp", null);
88
89                // Take it out of the list because we free the other images (if any), but keep this one in 'scannedBitmap'
90                scanned.TakeOut(scannedBitmap);
91                // Just use 1 image
92                break;
93             }
94             scanned.Free();
95             delete scanned;
96
97             SetImage();
98          }
99          return true;
100       }
101    };
102 }
103
104 ScanningTest mainForm {};