Android SurfaceView Preview Blurry

天涯浪子 提交于 2019-12-09 18:00:59

问题


I have a quick question. I'm using Android's SurfaceView to take a picture and save it. However, the preview size and the picture quality itself is both terrible; as in, it is very blurry. There's no sharpness to the picture quality at all.

Here's where I initialize my surfaceView:

                    camera.setDisplayOrientation(90);
                    Parameters parameters = camera.getParameters();
                    parameters.setWhiteBalance(Camera.Parameters.WHITE_BALANCE_AUTO);
                    parameters.setExposureCompensation(0);
                    parameters.setPictureFormat(ImageFormat.JPEG);
                    parameters.setJpegQuality(100);

                    Camera.Size picSize =   getOptimalPreviewSize(parameters.getSupportedPreviewSizes(),
                            getResources().getDisplayMetrics().widthPixels, 
                            getResources().getDisplayMetrics().heightPixels);

                    parameters.setPictureSize(picSize.width, picSize.height);
                    parameters.setPreviewSize(picSize.width, picSize.height);

                    camera.setParameters(parameters);
                    camera.setPreviewDisplay(holder);
                    camera.startPreview();

The getOptimalPreviewSize() returns the best size available from parameters.getSupportedPreviewSizes(). The size that it returns is 1280x720, the best size that my phone will support. However, the surfaceView is still very blurry. Is there anything obviously wrong that I'm doing, or is there a way to optimize the surfaceView? Thanks.


回答1:


There are two reason why your image and preview may appear blurry

1) the way you are settings the picture size and preview size is wrong. You have to query the supported sizes and decide which is the best size for you and set size from the list that your have got. You cannot give arbit values. Check this sample app for implementation details - https://github.com/josnidhin/Android-Camera-Example

2) you have to put your camera in auto foucs mode so that it will focus automatically. (better is to implement a touch to focus with a proper ui). Once your camera starts just set the below

private void setCamFocusMode(){

    if(null == mCamera) {
        return;
     }

    /* Set Auto focus */ 
    Parameters parameters = mCamera.getParameters();
    List<String>    focusModes = parameters.getSupportedFocusModes();
    if(focusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)){
        parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);   
    } else 
    if(focusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO)){
        parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
    }   

    mCamera.setParameters(parameters);
}

make sure your have the proper permission in your manifest

Hope this helps

Regards, Shrish



来源:https://stackoverflow.com/questions/27021347/android-surfaceview-preview-blurry

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!