添加C#实现
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.Drawing.Drawing2D;
|
||||
using System.Drawing.Text;
|
||||
using OpenCvSharp;
|
||||
using OpenCvSharp.Extensions;
|
||||
using Size = OpenCvSharp.Size;
|
||||
using Point = OpenCvSharp.Point;
|
||||
|
||||
namespace Com.Lmc.ShuiYin.One.Util
|
||||
{
|
||||
public class Utils
|
||||
{
|
||||
public static Mat Read(string image, int type)
|
||||
{
|
||||
// Map Java CV_8S (1) -> Color, CV_8U (0) -> Grayscale
|
||||
ImreadModes mode = ImreadModes.Unchanged;
|
||||
if (type == 1) mode = ImreadModes.Color;
|
||||
else if (type == 0) mode = ImreadModes.Grayscale;
|
||||
else mode = (ImreadModes)type;
|
||||
|
||||
Mat src = Cv2.ImRead(image, mode);
|
||||
if (src.Empty())
|
||||
{
|
||||
Console.WriteLine("File not found!");
|
||||
Environment.Exit(-1);
|
||||
}
|
||||
return src;
|
||||
}
|
||||
|
||||
public static void Show(Mat mat)
|
||||
{
|
||||
Cv2.ImShow("Utils", mat);
|
||||
Cv2.WaitKey(0);
|
||||
}
|
||||
|
||||
public static Mat OptimalDft(Mat srcImg)
|
||||
{
|
||||
Mat padded = new Mat();
|
||||
int opRows = Cv2.GetOptimalDFTSize(srcImg.Rows);
|
||||
int opCols = Cv2.GetOptimalDFTSize(srcImg.Cols);
|
||||
Cv2.CopyMakeBorder(srcImg, padded, 0, opRows - srcImg.Rows,
|
||||
0, opCols - srcImg.Cols, BorderTypes.Constant, Scalar.All(0));
|
||||
return padded;
|
||||
}
|
||||
|
||||
public static bool IsAscii(string str)
|
||||
{
|
||||
foreach (char c in str)
|
||||
{
|
||||
if (c > 127) // Simple ASCII check
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static Mat DrawNonAscii(string watermark)
|
||||
{
|
||||
// Create a dummy bitmap to measure string
|
||||
using (Bitmap dummy = new Bitmap(1, 1))
|
||||
using (Graphics gDummy = Graphics.FromImage(dummy))
|
||||
{
|
||||
Font font = new Font("Arial", 64, FontStyle.Regular, GraphicsUnit.Pixel);
|
||||
// In Java code: Font font = new Font("Default", Font.PLAIN, 64);
|
||||
// "Default" might not exist in Windows fonts, usually maps to Dialog or SansSerif.
|
||||
// I'll use "Arial" or generic SansSerif.
|
||||
|
||||
SizeF size = gDummy.MeasureString(watermark, font);
|
||||
int width = (int)Math.Ceiling(size.Width);
|
||||
int height = (int)Math.Ceiling(size.Height);
|
||||
|
||||
// Create actual bitmap
|
||||
Bitmap bufferedImage = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
|
||||
// Java uses TYPE_BYTE_GRAY, but System.Drawing doesn't support drawing to 8bpp easily.
|
||||
// We will draw to RGB and convert to Gray later or let OpenCV handle it.
|
||||
|
||||
using (Graphics graphics = Graphics.FromImage(bufferedImage))
|
||||
{
|
||||
graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
|
||||
graphics.CompositingQuality = CompositingQuality.HighQuality;
|
||||
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
|
||||
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
|
||||
|
||||
graphics.Clear(Color.Black); // Assuming background is black? Java code says `new BufferedImage`. Default is 0 (black).
|
||||
// Java code: graphics.setColor(Color.WHITE); graphics.drawString(watermark, 0, metrics.getAscent());
|
||||
|
||||
graphics.DrawString(watermark, font, Brushes.White, 0, 0);
|
||||
}
|
||||
|
||||
// Convert to Mat
|
||||
Mat res = BitmapConverter.ToMat(bufferedImage);
|
||||
// Convert to grayscale CV_8U
|
||||
Cv2.CvtColor(res, res, ColorConversionCodes.BGR2GRAY);
|
||||
|
||||
Cv2.ImWrite("C:\\tools\\shuiyin.png", res); // Keep path as requested
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
public static void FixSize(Mat src, Mat mirror)
|
||||
{
|
||||
if (src.Rows != mirror.Rows)
|
||||
{
|
||||
Cv2.CopyMakeBorder(src, src, 0, mirror.Rows - src.Rows,
|
||||
0, 0, BorderTypes.Constant, Scalar.All(0));
|
||||
}
|
||||
if (src.Cols != mirror.Cols)
|
||||
{
|
||||
Cv2.CopyMakeBorder(src, src, 0, 0,
|
||||
0, mirror.Cols - src.Cols, BorderTypes.Constant, Scalar.All(0));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user