using Cognex.VisionPro;
using OpenCvSharp;
using OpenCvSharp.Extensions;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Imaging;
namespace TeamAAS_VP.Core
{
public class ImageHelper
{
///
/// 压缩并保存图片
///
///
/// 目标图像存储路径JPEG
/// 压缩等级,0到100,0 最差质量,100 最佳
public static void CompressImage(Bitmap bitmap, string path, long quality = 20)
{
ImageCodecInfo codecInfo = GetEncoderInfo("image/jpeg");
System.Drawing.Imaging.Encoder encoder = System.Drawing.Imaging.Encoder.Quality;
EncoderParameters parameters = new EncoderParameters(1);
EncoderParameter parameter = new EncoderParameter(encoder, quality);
parameters.Param[0] = parameter;
bitmap.Save(path, codecInfo, parameters);
bitmap.Dispose();
parameter.Dispose();
parameters.Dispose();
}
private static ImageCodecInfo GetEncoderInfo(String mimeType)
{
int j;
ImageCodecInfo[] encoders;
encoders = ImageCodecInfo.GetImageEncoders();
for (j = 0; j < encoders.Length; ++j)
{
if (encoders[j].MimeType == mimeType)
return encoders[j];
}
return null;
}
public static Bitmap ICogImageConvertBitmap(ICogImage image)
{
return image.ToBitmap();
}
public static Mat ICogImageConvertMat(ICogImage image)
{
var im = image.ToBitmap();
Mat im1 = OpenCvSharp.Extensions.BitmapConverter.ToMat(im);
im.Dispose();
return im1;
}
public static Bitmap MatConvertBitmap(Mat image)
{
return OpenCvSharp.Extensions.BitmapConverter.ToBitmap(image);
}
public static ICogImage MatConvertICogImage(Mat image, bool color = false)
{
var im = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(image);
ICogImage cogImage = null;
if (color)
{
cogImage = new CogImage24PlanarColor(im);
}
else
{
cogImage = new CogImage8Grey(im);
}
im.Dispose();
return cogImage;
}
public static BitmapImage ConvertToBitmapImage(System.Drawing.Bitmap bitmap)
{
using (MemoryStream memoryStream = new MemoryStream())
{
// 将 System.Drawing.Bitmap 保存到内存流中
bitmap.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);
// 重置内存流的位置,以便从头开始读取
memoryStream.Seek(0, SeekOrigin.Begin);
// 创建 BitmapImage 对象并加载内存流中的图像数据
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = memoryStream;
bitmapImage.EndInit();
return bitmapImage;
}
}
public static BitmapImage ConvertToBitmapImage(ICogImage image)
{
var bitmap = image.ToBitmap();
using (MemoryStream memoryStream = new MemoryStream())
{
// 将 System.Drawing.Bitmap 保存到内存流中
bitmap.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);
// 重置内存流的位置,以便从头开始读取
memoryStream.Seek(0, SeekOrigin.Begin);
// 创建 BitmapImage 对象并加载内存流中的图像数据
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = memoryStream;
bitmapImage.EndInit();
bitmap.Dispose();
return bitmapImage;
}
}
public static BitmapImage BitmapToBitmapImage(System.Drawing.Bitmap bitmap)
{
BitmapImage bitmapImage = new BitmapImage();
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
bitmap.Save(ms, bitmap.RawFormat);
bitmapImage.BeginInit();
bitmapImage.StreamSource = ms;
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.EndInit();
bitmapImage.Freeze();
}
return bitmapImage;
}
}
}