简体   繁体   English

从自定义视图缩放形状坐标以适应不同的屏幕尺寸

[英]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) 相机预览尺寸始终为: 640x480 (坐标与此尺寸匹配)
  • 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 在大多数情况下,图片尺寸会很大(在我的手机中是4260x3120 ),并且我正在调整画布的大小,使其适合屏幕大小

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 : 我的xml文件看起来像这样:

<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. 在我的自定义视图类CropImageView中,我将四个坐标传递给onDraw方法并在它们之间绘制路径

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 : 我要做的就是按照以下步骤找到YX的比例因子:

    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. 另外,您还需要确保preview sizepicture sizecontainer picture sizecontainer具有相同的宽高比,否则坐标会放错位置。

to get the aspect ration you need simply need to do this : width / height . 要获得长宽比,您只需要这样做: width / height

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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