2

I have added FrameLayout which display camera preview in it, but the preview is getting stretched.

I have tried solution given in https://stackoverflow.com/a/19592492/1597844 but that does not help either.

Can you help know what is wrong in my code?

Output that I am getting is Upper Image gets skewed horizontally (portrait mode) and the below one gets stretched vertically (Landscape mode) Upper Image gets skewed horizontally (portrait mode) and the below one gets stretched vertically (Landscape mode) Code that I am using is

mCamera = Camera.open(findBackFacingCamera());
        Camera.Parameters parameters = mCamera.getParameters();
        parameters.set("jpeg-quality", 100);
        parameters.setPictureFormat(PixelFormat.JPEG);
        List<Size> sizes = parameters.getSupportedPreviewSizes();
        Size optimalSize = getOptimalPreviewSize(sizes, getResources().getDisplayMetrics().widthPixels, getResources().getDisplayMetrics().heightPixels);
        parameters.setPreviewSize(optimalSize.width, optimalSize.height);
        mCamera.setParameters(parameters);

        mPicture = getPictureCallback();
        mPreview = new CameraPreview(myContext, mCamera);
        backCameraPreview.removeAllViews();
        backCameraPreview.addView(mPreview);

Method for getting optimal preview size

private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) {
    final double ASPECT_TOLERANCE = 0.05;
    double targetRatio = (double) w / h;

    if (sizes == null) return null;
    Size optimalSize = null;
    double minDiff = Double.MAX_VALUE;
    int targetHeight = h;
    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);
        }
    }
    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;
}

My xml looks like this

 <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="2">

    <FrameLayout
        android:id="@+id/dual_camera_capture_activity_ll_back_view"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@android:color/white"
        android:orientation="horizontal"
        android:padding="10dp" />

    <FrameLayout
        android:id="@+id/dual_camera_capture_activity_fl_front_view"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_weight="1"
        android:background="@android:color/white"
        android:orientation="horizontal"
        android:padding="10dp" />
</LinearLayout>

This is my CameraPreview class that is SurfaceView

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private final Context mContext;
private SurfaceHolder mHolder;
private Camera mCamera;
private static String TAG = "Dual Camera";
private float mDist;


public CameraPreview(Context context, Camera camera) {
    super(context);
    mContext = context;
    mCamera = camera;
    mHolder = getHolder();
    mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    mHolder.addCallback(this);
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    Log.d(TAG, "surface created");
    // The Surface has been created, now tell the camera where to draw the
    // preview.
    try {
        mCamera.setPreviewDisplay(holder);
        mCamera.setDisplayOrientation(90);
        mCamera.startPreview();
    } catch (IOException e) {
        Log.d(TAG, "Error setting camera preview: " + e.getMessage());
    }
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    Log.d(TAG, "surface destroyed");
    this.getHolder().removeCallback(this);
}

public void refreshCamera(Camera camera) {
    setCamera(camera);
    if (mCamera != null) {
        Display display = ((Activity) mContext).getWindowManager().getDefaultDisplay();
        if (mHolder.getSurface() == null) {
            return;
        }
        try {
            mCamera.stopPreview();
        } catch (Exception e) {
            e.printStackTrace();
        }
        Camera.Parameters parameters = mCamera.getParameters();
        if (display.getRotation() == Surface.ROTATION_0) {
            mCamera.setDisplayOrientation(90);
        }
        if (display.getRotation() == Surface.ROTATION_90) {
        }
        if (display.getRotation() == Surface.ROTATION_180) {
        }

        if (display.getRotation() == Surface.ROTATION_270) {
        }
        mCamera.setParameters(parameters);
        try {
            mCamera.setPreviewDisplay(mHolder);
            mCamera.startPreview();
        } catch (Exception e) {
            Log.d(VIEW_LOG_TAG, "Error starting camera preview: " + e.getMessage());
        }
    }
}

public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    refreshCamera(mCamera);
}

public void setCamera(Camera camera) {
    mCamera = camera;
}

}

Can you tell me what I am doing wrong?

Community
  • 1
  • 1
Roadies
  • 3,309
  • 2
  • 30
  • 46
  • How do I match my preview size and layout size? – Roadies Dec 26 '14 at 06:32
  • I want to achive like this app. https://play.google.com/store/apps/details?id=com.checkthis.frontback – Roadies Dec 26 '14 at 06:47
  • Some examples in Grafika (https://github.com/google/grafika); note the use of `AspectFrameLayout`. – fadden Dec 26 '14 at 17:09
  • Any updates @Roadies ?? actually I faces same issues . I'm setting 3 camera box in one screen using surfaceView and surfaceHolder .plz share any updated information if available . – Radhey Apr 24 '15 at 12:14
  • The question is old, but I comment because somebody can get help [here](http://stackoverflow.com/questions/19577299/android-camera-preview-stretched), to me was very useful the answer of @Hesam. – Crash Mar 15 '16 at 17:19
  • did you found the solution? – prasanthMurugan Jul 11 '17 at 09:54

1 Answers1

-1

This is because of less resolution of preview size

Set preview size hard code parameters.setPreviewSize(1920, 720);

Amit Thaper
  • 2,117
  • 4
  • 26
  • 49