Android zxing - 人像摄像头预览/ surfaceview拉伸/扭曲

时间:2012-07-17 09:52:16

标签: android surfaceview zxing portrait stretching

我设法使用此处的答案将相机预览旋转为纵向: 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;
}

5 个答案:

答案 0 :(得分:6)

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

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

答案 1 :(得分: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

答案 2 :(得分: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);
            }

答案 3 :(得分: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;

TO:

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

我在这篇文章中做到了这一点:Zxing Camera in Portrait mode on Android

希望这有助于其他人。

答案 4 :(得分:0)

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

手机屏幕的最大宽高比为4:3,如2560x1440,1920x1080等。当您使用parameter.setPreviewSize(width,height)设置相机预览尺寸时,如果height:width的值不等于您的手机屏幕的宽高比,相机的图片将变形以填充屏幕。