我目前有一个应用程序,onClick
将在电池和蓝色纸条周围绘制一个绿色的边界矩形。我还希望按钮onClick
在电池和纸带之间画一条线(如下图所示)。目前,我能够获得矩形的所有x和y值,因此知道我需要从534,1261
到788,1261
画一条线,并用x差异标记该线,如图所示。
最佳答案
对于绘制线条和文本,您可以使用如下代码:
Point firstPoint = new Point(100, 200);
Point secondPoint = new Point(100, 400);
Point middlePoint = new Point(firstPoint.x,
firstPoint.y + 0.5 * (secondPoint.y - firstPoint.y));
Scalar lineColor = new Scalar(255, 0, 0, 255);
int lineWidth = 3;
Scalar textColor = new Scalar(255, 0, 0, 255);
Imgproc.line(sourceMat, firstPoint, secondPoint, lineColor, lineWidth);
Imgproc.putText(sourceMat, " Text" , middlePoint,
Core.FONT_HERSHEY_PLAIN, 1.5 , textColor);
其中
sourceMat
-带有图像的Mat
。为了确定以厘米为单位的“高度”线(大约),应使用电池矩形的“高度”:
lineHeightCm = 4.46 / heightOfBatteryRectangleInPixels * lineHeightInPixels;
其中4.46-AAA电池的“高度”(以厘米为单位)。