ZBar相机预览拉伸Android

时间:2013-12-04 07:34:47

标签: android sdk camera preview zbar

我正在使用Zbar以横向模式扫描QRCodes。我在xml中有一个FrameLayout,它嵌套在RelativeLayout中,而FrameLayout的大小是300dp X 200dp。我一直在谷歌搜索两个说,并几乎阅读有关此问题的所有stackoverflow线程。但似乎没有什么对我有用。

这是我的代码。

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
    private SurfaceHolder mHolder;
    private Camera mCamera;
    private PreviewCallback previewCallback;
    private AutoFocusCallback autoFocusCallback;

    public CameraPreview(Context context, Camera camera,
                         PreviewCallback previewCb,
                         AutoFocusCallback autoFocusCb) {
        super(context);


        mCamera = camera;
        previewCallback = previewCb;
        autoFocusCallback = autoFocusCb;

        /* 
         * Set camera to continuous focus if supported, otherwise use
         * software auto-focus. Only works for API level >=9.
         */
        /*
        Camera.Parameters parameters = camera.getParameters();
        for (String f : parameters.getSupportedFocusModes()) {
            if (f == Parameters.FOCUS_MODE_CONTINUOUS_PICTURE) {
                mCamera.setFocusMode(Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
                autoFocusCallback = null;
                break;
            }
        }
        */

        // Install a SurfaceHolder.Callback so we get notified when the
        // underlying surface is created and destroyed.
        mHolder = getHolder();
        mHolder.addCallback(this);

        // deprecated setting, but required on Android versions prior to 3.0
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    public void surfaceCreated(SurfaceHolder holder) {
        // The Surface has been created, now tell the camera where to draw the preview.
        try {
            if(holder != null)
            {
                mCamera.setPreviewDisplay(holder);
                Camera.Parameters parameters = mCamera.getParameters();
                List<Size> sizes = parameters.getSupportedPictureSizes();
                parameters.setPictureSize(sizes.get(0).width, sizes.get(0).height); // mac dinh solution 0
                parameters.set("orientation","landscape");
                //parameters.setPreviewSize(viewWidth, viewHeight);
                List<Size> size = parameters.getSupportedPreviewSizes();
                parameters.setPreviewSize(size.get(0).width, size.get(0).height);
                mCamera.setParameters(parameters);
            }
        } catch (IOException e) {
            Log.d("DBG", "Error setting camera preview: " + e.getMessage());
        }
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        // Camera preview released in activity
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        /*
         * If your preview can change or rotate, take care of those events here.
         * Make sure to stop the preview before resizing or reformatting it.
         */
        if (mHolder.getSurface() == null){
          // preview surface does not exist
          return;
        }

        // stop preview before making changes
        try {
            mCamera.stopPreview();
        } catch (Exception e){
          // ignore: tried to stop a non-existent preview
        }

        try {
            // Hard code camera surface rotation 90 degs to match Activity view in portrait
            mCamera.setDisplayOrientation(0);

            mCamera.setPreviewDisplay(mHolder);
            mCamera.setPreviewCallback(previewCallback);
            mCamera.startPreview();
            mCamera.autoFocus(autoFocusCallback);
        } catch (Exception e){
            Log.d("DBG", "Error starting camera preview: " + e.getMessage());
        }
    }



}

任何帮助都将受到高度赞赏。

由于

2 个答案:

答案 0 :(得分:0)

表面的纵横比(300dp X 200dp)似乎与您的相机previewSize不匹配。首先确定您的previewSize(parameters.getPreviewSize())的宽高比,然后将曲面的大小设置为相同的宽高比。当纵横比几乎相同时,拉伸应该消失。希望有所帮助!

答案 1 :(得分:0)

阿里是对的。您可以找到基于Ali解决方案的代码here