6

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

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

据我所知,我得到了照片的高度和宽度以及照片的高度和宽度的所有正确值,但我不确定在哪里或如何调整拉伸因子。现在,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>

4

0 回答 0