Codemaru 1.0.0.0
QR 코드, 모바일 랜딩 페이지, vCard 연락처 저장과 명함 디자인을 한 화면에서 제공하는 디지털 명함 서비스입니다.
로딩중...
검색중...
일치하는것 없음
Codemaru.Services.ImageBackgroundRemover 클래스 참조sealed

더 자세히 ...

Public 멤버 함수

string CreateDataUrl (byte[] imageBytes, string contentType, bool removeBackground)

정적 Private 멤버 함수

static byte[] NormalizeLogo (byte[] imageBytes, bool removeBackground)
static void RemoveBackgroundPixels (byte[] pixels, int width, int height, int stride)
static BitmapSource FitIntoCanvas (BitmapSource source)
static Int32Rect FindVisibleBounds (byte[] pixels, int width, int height, int stride)
static Color SampleBackground (byte[] pixels, int width, int height, int stride)
static double ColorDistance (byte r1, byte g1, byte b1, byte r2, byte g2, byte b2)

정적 Private 속성

const double ClearDistance = 42
const double SoftDistance = 86
const int LogoCanvasWidth = 640
const int LogoCanvasHeight = 400
const int LogoPadding = 42

상세한 설명

Image Background Remover 기능과 관련 상태를 캡슐화합니다.

ImageBackgroundRemover.cs 파일의 16 번째 라인에서 정의되었습니다.

멤버 함수 문서화

◆ ColorDistance()

double Codemaru.Services.ImageBackgroundRemover.ColorDistance ( byte r1,
byte g1,
byte b1,
byte r2,
byte g2,
byte b2 )
inlinestaticprivate

Color Distance 작업을 수행합니다.

매개변수
r1r1에 사용할 byte 값입니다.
g1g1에 사용할 byte 값입니다.
b1b1에 사용할 byte 값입니다.
r2r2에 사용할 byte 값입니다.
g2g2에 사용할 byte 값입니다.
b2b2에 사용할 byte 값입니다.
반환값
Color Distance 작업에서 생성한 double 결과입니다.

ImageBackgroundRemover.cs 파일의 520 번째 라인에서 정의되었습니다.

521 {
522 var r = r1 - r2;
523 var g = g1 - g2;
524 var b = b1 - b2;
525 return Math.Sqrt(r * r + g * g + b * b);
526 }

다음에 의해서 참조됨 : RemoveBackgroundPixels().

이 함수를 호출하는 함수들에 대한 그래프입니다.:

◆ CreateDataUrl()

string Codemaru.Services.ImageBackgroundRemover.CreateDataUrl ( byte[] imageBytes,
string contentType,
bool removeBackground )
inline

Data Url 값을 생성합니다.

매개변수
imageBytesimage Bytes에 사용할 byte[] 값입니다.
contentTypecontent Type에 사용할 string 값입니다.
removeBackgroundremove Background에 사용할 bool 값입니다.
반환값
Create Data Url 작업에서 생성한 string 결과입니다.

ImageBackgroundRemover.cs 파일의 104 번째 라인에서 정의되었습니다.

105 {
106 if (contentType.Contains("svg", StringComparison.OrdinalIgnoreCase))
107 {
108 return $"data:{contentType};base64,{Convert.ToBase64String(imageBytes)}";
109 }
110
111 try
112 {
113 var png = NormalizeLogo(imageBytes, removeBackground);
114 return $"data:image/png;base64,{Convert.ToBase64String(png)}";
115 }
116 catch
117 {
118 return $"data:{contentType};base64,{Convert.ToBase64String(imageBytes)}";
119 }
120 }

다음을 참조함 : NormalizeLogo().

이 함수 내부에서 호출하는 함수들에 대한 그래프입니다.:

◆ FindVisibleBounds()

Int32Rect Codemaru.Services.ImageBackgroundRemover.FindVisibleBounds ( byte[] pixels,
int width,
int height,
int stride )
inlinestaticprivate

Visible Bounds 항목을 찾습니다.

매개변수
pixelspixels에 사용할 byte[] 값입니다.
widthwidth에 사용할 int 값입니다.
heightheight에 사용할 int 값입니다.
stridestride에 사용할 int 값입니다.
반환값
Find Visible Bounds 작업에서 생성한 Int32Rect 결과입니다.

ImageBackgroundRemover.cs 파일의 351 번째 라인에서 정의되었습니다.

