Android zxing - portrait camera preview/surfaceview is stretched/warped Android zxing - portrait camera preview/surfaceview is stretched/warped android android

Android zxing - portrait camera preview/surfaceview is stretched/warped


You have to do a fair bit more to get this to work than just set the preview to portrait mode. For example, you need to choose an appropriate preview size now that it's in portrait, not landscape.

Wrapping is surely not desirable? No you can't make it wrap; it will always fling the whole preview image onto the SurfaceView, stretching if needed. I suppose you could reimplement a lot of that code to do something different, but it would be quite hard.


I had the same problem but I could resolved it by inverting the aspect ratio on the CameraConfigurationManager.java file inside the findBestPreviewSizeValue changing this

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

to this

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

I found the solution here.

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


You can get Priview size by this function:

private List 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);            }