using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections; using System.IO; using System.Runtime.Serialization.Formatters.Binary; using System.Data; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Microsoft.AspNetCore.Http; using FMSAdmin.Models; using FMSAdmin.Entities; using System.Drawing; using System.Drawing.Imaging; using QRCoder; namespace FMSAdmin.Helpers { public class QrCodeHelper { private readonly AppSettings _appSettings; private readonly StorageHelper _storageHelper; private readonly ILogger _logger; public QrCodeHelper( IOptions appSettings, ILogger logger, StorageHelper storageHelper ) { _appSettings = appSettings.Value; _logger = logger; _storageHelper = storageHelper; var currentDir = _appSettings.StoragePath; Directory.CreateDirectory(currentDir + "/wwwroot/files/temp/qr"); Directory.CreateDirectory(currentDir + "/wwwroot/files/qr"); } //QR 생성전 임시 폴더 생성 public string CreateBaseDir(string folder) { string folderPath = _appSettings.StoragePath + "/wwwroot/files/temp/qr/" + folder; if (!Directory.Exists(folderPath)) { Directory.CreateDirectory(folderPath); } return _appSettings.StorageUrl; } //QR 이미지 생성 public void Save(string code, string id, string group = "") { if (string.IsNullOrEmpty(group)) { group = "default"; } if (!_storageHelper.DirectoryExists($"/qr/{group}")) { _storageHelper.CreateDirectory($"/qr/{group}"); } var path = $"/qr/{group}/{id}.jpg"; var dispCdoe = code.Length > 11 ? code.Substring(0, 11) : code; QRCodeGenerator qrGenerator = new QRCodeGenerator(); QRCodeData qrCodeData = qrGenerator.CreateQrCode(code, QRCodeGenerator.ECCLevel.Q); QRCode qrCode = new QRCode(qrCodeData); Bitmap qrCodeImage = qrCode.GetGraphic(12); Bitmap baseBitmap = new Bitmap(357, 357); Graphics g = Graphics.FromImage(baseBitmap); g.FillRectangle(Brushes.White, 0, 0, baseBitmap.Width, baseBitmap.Height); Brush blackBrush = new SolidBrush(Color.Black); // FontColor FontFamily familyName = new FontFamily("굴림"); // FontFamily System.Drawing.Font myFont = new System.Drawing.Font(familyName, 30, FontStyle.Bold, GraphicsUnit.Pixel);// 폰트생성 PointF startPoint = new PointF(70, 310); // 글자 시작위치 g.DrawImage(qrCodeImage, new Point(5, 0)); g.DrawString(dispCdoe, myFont, blackBrush, startPoint); _logger.LogInformation("저장:" + _storageHelper.GetLocalPath(path)); baseBitmap.Save(_storageHelper.GetLocalPath(path), System.Drawing.Imaging.ImageFormat.Jpeg); } //QR 이미지 생성 /* public void QrGenerate(string code, string folder) { var folderPath = _appSettings.StoragePath + "/wwwroot/files/temp/qr/" + folder; var dispCdoe = code.Length > 11 ? code.Substring(0, 11) : code; QRCodeGenerator qrGenerator = new QRCodeGenerator(); QRCodeData qrCodeData = qrGenerator.CreateQrCode(code, QRCodeGenerator.ECCLevel.Q); QRCode qrCode = new QRCode(qrCodeData); Bitmap qrCodeImage = qrCode.GetGraphic(12); Bitmap baseBitmap = new Bitmap(357, 357); Graphics g = Graphics.FromImage(baseBitmap); g.FillRectangle(Brushes.White, 0, 0, baseBitmap.Width, baseBitmap.Height); Brush blackBrush = new SolidBrush(Color.Black); // FontColor FontFamily familyName = new FontFamily("굴림"); // FontFamily System.Drawing.Font myFont = new System.Drawing.Font(familyName, 30, FontStyle.Bold, GraphicsUnit.Pixel);// 폰트생성 PointF startPoint = new PointF(70, 310); // 글자 시작위치 g.DrawImage(qrCodeImage, new Point(5, 0)); g.DrawString(dispCdoe, myFont, blackBrush, startPoint); baseBitmap.Save(folderPath + "/" + code + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg); } */ //폴더 압축 public bool FileCompress(string folder, string filename) { bool bRet = false; var folderPath = _appSettings.StoragePath + "/wwwroot/files/temp/qr/"; try { System.IO.Compression.ZipFile.CreateFromDirectory(folderPath + folder, folderPath + filename + ".zip"); bRet = true; } catch (Exception ex) { _logger.LogError(ex, ""); } return bRet; } // 폴더 삭제 public void FolderDelete(string folder) { var folderPath = _appSettings.StoragePath + "/wwwroot/files/temp/qr/"; //Directory dir = new Directory(folderPath); System.IO.Directory.Delete(folderPath + folder, true); } private static byte[] BitmapToBytes(Bitmap img) { using (MemoryStream stream = new MemoryStream()) { img.Save(stream, System.Drawing.Imaging.ImageFormat.Png); return stream.ToArray(); } } } }