2
2
using System . Diagnostics . CodeAnalysis ;
3
3
using System . Drawing ;
4
4
using System . Drawing . Imaging ;
5
- using System . Runtime . InteropServices ;
6
5
using System . Threading . Tasks ;
7
6
8
7
namespace TesserNet
@@ -28,16 +27,29 @@ public static string Read(this ITesseract tesseract, Image image)
28
27
/// <param name="image">The image.</param>
29
28
/// <param name="rectangle">The rectangle to perform OCR in.</param>
30
29
/// <returns>The found text as a UTF8 string.</returns>
30
+ [ SuppressMessage ( "Reliability" , "CA2000" , Justification = "Bitmap is disposed if new one was created." ) ]
31
31
public static string Read ( this ITesseract tesseract , Image image , Rectangle rectangle )
32
32
{
33
33
if ( tesseract is null )
34
34
{
35
35
throw new ArgumentNullException ( nameof ( tesseract ) ) ;
36
36
}
37
37
38
- byte [ ] data = BitmapToBytes ( image ) ;
39
- int bpp = Image . GetPixelFormatSize ( image . PixelFormat ) / 8 ;
40
- return tesseract . Read ( data , image . Width , image . Height , bpp , rectangle . X , rectangle . Y , rectangle . Width , rectangle . Height ) ;
38
+ if ( image is not Bitmap bmp )
39
+ {
40
+ bmp = new Bitmap ( image ) ;
41
+ }
42
+
43
+ IntPtr data = BitmapToBytes ( bmp ) ;
44
+ int bpp = Image . GetPixelFormatSize ( bmp . PixelFormat ) / 8 ;
45
+ string result = tesseract . Read ( data , image . Width , image . Height , bpp , rectangle . X , rectangle . Y , rectangle . Width , rectangle . Height ) ;
46
+
47
+ if ( bmp != image )
48
+ {
49
+ bmp . Dispose ( ) ;
50
+ }
51
+
52
+ return result ;
41
53
}
42
54
43
55
/// <summary>
@@ -56,39 +68,40 @@ public static Task<string> ReadAsync(this ITesseract tesseract, Image image)
56
68
/// <param name="image">The image.</param>
57
69
/// <param name="rectangle">The rectangle to perform OCR in.</param>
58
70
/// <returns>The found text as a UTF8 string.</returns>
71
+ [ SuppressMessage ( "Reliability" , "CA2000" , Justification = "Bitmap is disposed if new one was created." ) ]
59
72
public static Task < string > ReadAsync ( this ITesseract tesseract , Image image , Rectangle rectangle )
60
73
{
61
74
if ( tesseract is null )
62
75
{
63
76
throw new ArgumentNullException ( nameof ( tesseract ) ) ;
64
77
}
65
78
66
- byte [ ] data = BitmapToBytes ( image ) ;
67
- int bpp = Image . GetPixelFormatSize ( image . PixelFormat ) / 8 ;
68
- return tesseract . ReadAsync ( data , image . Width , image . Height , bpp , rectangle . X , rectangle . Y , rectangle . Width , rectangle . Height ) ;
69
- }
70
-
71
- [ SuppressMessage ( "Reliability" , "CA2000" , Justification = "Bitmap is disposed if new one was created." ) ]
72
- private static byte [ ] BitmapToBytes ( Image image )
73
- {
74
79
if ( image is not Bitmap bmp )
75
80
{
76
81
bmp = new Bitmap ( image ) ;
77
82
}
78
83
79
- BitmapData bmpData = bmp . LockBits ( new Rectangle ( 0 , 0 , bmp . Width , bmp . Height ) , ImageLockMode . ReadOnly , bmp . PixelFormat ) ;
80
- IntPtr ptr = bmpData . Scan0 ;
81
- int size = bmp . Width * bmp . Height * Image . GetPixelFormatSize ( bmp . PixelFormat ) / 8 ;
82
- byte [ ] bytes = new byte [ size ] ;
83
- Marshal . Copy ( ptr , bytes , 0 , size ) ;
84
- bmp . UnlockBits ( bmpData ) ;
84
+ IntPtr data = BitmapToBytes ( bmp ) ;
85
+ int bpp = Image . GetPixelFormatSize ( image . PixelFormat ) / 8 ;
86
+ Task < string > resultTask = tesseract . ReadAsync ( data , image . Width , image . Height , 4 , rectangle . X , rectangle . Y , rectangle . Width , rectangle . Height ) ;
85
87
86
- if ( bmp != image )
88
+ return resultTask . ContinueWith ( r =>
87
89
{
88
- bmp . Dispose ( ) ;
89
- }
90
+ if ( bmp != image )
91
+ {
92
+ bmp . Dispose ( ) ;
93
+ }
90
94
91
- return bytes ;
95
+ return r . Result ;
96
+ } ) ;
97
+ }
98
+
99
+ private static IntPtr BitmapToBytes ( Bitmap image )
100
+ {
101
+ BitmapData bmpData = image . LockBits ( new Rectangle ( 0 , 0 , image . Width , image . Height ) , ImageLockMode . ReadOnly , image . PixelFormat ) ;
102
+ IntPtr ptr = bmpData . Scan0 ;
103
+ image . UnlockBits ( bmpData ) ;
104
+ return ptr ;
92
105
}
93
106
}
94
107
}
0 commit comments