Search result thumbnail for PowerLauncher (#2124)

* Replace WPF Imaging library with UWP

* Removed UWP and WPF namespace conflicting files from Wox

* Removed Image hashing as it wasn't used anywhere

* Updated formatting
This commit is contained in:
Divyansh Srivastava
2020-04-16 11:01:38 -07:00
committed by GitHub
parent 37a551ecd3
commit 367cb41121
16 changed files with 43 additions and 421 deletions

View File

@@ -1,8 +1,7 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Media;
using Windows.UI.Xaml.Media;
namespace Wox.Infrastructure.Image
{

View File

@@ -1,49 +0,0 @@
using System;
using System.IO;
using System.Security.Cryptography;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace Wox.Infrastructure.Image
{
public interface IImageHashGenerator
{
string GetHashFromImage(ImageSource image);
}
public class ImageHashGenerator : IImageHashGenerator
{
public string GetHashFromImage(ImageSource imageSource)
{
if (!(imageSource is BitmapSource image))
{
return null;
}
try
{
using (var outStream = new MemoryStream())
{
// PngBitmapEncoder enc2 = new PngBitmapEncoder();
// enc2.Frames.Add(BitmapFrame.Create(tt));
var enc = new JpegBitmapEncoder();
var bitmapFrame = BitmapFrame.Create(image);
bitmapFrame.Freeze();
enc.Frames.Add(bitmapFrame);
enc.Save(outStream);
var byteArray = outStream.GetBuffer();
using (var sha1 = new SHA1CryptoServiceProvider())
{
var hash = Convert.ToBase64String(sha1.ComputeHash(byteArray));
return hash;
}
}
}
catch
{
return null;
}
}
}
}

View File

@@ -3,10 +3,10 @@ using System.Collections.Concurrent;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Wox.Infrastructure.Logger;
using Wox.Infrastructure.Storage;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Media;
namespace Wox.Infrastructure.Image
{
@@ -14,9 +14,6 @@ namespace Wox.Infrastructure.Image
{
private static readonly ImageCache ImageCache = new ImageCache();
private static BinaryStorage<ConcurrentDictionary<string, int>> _storage;
private static readonly ConcurrentDictionary<string, string> GuidToKey = new ConcurrentDictionary<string, string>();
private static IImageHashGenerator _hashGenerator;
private static readonly string[] ImageExtensions =
{
@@ -33,13 +30,11 @@ namespace Wox.Infrastructure.Image
public static void Initialize()
{
_storage = new BinaryStorage<ConcurrentDictionary<string, int>>("Image");
_hashGenerator = new ImageHashGenerator();
ImageCache.Usage = _storage.TryLoad(new ConcurrentDictionary<string, int>());
foreach (var icon in new[] { Constant.DefaultIcon, Constant.ErrorIcon })
{
ImageSource img = new BitmapImage(new Uri(icon));
img.Freeze();
ImageCache[icon] = img;
}
Task.Run(() =>
@@ -101,7 +96,6 @@ namespace Wox.Infrastructure.Image
if (path.StartsWith("data:", StringComparison.OrdinalIgnoreCase))
{
var imageSource = new BitmapImage(new Uri(path));
imageSource.Freeze();
return new ImageResult(imageSource, ImageType.Data);
}
@@ -156,11 +150,6 @@ namespace Wox.Infrastructure.Image
image = ImageCache[Constant.ErrorIcon];
path = Constant.ErrorIcon;
}
if (type != ImageType.Error)
{
image.Freeze();
}
}
catch (System.Exception e)
{
@@ -172,43 +161,22 @@ namespace Wox.Infrastructure.Image
return new ImageResult(image, type);
}
private static bool EnableImageHash = true;
public static ImageSource Load(string path, bool loadFullImage = false)
{
var imageResult = LoadInternal(path, loadFullImage);
var img = imageResult.ImageSource;
if (imageResult.ImageType != ImageType.Error && imageResult.ImageType != ImageType.Cache)
{ // we need to get image hash
string hash = EnableImageHash ? _hashGenerator.GetHashFromImage(img) : null;
if (hash != null)
{
if (GuidToKey.TryGetValue(hash, out string key))
{ // image already exists
img = ImageCache[key];
}
else
{ // new guid
GuidToKey[hash] = path;
}
}
{
// update cache
ImageCache[path] = img;
}
return img;
}
private static BitmapImage LoadFullImage(string path)
{
BitmapImage image = new BitmapImage();
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad;
image.UriSource = new Uri(path);
image.EndInit();
BitmapImage image = new BitmapImage(new Uri(path));
return image;
}
}

View File

@@ -4,6 +4,9 @@ using System.IO;
using System.Windows.Interop;
using System.Windows.Media.Imaging;
using System.Windows;
using Windows.Storage.Streams;
using BitmapSourceWPF = System.Windows.Media.Imaging.BitmapSource;
using BitmapImageUWP = Windows.UI.Xaml.Media.Imaging.BitmapImage;
namespace Wox.Infrastructure.Image
{
@@ -103,14 +106,36 @@ namespace Wox.Infrastructure.Image
public int Height { set { height = value; } }
};
public static BitmapImageUWP ByteToImage(byte[] imageData)
{
using (InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream())
{
using (DataWriter writer = new DataWriter(ms.GetOutputStreamAt(0)))
{
writer.WriteBytes(imageData);
writer.StoreAsync().GetResults();
}
BitmapImageUWP image = new BitmapImageUWP();
image.SetSource(ms);
return image;
}
}
public static BitmapSource GetThumbnail(string fileName, int width, int height, ThumbnailOptions options)
public static BitmapImageUWP GetThumbnail(string fileName, int width, int height, ThumbnailOptions options)
{
IntPtr hBitmap = GetHBitmap(Path.GetFullPath(fileName), width, height, options);
try
{
return Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
byte[] data;
BitmapSourceWPF bitmapSourceWPF = Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
PngBitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bitmapSourceWPF));
using (MemoryStream ms = new MemoryStream())
{
encoder.Save(ms);
data = ms.ToArray();
}
return ByteToImage(data);
}
finally
{

View File

@@ -48,6 +48,7 @@
<ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="2019.1.3" />
<PackageReference Include="Microsoft.Toolkit.Wpf.UI.XamlHost" Version="6.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="NLog.Schema" Version="4.7.0" />
<PackageReference Include="NLog.Web.AspNetCore" Version="4.9.1" />