Home » Android » android – How come that a camera preview in a textureview is much more fuzzy than in a surfaceview?

android – How come that a camera preview in a textureview is much more fuzzy than in a surfaceview?

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have found out that when using a textureview instead of a surfaceview as a camera preview (both hooked up to the camera via a mediarecorder) then the preview is much more fuzzy.

What I mean by fuzzy is that in a texture view you can see the pixels, especially when zooming. That is not the case when using a surfaceview. Why is that the case?

How to&Answers:

UPD:
Sorry,but after I re-write my shit code, the key is the preview size too small that caused “fuzziness”, so you should set a reasonable preview Size,not the reason strikeout below, but auto-focus is suggested …

Size size = getBestSupportSize(parameters.getSupportedPreviewSizes(), width, height);
parameters.setPreviewSize(size.width, size.height);

As to the method getBestSupportSize(), how to get the bestSize for your project needs, in this case, it is as large as the screen width andhe ratio is 4/3 your’s may be some other, I calculate the ration dividing width/height.

 private Size getBestSupportSize(List<Size> sizes, int width, int height) {
    Size bestsize = sizes.get(0);
    int screenWidth = getResources().getDisplayMetrics().widthPixels;
    int dt = Integer.MAX_VALUE;
    for (int i = sizes.size() - 1; i >= 0; i--) {
        Log.d(TAG, "-index : " + i);
        Size s = sizes.get(i);
        if (s.width * 3.0f / 4 == s.height) {
            int newDT = Math.abs(screenWidth - s.width);
            if (newDT < dt && screenWidth < s.width) {
                dt = newDT;
                bestsize = s;
            }
        }
    }
    return bestsize;//note that if no "4/3" size supported,default return size[0]
}

So this “fuzziness” was caused by a small previewSize calcualate a best size for the camera using this getSupportedPreviewSizes() method


And I will keep the autoFocus snippet below, strikeout though, FYR if is needed.



Well i got the solution for this “fuzzy” problem,and my case is just using TextureView andsurfaceTexture to take a pic instead of old surfaceView withsurfaceHolderway.

The key is set this mCamera.autofocus(), why the pic is”fuzzy” is bacause we lack of this autoFocus setting.
like below :

mCamera.setPreviewTexture(surface);

//enable autoFocus  if moving   
mCamera.setAutoFocusMoveCallback(new AutoFocusMoveCallback() {

    @Override
    public void onAutoFocusMoving(boolean start, Camera camera) {

        if (start) {  //true means you are moving the camera  

            mCamera.autoFocus(myAutoFocus);

        }
    }
});
mCamera.startPreview();

The autoFocusCallback like this:

AutoFocusCallback myAutoFocus = new AutoFocusCallback() {

    @Override
    public void onAutoFocus(boolean success, Camera camera) {

    }
};