简体   繁体   中英

scale shape coordinates from custom view to adapt different screen sizes

I'm working on an app to detect objects in real time using the camera preview and then take a picture and display it in an other activity and a bounding box (most cases a polygon) will be drawn over the detected object.

I managed to do that and everything works fine. Now I'm trying to speed up the detection process by setting a small camera preview size and taking a picture with a bigger resolution by changing the camera parameters. But the coordinates that I will be getting must be scaled to match the big picture size.

In my case :

  • Camera Preview Size is always : 640x480 (the coordinates matches this size)
  • Picture taken Size will be chosen using the method mentioned in the accepted answer here .
  • In most cases the Picture size will be so big (in my phone it's 4260x3120 ) and I'm resizing the canvas so it fits the screen

For the canvas size I'm using this to get the screen resolution :

public static int getScreenWidth() {
    return Resources.getSystem().getDisplayMetrics().widthPixels;
}

public static int getScreenHeight() {
    return Resources.getSystem().getDisplayMetrics().heightPixels;
}

my xml file looks like this :

<LinearLayout>
     ....
    <android.support.v7.widget.Toolbar
      ....
    </android.support.v7.widget.Toolbar>

      ....
    <FrameLayout
        android:id="@android:id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:layout_marginBottom="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginTop="16dp">

        <packagename.CropImageView
            android:id="@+id/scannedImage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:adjustViewBounds="true"
            android:src="@mipmap/ic_launcher_round" />

    </FrameLayout>
</LinearLayout>

and in my custom view class CropImageView I'm passing four coordinates to the onDraw method and drawing paths between them.

Note : I saw a couple of questions about approximately the same subject but I couldn't relate to them since I couldn't a use case like mine. Any information, help is much appreciated.

all I needed to do is to find the scale factors for Y and X following this :

    scaleFactorY = (float) newWidth / oldWidth;
    scaleFactorX = (float) newHeight / oldHeight;

then multiply the coordinates with those factors. Also you need to make sure that the preview size , picture size and the container that will hold the picture have the same aspect ratio or else you will get misplaced coordinates.

to get the aspect ration you need simply need to do this : width / height .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM