Fix Android Camera app aspect ratio and rotation
Asked Answered
I

1

10

How it looks: http://i41.tinypic.com/30278m1.png

It looks like in the pic ,I want it to have a correct aspect ratio with correct rotaion

Take a look the code please , how can I fix it ?

This is my code:

    public class MainActivity extends Activity implements SurfaceHolder.Callback {

Camera mCamera;
SurfaceView mPreview; 

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mPreview = (SurfaceView)findViewById(R.id.preview);
    mPreview.getHolder().addCallback(this);
    mPreview.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    mCamera = Camera.open();
}   
@Override
public void onPause() {
    super.onPause();
    mCamera.stopPreview();
}    
@Override
public void onDestroy() {
    super.onDestroy();
    mCamera.release();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    Camera.Parameters params = mCamera.getParameters();
    List<Camera.Size> sizes = params.getSupportedPreviewSizes();
    Camera.Size selected = sizes.get(0);
    params.setPreviewSize(selected.width,selected.height);
    mCamera.setParameters(params);
    mCamera.startPreview();
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
    try {
        mCamera.setPreviewDisplay(mPreview.getHolder());
    } catch (Exception e) {
        e.printStackTrace();
    }
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    Log.i("PREVIEW","surfaceDestroyed");
}
}
Ironbound answered 10/6, 2013 at 21:53 Comment(0)
C
8

Change your surfaceChanged method with this :

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {       
    Parameters parameters = mCamera.getParameters();
    List<Camera.Size> previewSizes = parameters.getSupportedPreviewSizes();
    Camera.Size previewSize = previewSizes.get(4); //480h x 720w

    parameters.setPreviewSize(previewSize.width, previewSize.height);
    parameters.setFlashMode(Parameters.FLASH_MODE_AUTO);
    parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);

    mCamera.setParameters(parameters);

    Display display = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    if(display.getRotation() == Surface.ROTATION_0) {                        
        mCamera.setDisplayOrientation(90);
    } else if(display.getRotation() == Surface.ROTATION_270) {
        mCamera.setDisplayOrientation(180);
    }

    mCamera.startPreview();
}

I hope I have helped you!

Crud answered 10/6, 2013 at 22:27 Comment(4)
isPreviewRunning & getContext are marked as errors ! isPreviewRunning cannot be resolved to a variable & The method getContext() is undefinedIronbound
Ok, I just changed my answer.Crud
excellent work mikhael !! it worked , I took a look at your site you have done good projectsIronbound
Could you explain a the solution for the aspect ratio fix?Tutelary

© 2022 - 2024 — McMap. All rights reserved.