Android SurfaceView Preview Blurry

2019-04-08 20:13发布

问题:

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