disableSlope method
this function gets line list and removes the slope handles problem of rotated images
Implementation
Future<List<Line>?> disableSlope(List<Line>? lines) async {
try {
var minx = 0.0;
var miny = 0.0;
var slopeSum = 0.0;
int slopeCount = 0;
for (var l in lines!) {
if (l.text!.length > averageLineLength) {
slopeSum = slopeSum + slopeOfLine(l);
slopeCount++;
}
}
if (slopeCount == 0) return lines;
var tan = slopeSum / slopeCount; //average of slopes
var cos2 = 1 / (1 + tan * tan);
var sin2 = (tan * tan) * cos2;
var cos = sqrt(cos2);
var sin = sqrt(sin2);
//negative the angle(it should rotate back to get into x axis.)
if (tan > 0) {
sin = -sin;
}
if (cos == 0) return lines;
///Rotate formula
///x' = x cos - y sin
///y' = x sin + y cos
// var maxx = 0;
// var maxy = 0;
// for (Line l in lines) {
// if (l.cornerList![0].x > maxx) {
// maxx = l.cornerList![0].x;
// }
// if (l.cornerList![0].y > maxy) {
// maxy = l.cornerList![0].y;
// }
// }
for (Line l in lines) {
for (int i = 0; i < l.cornerList!.length; i++) {
var x = -1 * l.cornerList![i].y;
var y = -1 * l.cornerList![i].x;
// var x = maxy - l.cornerList![i].y;
// var y = maxx - l.cornerList![i].x;
var xx = x * cos - y * sin;
var yy = x * sin + y * cos;
l.cornerList![i].x = -1 * yy;
l.cornerList![i].y = -1 * xx;
if (l.cornerList![i].x < minx) minx = l.cornerList![i].x;
if (l.cornerList![i].y < miny) miny = l.cornerList![i].y;
}
}
// for (Line l in lines) {
// if (l.cornerList![0].x < minx) {
// minx = l.cornerList![0].x;
// }
// if (l.cornerList![0].y < miny) {
// miny = l.cornerList![0].y;
// }
// }
// for (Line l in lines) {
// for (int i = 0; i < l.cornerList!.length; i++) {
// l.cornerList![i].x = l.cornerList![i].x - minx;
// l.cornerList![i].y = l.cornerList![i].y - miny;
// }
// }
if (minx < 0 || miny < 0) {
for (Line l in lines) {
for (int i = 0; i < l.cornerList!.length; i++) {
l.cornerList![i].x = l.cornerList![i].x - minx;
l.cornerList![i].y = l.cornerList![i].y - miny;
}
}
}
return lines;
} catch (e, stacktrace) {
print("disableSlope: " + e.toString());
print('Stacktrace: ' + stacktrace.toString());
return lines;
}
}