Android - 如何将视图坐标传递到曲面视图坐标

时间:2016-06-29 05:41:56

标签: android camera surfaceview zxing

在我的应用程序中,我有扫描仪视图,必须使用zxing库扫描两个条形码。顶级条形码为PDF417格式,底部 - 在DATAMATRIX中。我使用https://github.com/dm77/barcodescanner作为基础,但主要区别:我必须使用图像坐标作为扫描区域。主要算法:

  1. 取决于当前步骤,扫描活动将当前扫描区域传递到屏幕坐标中的扫描仪视图。这些坐标计算如下:
  2. public static Rect getScanRectangle(View view) { int[] l = new int[2]; view.measure(0, 0); view.getLocationOnScreen(l); return new Rect(l[0], l[1], l[0] + view.getMeasuredWidth(), l[1] + view.getMeasuredHeight()); }

    2.在扫描仪视图中,在onPreviewFrame方法中,从相机参数接收相机预览尺寸。当我将字母数据从相机转换为内存中的位图图像时,我看到它旋转了90度,并且相机分辨率不等于屏幕分辨率。所以,我必须将屏幕坐标映射到相机(或表面视图)坐标:

    private Rect normalizeScreenCoordinates(Rect input, int cameraWidth, int cameraHeight) {
        if(screenSize == null) {
            screenSize = new Point();
            Display display = activity.getWindowManager().getDefaultDisplay();
            display.getSize(screenSize);
        }
        int height = screenSize.x;
        int width = screenSize.y;
        float widthCoef = (float)cameraWidth/width;
        float heightCoef = (float)cameraHeight/height;
        Rect result = new Rect((int)(input.top * widthCoef), (int)(input.left * heightCoef), (int)(input.bottom * widthCoef), (int)(input.right * heightCoef));
        return result;
    }
    
    1. 之后,翻译的坐标进入削减,在大多数测试设备上,所有工作正常。但不是Nexus 5X。首先,显示大小和activity.getWindow()。getDecorView()大小之间存在严重差距。也许这与状态栏大小有关,这是半透明的,由于某种原因,它的高度可能没有计算。但是,即使我添加了垂直偏移,扫描区域也有问题。这个错误可能是什么原因?

0 个答案:

没有答案