352 {
353 var left = width;
354 var top = height;
355 var right = -1;
356 var bottom = -1;
357
358 for (var y = 0; y < height; y++)
359 {
360 for (var x = 0; x < width; x++)
361 {
362 var alpha = pixels[y * stride + x * 4 + 3];
363 if (alpha <= 12)
364 {
365 continue;
366 }
367
368 left = Math.Min(left, x);
369 top = Math.Min(top, y);
370 right = Math.Max(right, x);
371 bottom = Math.Max(bottom, y);
372 }
373 }
374
375 if (right < left || bottom < top)
376 {
377 return new Int32Rect(0, 0, width, height);
378 }
379
380 return new Int32Rect(left, top, right - left + 1, bottom - top + 1);
381 }

다음에 의해서 참조됨 : NormalizeLogo().

이 함수를 호출하는 함수들에 대한 그래프입니다.:

◆ FitIntoCanvas()

BitmapSource Codemaru.Services.ImageBackgroundRemover.FitIntoCanvas ( BitmapSource source)
inlinestaticprivate

Fit Into Canvas 작업을 수행합니다.

매개변수
sourcesource에 사용할 BitmapSource 값입니다.
반환값
Fit Into Canvas 작업에서 생성한 BitmapSource 결과입니다.

ImageBackgroundRemover.cs 파일의 280 번째 라인에서 정의되었습니다.

281 {
282 var availableWidth = LogoCanvasWidth - LogoPadding * 2;
283 var availableHeight = LogoCanvasHeight - LogoPadding * 2;
284 var scale = Math.Min((double)availableWidth / source.PixelWidth, (double)availableHeight / source.PixelHeight);
285 var targetWidth = Math.Max(1, source.PixelWidth * scale);
286 var targetHeight = Math.Max(1, source.PixelHeight * scale);
287 var left = (LogoCanvasWidth - targetWidth) / 2;
288 var top = (LogoCanvasHeight - targetHeight) / 2;
289
290 var visual = new DrawingVisual();
291 using (var context = visual.RenderOpen())
292 {
293 context.DrawRectangle(Brushes.Transparent, null, new Rect(0, 0, LogoCanvasWidth, LogoCanvasHeight));
294 context.DrawImage(source, new Rect(left, top, targetWidth, targetHeight));
295 }
296
297 var target = new RenderTargetBitmap(LogoCanvasWidth, LogoCanvasHeight, 96, 96, PixelFormats.Pbgra32);
298 target.Render(visual);
299 target.Freeze();
300 return target;
301 }

다음을 참조함 : LogoCanvasHeight, LogoCanvasWidth, LogoPadding.

다음에 의해서 참조됨 : NormalizeLogo().

이 함수를 호출하는 함수들에 대한 그래프입니다.:

◆ NormalizeLogo()

byte[] Codemaru.Services.ImageBackgroundRemover.NormalizeLogo ( byte[] imageBytes,
bool removeBackground )
inlinestaticprivate

Normalize Logo 작업을 수행합니다.

매개변수
imageBytesimage Bytes에 사용할 byte[] 값입니다.
removeBackgroundremove Background에 사용할 bool 값입니다.
반환값
Normalize Logo 작업에서 생성한 byte[] 결과입니다.

ImageBackgroundRemover.cs 파일의 154 번째 라인에서 정의되었습니다.

155 {
156 using var source = new MemoryStream(imageBytes);
157 var decoder = BitmapDecoder.Create(source, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
158 var frame = decoder.Frames[0];
159 var bitmap = new FormatConvertedBitmap(frame, PixelFormats.Bgra32, null, 0);
160 var width = bitmap.PixelWidth;
161 var height = bitmap.PixelHeight;
162 var stride = width * 4;
163 var pixels = new byte[stride * height];
164
165 bitmap.CopyPixels(pixels, stride, 0);
166
167 if (removeBackground)
168 {
169 RemoveBackgroundPixels(pixels, width, height, stride);
170 }
171
172 var prepared = BitmapSource.Create(width, height, 96, 96, PixelFormats.Bgra32, null, pixels, stride);
173 var bounds = removeBackground
174 ? FindVisibleBounds(pixels, width, height, stride)
175 : new Int32Rect(0, 0, width, height);
176 var cropped = new CroppedBitmap(prepared, bounds);
177 var fitted = FitIntoCanvas(cropped);
178
179 var encoder = new PngBitmapEncoder();
180 encoder.Frames.Add(BitmapFrame.Create(fitted));
181
182 using var target = new MemoryStream();
183 encoder.Save(target);
184 return target.ToArray();
185 }

다음을 참조함 : FindVisibleBounds(), FitIntoCanvas(), RemoveBackgroundPixels().

다음에 의해서 참조됨 : CreateDataUrl().

이 함수 내부에서 호출하는 함수들에 대한 그래프입니다.:
이 함수를 호출하는 함수들에 대한 그래프입니다.:

◆ RemoveBackgroundPixels()

void Codemaru.Services.ImageBackgroundRemover.RemoveBackgroundPixels ( byte[] pixels,
int width,
int height,
int stride )
inlinestaticprivate

Background Pixels 항목을 제거합니다.

매개변수
pixelspixels에 사용할 byte[] 값입니다.
widthwidth에 사용할 int 값입니다.
heightheight에 사용할 int 값입니다.
stridestride에 사용할 int 값입니다.

ImageBackgroundRemover.cs 파일의 227 번째 라인에서 정의되었습니다.

228 {
229 var background = SampleBackground(pixels, width, height, stride);
230 for (var y = 0; y < height; y++)
231 {
232 for (var x = 0; x < width; x++)
233 {
234 var i = y * stride + x * 4;
235 var distance = ColorDistance(
236 pixels[i + 2],
237 pixels[i + 1],
238 pixels[i],
239 background.R,
240 background.G,
241 background.B);
242
243 if (distance <= ClearDistance)
244 {
245 pixels[i + 3] = 0;
246 }
247 else if (distance < SoftDistance)
248 {
249 var keep = (distance - ClearDistance) / (SoftDistance - ClearDistance);
250 pixels[i + 3] = (byte)Math.Min(pixels[i + 3], Math.Round(255 * keep));
251 }
252 }
253 }
254 }

다음을 참조함 : ClearDistance, ColorDistance(), SampleBackground(), SoftDistance.

다음에 의해서 참조됨 : NormalizeLogo().

이 함수 내부에서 호출하는 함수들에 대한 그래프입니다.:
이 함수를 호출하는 함수들에 대한 그래프입니다.:

◆ SampleBackground()

Color Codemaru.Services.ImageBackgroundRemover.SampleBackground ( byte[] pixels,
int width,
int height,
int stride )
inlinestaticprivate

Sample Background 작업을 수행합니다.

매개변수
pixelspixels에 사용할 byte[] 값입니다.
widthwidth에 사용할 int 값입니다.
heightheight에 사용할 int 값입니다.
stridestride에 사용할 int 값입니다.
반환값
Sample Background 작업에서 생성한 Color 결과입니다.

ImageBackgroundRemover.cs 파일의 431 번째 라인에서 정의되었습니다.

432 {
433 var points = new[]
434 {
435 (X: 0, Y: 0),
436 (X: width - 1, Y: 0),
437 (X: 0, Y: height - 1),
438 (X: width - 1, Y: height - 1)
439 };
440
441 var r = 0;
442 var g = 0;
443 var b = 0;
444
445 foreach (var point in points)
446 {
447 var i = point.Y * stride + point.X * 4;
448 b += pixels[i];
449 g += pixels[i + 1];
450 r += pixels[i + 2];
451 }
452
453 return Color.FromRgb((byte)(r / points.Length), (byte)(g / points.Length), (byte)(b / points.Length));
454 }

다음에 의해서 참조됨 : RemoveBackgroundPixels().

이 함수를 호출하는 함수들에 대한 그래프입니다.:

멤버 데이터 문서화

◆ ClearDistance

const double Codemaru.Services.ImageBackgroundRemover.ClearDistance = 42
staticprivate

Clear Distance 값을 보관합니다.

ImageBackgroundRemover.cs 파일의 26 번째 라인에서 정의되었습니다.

다음에 의해서 참조됨 : RemoveBackgroundPixels().

◆ LogoCanvasHeight

const int Codemaru.Services.ImageBackgroundRemover.LogoCanvasHeight = 400
staticprivate

Logo Canvas Height 값을 보관합니다.

ImageBackgroundRemover.cs 파일의 53 번째 라인에서 정의되었습니다.

다음에 의해서 참조됨 : FitIntoCanvas().

◆ LogoCanvasWidth

const int Codemaru.Services.ImageBackgroundRemover.LogoCanvasWidth = 640
staticprivate

Logo Canvas Width 값을 보관합니다.

ImageBackgroundRemover.cs 파일의 44 번째 라인에서 정의되었습니다.

다음에 의해서 참조됨 : FitIntoCanvas().

◆ LogoPadding

const int Codemaru.Services.ImageBackgroundRemover.LogoPadding = 42
staticprivate

Logo Padding 값을 보관합니다.

ImageBackgroundRemover.cs 파일의 62 번째 라인에서 정의되었습니다.

다음에 의해서 참조됨 : FitIntoCanvas().

◆ SoftDistance

const double Codemaru.Services.ImageBackgroundRemover.SoftDistance = 86
staticprivate

Soft Distance 값을 보관합니다.

ImageBackgroundRemover.cs 파일의 35 번째 라인에서 정의되었습니다.

다음에 의해서 참조됨 : RemoveBackgroundPixels().


이 클래스에 대한 문서화 페이지는 다음의 파일로부터 생성되었습니다.: