Image and Canvas coordinate displacement when drawing

I am drawing lines on Canvas of SurfaceView which is in FrameLayout. I receive image from camera preview, process it, get coordinates of rectangles and raw the lines of it on canvas. I get a displacement of those lines in y axis when drawn, the lower the line is, the bigger displacement occurs (see photos below):

With red line I marked (with Paint program, not the actual app) the approximate position of where the coordinates coordinates of lower line are on bitmap, and the lower green line of rectangle (placed by the actual app, drawn on canvas) along with red line shows, how much coordinates are displaced. The coordinates on top cannot cross the screen, but on the bottom they can- if the rectangle is low enough, lines come below the screen, but on top they do not (they should not do that on either of all 4 sides, because the given coordinates are never out of bitmap).

The drawing function looks like this:

public void DrawAllContours(List<Point[]> rectPoints, int ratio)

    canvas = sh.lockCanvas();
    canvas.drawColor(0, PorterDuff.Mode.CLEAR);

    for (int i = 0; i < rectPoints.size(); i++)
        for (int j = 0; j < 4; j++)
            canvas.drawLine((float)rectPoints.get(i)[j].x*ratio, (float)rectPoints.get(i)[j].y*ratio,
                    (float)rectPoints.get(i)[(j+1)%4].x*ratio, (float)rectPoints.get(i)[(j+1)%4].y*ratio, paint);


The ratio variable is the difference between the actual preview image size and resized bitmap for processing size (usualy the value is 3). The actual coordinates are as they should be on preview bitmap, but not on Canvas.

How can I overcome this issue and place lines on their exact places?

Thank you in advance.


So I found out, that camera preview size does differ from the canvas size. My preview size was 1920X1080, and canvas 1845X1080 or so, so thats why Y coordinate was displaced. I just found out the difference ratio and drew lines respectively by that ratio. Canvas resolution can be found with Canvas.getWidth(), Canvas.getHeight().

Need Your Help

C# Confusing Results from Performance Test

c# .net performance profiling stopwatch

I am currently working on an image processing application. The application captures images from a webcam and then does some processing on it. The app needs to be real time responsive (ideally &lt; ...

How to Remove/Replace Alfresco from Java application with Drupal or Drupal plugin?

java drupal alfresco

OK, here are some more details. I need to remove/replace Alfresco from a Java application, the content that Alfresco manages is some HTML and some PDFs. My team wants to move the content to a remote