Android相机显示效果日益紧张

时间:2012-04-20 06:06:10

标签: android android-layout android-camera

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

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

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

0 个答案:

没有答案