From 090fc9ea0b21efd1f7b0fd46ccfe4dc90dac3246 Mon Sep 17 00:00:00 2001 From: liuwf Date: Tue, 25 Jun 2024 23:03:17 +0800 Subject: [PATCH] =?UTF-8?q?add-=E6=B7=BB=E5=8A=A0=E7=85=A7=E7=89=87?= =?UTF-8?q?=E5=8E=8B=E7=BC=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SlnMesnac.Business/LogoBusiness.cs | 4 +- SlnMesnac.Common/FileHelper.cs | 53 ++++++++++++++++++++++ SlnMesnac.Common/GunHelper.cs | 17 +++---- SlnMesnac.Common/ImageHelper.cs | 55 +++++++++++++++++++++++ SlnMesnac.WPF/ViewModel/IndexViewModel.cs | 4 +- 5 files changed, 121 insertions(+), 12 deletions(-) create mode 100644 SlnMesnac.Common/ImageHelper.cs diff --git a/SlnMesnac.Business/LogoBusiness.cs b/SlnMesnac.Business/LogoBusiness.cs index 0180775..11a4c6c 100644 --- a/SlnMesnac.Business/LogoBusiness.cs +++ b/SlnMesnac.Business/LogoBusiness.cs @@ -179,7 +179,9 @@ namespace SlnMesnac.Business ImageData = FileHelper.FindPhoto(config.CameraFilePath); if (ImageData != null) { - FileHelper.SaveImage(ImageData, materialCodeStr + ".jpg", PicturePath); + // 图片压缩 + byte[] compressedImageData = FileHelper.CompressImageData(ImageData, 20); + FileHelper.SaveImage(compressedImageData, materialCodeStr + ".jpg", PicturePath); RefreshPictureEvent?.Invoke(ImageData); } else diff --git a/SlnMesnac.Common/FileHelper.cs b/SlnMesnac.Common/FileHelper.cs index dc7af4f..893fad4 100644 --- a/SlnMesnac.Common/FileHelper.cs +++ b/SlnMesnac.Common/FileHelper.cs @@ -1,13 +1,66 @@ using System; using System.Collections.Generic; +using System.Drawing; +using System.Drawing.Imaging; using System.IO; using System.Text; +using Encoder = System.Drawing.Imaging.Encoder; namespace SlnMesnac.Common { public static class FileHelper { + #region + /// + ///quality: 0-100 ,越小压缩程度也越高,但是图片质量越低 + /// + /// + /// + /// + public static byte[] CompressImageData(byte[] imageData, long quality) + { + try + { + using (MemoryStream memoryStream = new MemoryStream(imageData)) + { + using (Image image = Image.FromStream(memoryStream)) + { + using (MemoryStream compressedStream = new MemoryStream()) + { + ImageCodecInfo jpgEncoder = GetEncoder(ImageFormat.Jpeg); + + Encoder myEncoder = Encoder.Quality; + EncoderParameters myEncoderParameters = new EncoderParameters(1); + EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, quality); + myEncoderParameters.Param[0] = myEncoderParameter; + + image.Save(compressedStream, jpgEncoder, myEncoderParameters); + return compressedStream.ToArray(); + } + } + } + } + catch (Exception ex) + { + Console.WriteLine($"压缩图片出错:{ex.Message}"); + return null; + } + } + + private static ImageCodecInfo GetEncoder(ImageFormat format) + { + ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders(); + foreach (ImageCodecInfo codec in codecs) + { + if (codec.FormatID == format.Guid) + { + return codec; + } + } + return null; + } + #endregion /// /// 查找指定目录下的文件 diff --git a/SlnMesnac.Common/GunHelper.cs b/SlnMesnac.Common/GunHelper.cs index 5dfbd5e..c0c4dc2 100644 --- a/SlnMesnac.Common/GunHelper.cs +++ b/SlnMesnac.Common/GunHelper.cs @@ -15,6 +15,14 @@ namespace SlnMesnac.Common #region 单例实现 private static readonly GunHelper lazy = new GunHelper(); + public static GunHelper Instance + { + get + { + return lazy; + } + } + #endregion #region 变量定义 private static SerialPort serialPort = new SerialPort(); @@ -29,14 +37,7 @@ namespace SlnMesnac.Common public delegate void RefreshMaterialCodeStr(string code); public static event RefreshMaterialCodeStr RefreshMaterialCodeStrEvent; - public static GunHelper Instance - { - get - { - return lazy; - } - } - #endregion + private DebugConfig config = DebugConfig.Instance; diff --git a/SlnMesnac.Common/ImageHelper.cs b/SlnMesnac.Common/ImageHelper.cs new file mode 100644 index 0000000..4ec5e91 --- /dev/null +++ b/SlnMesnac.Common/ImageHelper.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Drawing.Imaging; +using System.IO; +using System.Text; + +namespace SlnMesnac.Common +{ + public class ImageHelper + { + #region 单例实现 + private static readonly ImageHelper lazy = new ImageHelper(); + public static ImageHelper Instance + { + get + { + return lazy; + } + } + #endregion + + public void CompressImage(string sourceImagePath, string destinationPath, long quality) + { + using (Image image = Image.FromFile(sourceImagePath)) + { + ImageCodecInfo jpgEncoder = GetEncoder(ImageFormat.Jpeg); + + System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality; + EncoderParameters myEncoderParameters = new EncoderParameters(1); + EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, quality); + myEncoderParameters.Param[0] = myEncoderParameter; + + image.Save(destinationPath, jpgEncoder, myEncoderParameters); + } + } + + private ImageCodecInfo GetEncoder(ImageFormat format) + { + ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders(); + + foreach (ImageCodecInfo codec in codecs) + { + if (codec.FormatID == format.Guid) + { + return codec; + } + } + return null; + } + + + } + +} diff --git a/SlnMesnac.WPF/ViewModel/IndexViewModel.cs b/SlnMesnac.WPF/ViewModel/IndexViewModel.cs index 64e7ede..7c40ece 100644 --- a/SlnMesnac.WPF/ViewModel/IndexViewModel.cs +++ b/SlnMesnac.WPF/ViewModel/IndexViewModel.cs @@ -75,9 +75,7 @@ namespace SlnMesnac.WPF.ViewModel /// private void Reset() { - - - logoBusiness.Pass(); + logoBusiness.Pass(); } ///