2

I have a problem with my application, I have a Camera Preview inside a ViewPager but when I try to swipe from both sides it's very laggy and sometime when I swipe quicky there is a little black space between the other fragment and my camera preview.

My camera preview class (from a tutorial) :

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {

private final String TAG = "Camera Preview";

private SurfaceHolder mHolder;
private Camera mCamera;
Size mPreviewSize;
List<Size> mSupportedPreviewSizes;

public CameraPreview(Context context, Camera camera) {
    super(context);
    mCamera = camera;
    mSupportedPreviewSizes = mCamera.getParameters().getSupportedPreviewSizes();
    mHolder = getHolder(); // Install a SurfaceHolder.Callback so we get notified when the underlying surface is created and destroyed.
    mHolder.addCallback(this);
}

public void surfaceCreated(SurfaceHolder holder) {
    try {
        if (mCamera != null) {
            mCamera.setPreviewDisplay(holder);
            mCamera.startPreview();
        }
    } catch (IOException exception) {
        Log.e(TAG, "IOException caused by setPreviewDisplay()", exception);
    }
}

public void surfaceDestroyed(SurfaceHolder holder) {
   mCamera.stopPreview();
}

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
    if (mHolder.getSurface() == null) {
        return;
    }

    try {
        mCamera.stopPreview();
    } catch (Exception e) {
    }

    try {

        Parameters params = mCamera.getParameters();
        List<String> focusModes = params.getSupportedFocusModes();
        if (focusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO)) {
            params.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
        }
        params.setPreviewSize(mPreviewSize.width, mPreviewSize.height);
        mCamera.setParameters(params);
        mCamera.setPreviewDisplay(mHolder);
        mCamera.startPreview();

    } catch (Exception e) {
        Log.d(TAG, "Error starting camera preview: " + e.getMessage());
    }

}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

    final int width = resolveSize(getSuggestedMinimumWidth(), widthMeasureSpec);
    final int height = resolveSize(getSuggestedMinimumHeight(), heightMeasureSpec);

    setMeasuredDimension(width, height);

    if (mSupportedPreviewSizes != null) {
        mPreviewSize =
                getOptimalPreviewSize(mSupportedPreviewSizes, width, height);
    }
}

private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) {

    final double ASPECT_TOLERANCE = 0.1;
    double targetRatio = (double) h / w;

    if (sizes == null)
        return null;

    Size optimalSize = null;
    double minDiff = Double.MAX_VALUE;

    int targetHeight = h;

    // find an size match aspect ratio and size
    for (Size size : sizes) {
        double ratio = (double) size.width / size.height;
        if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE)
            continue;
        if (Math.abs(size.height - targetHeight) < minDiff) {
            optimalSize = size;
            minDiff = Math.abs(size.height - targetHeight);
        }
    }

    // Cannot find the one match the aspect ratio, ignore the requirement
    if (optimalSize == null) {
        minDiff = Double.MAX_VALUE;
        for (Size size : sizes) {
            if (Math.abs(size.height - targetHeight) < minDiff) {
                optimalSize = size;
                minDiff = Math.abs(size.height - targetHeight);
            }
        }
    }
    return optimalSize;
}
    }

Do you know how I can do a smooth camera preview in a ViewPager ?

Like Snapchat it would be awesome !

ChargerDukes
  • 373
  • 1
  • 4
  • 9

2 Answers2

0

You can try this for better reference http://opencamera.sourceforge.net/

user1592261
  • 97
  • 1
  • 1
  • 6
0

I spent a lot of time and coffee, but found the cause of the trouble. The problem in using SurfaceView to display a preview.

Use TextureView to display a preview.

It will be useful: How can I make my view pager more smooth?

Good luck!

Community
  • 1
  • 1
Qulery
  • 193
  • 4
  • 13