-
Notifications
You must be signed in to change notification settings - Fork 1
/
ScannedImage.cs
134 lines (113 loc) · 4.13 KB
/
ScannedImage.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
namespace reversal_film_scanner_rework_processing
{
class ScannedImage
{
private const double RrgbIsBlackLimit = 45; // Sum of R, G, B
private const double OriantationConvictionLevel = 95.00; // Percent
public string ImagePath { get; set; }
private Bitmap Image { get; set; }
public ScannedImage(string path)
{
this.UpdatePath(path);
}
private void UpdatePath(string path){
this.ImagePath = path;
this.Image = new Bitmap(this.ImagePath, true);
}
public void Rename(string prefix, string numberFormat, int index)
{
string fileName = Path.GetFileName(ImagePath);
string directory = Path.GetDirectoryName(ImagePath);
string extension = Path.GetExtension(ImagePath);
string newFileName = prefix + String.Format(numberFormat, index) + extension;
string newPath = $"{directory}/{newFileName}";
File.Move(ImagePath, newPath);
this.UpdatePath(newPath);
}
public Orientation GetOriantation()
{
double left = GetPartBlackPercentage(1070, 1670, 700, 3200);
double right = GetPartBlackPercentage(4170, 4770, 700, 3200);
double percantageAvg = (left + right) / 2;
if (percantageAvg > OriantationConvictionLevel)
{
return Orientation.PORTRAIT;
}
else
{
return Orientation.LANDSCAPE;
}
}
public double GetPartBlackPercentage(int xStart, int xEnd, int yStart, int yEnd)
{
int totalPixel = (xEnd - xStart) * (yEnd - yStart);
int blackPixel = 0;
for (int x = xStart; x < xEnd; x++)
{
for (int y = yStart; y < yEnd; y++)
{
Color pixelColor = this.Image.GetPixel(x, y);
int rgbSum = pixelColor.R + pixelColor.G + pixelColor.B;
if (rgbSum <= RrgbIsBlackLimit)
{
blackPixel++;
}
}
}
return (double) blackPixel / totalPixel * 100.00;
}
public void CropByAutoOriantation()
{
Rectangle croppingArea;
if (this.GetOriantation() == Orientation.PORTRAIT)
{
croppingArea = new Rectangle(1800, 170, 2270, 3540);
}
else
{
croppingArea = new Rectangle(1170, 800, 3540, 2270);
}
this.Crop(croppingArea);
}
public void Crop(Rectangle croppingArea)
{
using (Bitmap nb = new Bitmap(croppingArea.Width, croppingArea.Height))
{
using (Graphics g = Graphics.FromImage(nb))
{
g.DrawImage(this.Image, -croppingArea.X, -croppingArea.Y);
this.Image = nb;
this.Image.Save(this.ImagePath);
this.UpdatePath(this.ImagePath);
}
}
}
public void RotateFlip(bool rotate180, bool flipHorizontal)
{
if (rotate180 || flipHorizontal)
{
RotateFlipType rotateFlipType = RotateFlipType.RotateNoneFlipNone;
if (rotate180 == true && flipHorizontal == false)
{
rotateFlipType = RotateFlipType.Rotate180FlipNone;
}
else if (rotate180 == false && flipHorizontal == true)
{
rotateFlipType = RotateFlipType.RotateNoneFlipX;
}
else if (rotate180 == true && flipHorizontal == true)
{
rotateFlipType = RotateFlipType.Rotate180FlipX;
}
this.Image.RotateFlip(rotateFlipType);
this.Image.Save(this.ImagePath);
this.UpdatePath(this.ImagePath);
}
}
}
}