添加C#实现

This commit is contained in:
ShaoHua
2025-12-27 20:36:56 +08:00
parent a5303d6f3e
commit ce394c0646
21 changed files with 1312 additions and 0 deletions
@@ -0,0 +1,32 @@
using Com.Lmc.ShuiYin.One.Converter;
using Com.Lmc.ShuiYin.One.Util;
using OpenCvSharp;
namespace Com.Lmc.ShuiYin.One.Dencoder
{
public class Decoder
{
private Converter.Converter converter;
public Decoder(Converter.Converter converter)
{
this.converter = converter;
}
public Converter.Converter GetConverter()
{
return converter;
}
public void SetConverter(Converter.Converter converter)
{
this.converter = converter;
}
public void Decode(string image, string output)
{
// CV_8U is 0
Cv2.ImWrite(output, this.converter.ShowWatermark(this.converter.Start(Utils.Read(image, 0))));
}
}
}
@@ -0,0 +1,72 @@
using System.Collections.Generic;
using OpenCvSharp;
using Com.Lmc.ShuiYin.One.Converter;
using Com.Lmc.ShuiYin.One.Util;
namespace Com.Lmc.ShuiYin.One.Dencoder
{
public abstract class Encoder
{
protected Converter.Converter converter; // protected for subclasses
public Encoder(Converter.Converter converter)
{
this.converter = converter;
}
public Converter.Converter GetConverter()
{
return converter;
}
public void SetConverter(Converter.Converter converter)
{
this.converter = converter;
}
public void Encode(string image, string watermark, string output)
{
Mat src = Utils.Read(image, 1); // CV_8S -> Color (1)
List<Mat> channel = new List<Mat>();
List<Mat> newChannel = new List<Mat>();
Cv2.Split(src, out Mat[] splitChannel);
channel.AddRange(splitChannel);
// Java: for (int i = 0; i < 3; i++)
// OpenCvSharp Split returns array of Mats.
// Ensure we have 3 channels? The loop is hardcoded to 3.
for (int i = 0; i < 3 && i < channel.Count; i++)
{
Mat com = this.converter.Start(channel[i]).Clone();
this.AddWatermark(com, watermark);
this.converter.Inverse(com);
// In Java: newChannel.add(i, com);
newChannel.Add(com);
}
// If src has more than 3 channels (e.g. alpha), we should probably keep them?
// The java code only loops 3 times.
if (channel.Count > 3)
{
for (int i = 3; i < channel.Count; i++)
{
newChannel.Add(channel[i]);
}
}
Mat res = new Mat();
Cv2.Merge(newChannel.ToArray(), res);
if (res.Rows != src.Rows || res.Cols != src.Cols)
{
res = new Mat(res, new Rect(0, 0, src.Width, src.Height));
}
Cv2.ImWrite(output, res);
}
public abstract void AddWatermark(Mat com, string watermark);
}
}
@@ -0,0 +1,19 @@
using Com.Lmc.ShuiYin.One.Converter;
using Com.Lmc.ShuiYin.One.Util;
using OpenCvSharp;
namespace Com.Lmc.ShuiYin.One.Dencoder
{
public class ImageEncoder : Encoder
{
public ImageEncoder(Converter.Converter converter) : base(converter)
{
}
public override void AddWatermark(Mat com, string watermark)
{
// CV_8U is 0
this.converter.AddImageWatermark(com, Utils.Read(watermark, 0));
}
}
}
@@ -0,0 +1,25 @@
using Com.Lmc.ShuiYin.One.Converter;
using Com.Lmc.ShuiYin.One.Util;
using OpenCvSharp;
namespace Com.Lmc.ShuiYin.One.Dencoder
{
public class TextEncoder : Encoder
{
public TextEncoder(Converter.Converter converter) : base(converter)
{
}
public override void AddWatermark(Mat com, string watermark)
{
if (Utils.IsAscii(watermark))
{
this.converter.AddTextWatermark(com, watermark);
}
else
{
this.converter.AddImageWatermark(com, Utils.DrawNonAscii(watermark));
}
}
}
}