Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fix Android Camera app aspect ratio and rotation

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");
}
}
like image 412
A.Jouni Avatar asked Jun 10 '13 21:06

A.Jouni


People also ask

How to fix screen rotation on Android devices?

There’s also a lock current bar that empowers you to fix the current rotation mode for all the apps. Rotation Manager is another app that manages your screen orientation. The main point of this app is to let you customize every little detail about the screen rotation of your mobile.

How to change aspect ratio of video on Android phone?

Video aspect ratio changer App allows you to change the video aspect ratio on your Phone with ease. In case you need to further edit your videos, such as adding text, music or other effects, a desktop application like Filmora Video Editor will help you to achieve this.

How to fix image rotated 90 degrees on Android?

When you capture an image on android using intentandroid.media.action.IMAGE_CAPTURE, it gets rotated 90 degrees on some devices.  Here is how to solve this problem with the code snippets. I have tested it on Android 4.1(Jelly Bean), Android 4.4(KitKat) and Android 5.0(Lollipop). Steps Scale down the image if it was bigger than 1024×1024.

How to fix rotated image to the right orientation?

Rotate the image to the right orientation onlyif it was rotated 90, 180 or 270 degrees. Recycle the rotated image for memory purposes. Here is the code part: Call the following method with the current Contextand the image URIthat you want to fix


1 Answers

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!

like image 111
lopez.mikhael Avatar answered Oct 07 '22 01:10

lopez.mikhael