1

嗨,我的相机应用程序有问题,显示被拉伸。但是出来的照片没问题。只有显示被拉伸。这是必要的代码,表面改变了回调

public void surfaceChanged(SurfaceHolder holder, int format,
                               int width, int height) {
      initPreview(width, height);
      startPreview();
    }

这是初始化预览功能,

private void initPreview(int width, int height) {
    if (camera != null && previewHolder.getSurface() != null) {
        try {
            camera.setPreviewDisplay(previewHolder);
        }
        catch (Throwable t) {
            Log.e("PreviewDemo-surfaceCallback",
                "Exception in setPreviewDisplay()", t);
            Toast.makeText(PictureDemo.this, t.getMessage(),
                Toast.LENGTH_LONG).show();
        }

        if (!cameraConfigured) {
            Camera.Parameters parameters=camera.getParameters();
            Camera.Size size=getOptimalPreviewSize(width, height);
            Camera.Size pictureSize=getSmallestPictureSize(parameters);

            if (size != null && pictureSize != null) {
                parameters.setPreviewSize(size.width, size.height);
                parameters.setPictureSize(pictureSize.width,
                    pictureSize.height);
                parameters.setPictureFormat(ImageFormat.JPEG);
                camera.setParameters(parameters);
                cameraConfigured=true;
            }
        }
    }
}

其他两个函数如下。

private Camera.Size getOptimalPreviewSize(int width, int height) {
    Camera.Size result=null;
    Camera.Parameters p = camera.getParameters();
    for (Camera.Size size : p.getSupportedPreviewSizes()) {
        if (size.width<=width && size.height<=height) {
            if (result==null) {
                result=size;
            } else {
                int resultArea=result.width*result.height;
                int newArea=size.width*size.height;
                if (newArea>resultArea) {
                    result=size;
                }
            }
        }
    }
    return result;
}

private Camera.Size getSmallestPictureSize(Camera.Parameters parameters) {
    Camera.Size result=null;
    for (Camera.Size size : parameters.getSupportedPictureSizes()) {
        if (result == null) {
            result=size;
        }
        else {
            int resultArea=result.width * result.height;
            int newArea=size.width * size.height;
            if (newArea < resultArea) {
                result=size;
            }
        }
    }
    return(result);
}

那么,你认为是什么问题。为什么显示被拉伸?

4

0 回答 0