mirror of
https://github.com/ShareX/ShareX.git
synced 2024-09-28 23:42:13 +12:00
Added ScaleImageFast function for OCR
This commit is contained in:
parent
3bd51404e1
commit
09873d6278
3 changed files with 42 additions and 6 deletions
|
@ -34,7 +34,7 @@ You should have received a copy of the GNU General Public License
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Windows.Forms;
|
using System.Windows.Forms;
|
||||||
using Encoder = System.Drawing.Imaging.Encoder;
|
using System.Windows.Media.Imaging;
|
||||||
|
|
||||||
namespace ShareX.HelpersLib
|
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);
|
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)
|
public static Bitmap ResizeImage(Bitmap bmp, Size size, bool allowEnlarge, bool centerImage = true)
|
||||||
{
|
{
|
||||||
return ResizeImage(bmp, size.Width, size.Height, allowEnlarge, centerImage);
|
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))
|
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);
|
img.Save(stream, ImageFormat.Jpeg.GetCodecInfo(), encoderParameters);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -86,6 +86,7 @@
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Reference Include="Microsoft.CSharp" />
|
<Reference Include="Microsoft.CSharp" />
|
||||||
<Reference Include="Microsoft.VisualBasic" />
|
<Reference Include="Microsoft.VisualBasic" />
|
||||||
|
<Reference Include="PresentationCore" />
|
||||||
<Reference Include="System" />
|
<Reference Include="System" />
|
||||||
<Reference Include="System.Data" />
|
<Reference Include="System.Data" />
|
||||||
<Reference Include="System.Design" />
|
<Reference Include="System.Design" />
|
||||||
|
@ -99,6 +100,7 @@
|
||||||
<Reference Include="System.Windows.Forms" />
|
<Reference Include="System.Windows.Forms" />
|
||||||
<Reference Include="System.Xml" />
|
<Reference Include="System.Xml" />
|
||||||
<Reference Include="System.Xml.Linq" />
|
<Reference Include="System.Xml.Linq" />
|
||||||
|
<Reference Include="WindowsBase" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="..\GlobalAssemblyInfo.cs">
|
<Compile Include="..\GlobalAssemblyInfo.cs">
|
||||||
|
|
|
@ -64,8 +64,7 @@ public static void ThrowIfNotSupported()
|
||||||
{
|
{
|
||||||
if (!IsSupported)
|
if (!IsSupported)
|
||||||
{
|
{
|
||||||
throw new Exception(string.Format(Resources.OpticalCharacterRecognitionFeatureIsOnlyAvailableWithWindowsVersion0OrNewer,
|
throw new Exception(string.Format(Resources.OpticalCharacterRecognitionFeatureIsOnlyAvailableWithWindowsVersion0OrNewer, SupportedVersion));
|
||||||
SupportedVersion));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -77,8 +76,7 @@ public static async Task<string> OCR(Bitmap bmp, string languageTag = "en", floa
|
||||||
|
|
||||||
return await Task.Run(async () =>
|
return await Task.Run(async () =>
|
||||||
{
|
{
|
||||||
using (Bitmap bmpClone = (Bitmap)bmp.Clone())
|
using (Bitmap bmpScaled = ImageHelpers.ScaleImageFast(bmp, scaleFactor))
|
||||||
using (Bitmap bmpScaled = ImageHelpers.ResizeImage(bmpClone, (int)(bmpClone.Width * scaleFactor), (int)(bmpClone.Height * scaleFactor)))
|
|
||||||
{
|
{
|
||||||
return await OCRInternal(bmpScaled, languageTag, singleLine);
|
return await OCRInternal(bmpScaled, languageTag, singleLine);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue