18

我已设法使用此处的答案将相机预览旋转为纵向: http ://code.google.com/p/zxing/issues/detail?id=178#c46

但是预览本身被拉伸/扭曲 - 高度似乎正在拉伸以填充框(但可能是宽度对于屏幕来说太大并且它被挤压以适应)

有什么方法可以“包装”内容而不是使其拉伸以适应表面视图?

编辑:我通过更改“CameraConfigurationManager”中的“findBestPreviewSizeValue”方法并反转 X 和 Y 点(感谢 sean owen 提供下面的指针!)对其进行排序,这是代码:

private static Point findBestPreviewSizeValue(CharSequence previewSizeValueString,
  Point screenResolution) {
int bestX = 0;
int bestY = 0;
int diff = Integer.MAX_VALUE;
for (String previewSize : COMMA_PATTERN.split(previewSizeValueString)) {

  previewSize = previewSize.trim();
  int dimPosition = previewSize.indexOf('x');
  if (dimPosition < 0) {
    Log.w(TAG, "Bad preview-size: " + previewSize);
    continue;
  }

  int newX;
  int newY;
  try {
    newY = Integer.parseInt(previewSize.substring(0, dimPosition));
    newX = Integer.parseInt(previewSize.substring(dimPosition + 1));
    //original:
    //newX = Integer.parseInt(previewSize.substring(0, dimPosition));
    //newY = Integer.parseInt(previewSize.substring(dimPosition + 1));
  } catch (NumberFormatException nfe) {
    Log.w(TAG, "Bad preview-size: " + previewSize);
    continue;
  }

  int newDiff = Math.abs(newX - screenResolution.x) + Math.abs(newY - screenResolution.y);
  if (newDiff == 0) {
    bestX = newY;
    bestY = newX;
    //original:
    //bestX = newX;
    //bestY = newY;
    break;
  } else if (newDiff < diff) {
    bestX = newY;
    bestY = newX;
    //original:
    //bestX = newX;
    //bestY = newY;
    diff = newDiff;
  }

}

if (bestX > 0 && bestY > 0) {
  return new Point(bestX, bestY);
}
return null;
}
4

5 回答 5

6

除了将预览设置为纵向模式外,您还需要做更多的工作才能使其正常工作。例如,您需要选择合适的预览尺寸,因为它是纵向的,而不是横向的。

包装肯定是不可取的?不,你不能让它包装;它总是将整个预览图像扔到 上SurfaceView,如果需要,可以拉伸。我想你可以重新实现很多代码来做一些不同的事情,但这会很困难。

于 2012-07-17T10:34:12.307 回答
2

我有同样的问题,但我可以通过反转 findBestPreviewSizeValue 内的 CameraConfigurationManager.java 文件的纵横比来解决这个问题

 float screenAspectRatio = (float) screenResolution.x / (float) screenResolution.y;

对此

 float screenAspectRatio = (float) screenResolution.y / (float) screenResolution.x;

我在这里找到了解决方案。

http://bighow.net/4660010-Android_zxing___portrait_camera_preview_surfaceview_is_stretched_warped.html

于 2014-05-29T15:56:21.883 回答
1

您可以通过此函数获取 Priview 大小:

私有列表 mSupportedPreviewSizes = parameters.getSupportedPreviewSizes();

if (mSupportedPreviewSizes != null) {
                mPreviewSize = getOptimalPreviewSize(mSupportedPreviewSizes, width, height);
            }

private Camera.Size getOptimalPreviewSize(List<Camera.Size> sizes, int w, int h) {
            final double ASPECT_TOLERANCE = 0.1;
            double targetRatio = (double) h / w;

            if (sizes == null)
                return null;

            Camera.Size optimalSize = null;
            double minDiff = Double.MAX_VALUE;

            int targetHeight = h;

            for (Camera.Size size : sizes) {
                double ratio = (double) size.height / size.width;
                if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE)
                    continue;

                if (Math.abs(size.height - targetHeight) < minDiff) {
                    optimalSize = size;
                    minDiff = Math.abs(size.height - targetHeight);
                }
            }

            if (optimalSize == null) {
                minDiff = Double.MAX_VALUE;
                for (Camera.Size size : sizes) {
                    if (Math.abs(size.height - targetHeight) < minDiff) {
                        optimalSize = size;
                        minDiff = Math.abs(size.height - targetHeight);
                    }
                }
            }

            return optimalSize;
        }


            float ratio;
            if (mPreviewSize.height >= mPreviewSize.width)
                ratio = (float) mPreviewSize.height / (float) mPreviewSize.width;
            else
                ratio = (float) mPreviewSize.width / (float) mPreviewSize.height;

            after getting ratio set your size in 

            protected void onMeasure(int widthMeasureSpec, int                              heightMeasureSpec) {

            setMeasuredDimension((int) (width * ratio), height);
            }
于 2016-05-18T10:52:55.783 回答
0

我设法通过更改以下代码来使其工作CameraConfigurationManager.java

float screenAspectRatio = (float) screenResolution.x / (float) screenResolution.y;

到:

float screenAspectRatio = (float) screenResolution.y / (float) screenResolution.x;

float aspectRatio = (float) maybeFlippedWidth / (float) maybeFlippedHeight;

到:

float aspectRatio = (float) maybeFlippedHeight / (float) maybeFlippedWidth;

我在这篇文章旁边做了这个:Android 上的人像模式下的 Zxing 相机

希望这对其他人有帮助。

于 2016-11-03T12:46:48.730 回答
0

变形的原因可能是相机的预览纵横比与显示器的纵横比不匹配。

大多数手机屏幕的长宽比是4:3,如2560x1440、1920x1080等。当您使用 parameter.setPreviewSize(width,height) 设置相机预览尺寸时,如果 height : width 的值不等于您手机屏幕的纵横比,相机的图片将变形以填满整个屏幕。

于 2017-03-10T10:23:10.667 回答