Added ScaleImageFast function for OCR

This commit is contained in:
Jaex 2023-09-29 06:11:37 +03:00
parent 3bd51404e1
commit 09873d6278
3 changed files with 42 additions and 6 deletions

View file

@ -34,7 +34,7 @@ You should have received a copy of the GNU General Public License
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using Encoder = System.Drawing.Imaging.Encoder;
using System.Windows.Media.Imaging;
namespace ShareX.HelpersLib
{
@ -76,6 +76,42 @@ public static Bitmap ResizeImage(Bitmap bmp, Size size, InterpolationMode interp
return ResizeImage(bmp, size.Width, size.Height, interpolationMode);
}
public static Bitmap ScaleImageFast(Bitmap bmp, double scale)
{
return ScaleImageFast(bmp, scale, scale);
}
public static Bitmap ScaleImageFast(Bitmap bmp, double scaleX, double scaleY)
{
using (MemoryStream memoryStream = new MemoryStream())
{
bmp.Save(memoryStream, ImageFormat.Bmp);
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.StreamSource = memoryStream;
bitmapImage.EndInit();
TransformedBitmap transformedBitmap = new TransformedBitmap();
transformedBitmap.BeginInit();
transformedBitmap.Source = bitmapImage;
transformedBitmap.Transform = new System.Windows.Media.ScaleTransform(scaleX, scaleY);
transformedBitmap.EndInit();
return GetBitmap(transformedBitmap, bmp.PixelFormat);
}
}
private static Bitmap GetBitmap(BitmapSource bitmapSource, PixelFormat pixelFormat = PixelFormat.Format32bppArgb)
{
Bitmap bmp = new Bitmap(bitmapSource.PixelWidth, bitmapSource.PixelHeight, pixelFormat);
BitmapData data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.WriteOnly, pixelFormat);
bitmapSource.CopyPixels(System.Windows.Int32Rect.Empty, data.Scan0, data.Height * data.Stride, data.Stride);
bmp.UnlockBits(data);
return bmp;
}
public static Bitmap ResizeImage(Bitmap bmp, Size size, bool allowEnlarge, bool centerImage = true)
{
return ResizeImage(bmp, size.Width, size.Height, allowEnlarge, centerImage);
@ -2917,7 +2953,7 @@ public static void SaveJPEG(Image img, Stream stream, int quality)
using (EncoderParameters encoderParameters = new EncoderParameters(1))
{
encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, quality);
encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
img.Save(stream, ImageFormat.Jpeg.GetCodecInfo(), encoderParameters);
}
}

View file

@ -86,6 +86,7 @@
<ItemGroup>
<Reference Include="Microsoft.CSharp" />
<Reference Include="Microsoft.VisualBasic" />
<Reference Include="PresentationCore" />
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Design" />
@ -99,6 +100,7 @@
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
<Reference Include="System.Xml.Linq" />
<Reference Include="WindowsBase" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\GlobalAssemblyInfo.cs">

View file

@ -64,8 +64,7 @@ public static void ThrowIfNotSupported()
{
if (!IsSupported)
{
throw new Exception(string.Format(Resources.OpticalCharacterRecognitionFeatureIsOnlyAvailableWithWindowsVersion0OrNewer,
SupportedVersion));
throw new Exception(string.Format(Resources.OpticalCharacterRecognitionFeatureIsOnlyAvailableWithWindowsVersion0OrNewer, SupportedVersion));
}
}
@ -77,8 +76,7 @@ public static async Task<string> OCR(Bitmap bmp, string languageTag = "en", floa
return await Task.Run(async () =>
{
using (Bitmap bmpClone = (Bitmap)bmp.Clone())
using (Bitmap bmpScaled = ImageHelpers.ResizeImage(bmpClone, (int)(bmpClone.Width * scaleFactor), (int)(bmpClone.Height * scaleFactor)))
using (Bitmap bmpScaled = ImageHelpers.ScaleImageFast(bmp, scaleFactor))
{
return await OCRInternal(bmpScaled, languageTag, singleLine);
}