Android相机预览失真/大小调整的问题

时间:2012-10-07 16:36:47

标签: android android-layout android-camera

我有一个应用程序,其中照片的预览尺寸小于手机的屏幕。我已经阅读了很多关于如何解决这个问题的帖子/想法,但我的预览仍然是扭曲的。

理想情况下,我可以将预览区域设置为预设区域并“拉伸并重新居中”预览,这样照片的边缘就不会显示在屏幕上。

据我所知,我得到了身高和高度的所有正确值。照片的宽度和高度&照片的宽度,但我不知道我可以在何处或如何调整拉伸系数。现在,640x480(或任何尺寸)照片被填充到较小的预览区域中。

这是我正在使用的代码:

class Preview extends SurfaceView implements SurfaceHolder.Callback {

    SurfaceHolder mHolder;
    public Camera camera;
    boolean writingFile = false;
    Size theBiggest=null;
    Size screenSize=null;
    Float cameraRatio=null;


    Preview(Context context) {
        super(context);

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


    //
    // surfaceCreate is called the first time the Camera Tab is loaded
    //
    public void surfaceCreated(SurfaceHolder holder) {
        // The Surface has been created, acquire the camera and tell it where to draw.
        camera = Camera.open();
        camera.setDisplayOrientation(90);

        // Get For Photo Size
        Parameters camparams = camera.getParameters();

        // Find the Largest Possible Photo Size
        List<Size> sizes = camparams.getSupportedPictureSizes();
        int maxWidth = 0;
        int maxHeight = 0;
        for (Size s : sizes) {

            if (s.width > maxWidth || s.height > maxHeight) {
                    maxWidth = s.width;
                    maxHeight = s.height;
                    theBiggest = s;
            }
        }

        // Set Photo Size
        camparams.setPictureSize(theBiggest.width, theBiggest.height);
        camera.setParameters(camparams);

    } // end surfaceCreate()


    public void surfaceDestroyed(SurfaceHolder holder) {
        // Surface will be destroyed when we return, so stop the preview.
        // Because the CameraDevice object is not a shared resource, it's very
        // important to release it when the activity is paused.
        camera.setPreviewCallback(null);
        camera.stopPreview();
        camera.release();
        camera = null;
    }


    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {

        // Now that the size is known, set up the camera parameters and begin
        // the preview.
        camera.setDisplayOrientation(90);
        Camera.Parameters parameters = camera.getParameters();
        Camera.Size size = getBestPreviewSize(w, h);    
        parameters.setPreviewSize(size.width, size.height); // preview size
        camera.setParameters(parameters);
        camera.startPreview();  
    }


    private Camera.Size getBestPreviewSize(int width, int height)
    {

                // Get For Photo Size
        Camera.Parameters camparams = camera.getParameters();

        // Find the Largest Possible Preview Sizes
        List<Size> sizes = camparams.getSupportedPreviewSizes();
        Camera.Size result=null;
        for (Size s : sizes) {

            if (s.width <= width && s.height <= height) {
                       if (result == null) {
                        result = s;
                       } else {
                        int resultArea=result.width*result.height; 
                        int newArea=s.width*s.height;

                        if (newArea>resultArea) {
                                            result=s;
                        }
                           } // end else (result=null)
                } // end if (width<width&&height<height)
        } // end for

            return result;

    } // end function

}

这是我的布局xml文件

    <FrameLayout android:layout_weight="1" 
                    android:id="@+id/preview" 
                    android:fitsSystemWindows="true" 
                    android:layout_height="0dp" 
                    android:layout_width="wrap_content">
    </FrameLayout>

    <Button android:text="Take Photo"               
                android:id="@+id/buttonClick" 
            android:layout_height="wrap_content"
            android:layout_width="200dp" 
            android:layout_gravity="center"
            android:textSize="12sp">
    </Button>

0 个答案:

没有答案