Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android camera preview: 1:1 aspect ratio, how?

Tags:

android

Hi I am trying to use the camera to capture an image in one of my application. What is special is that I need a square preview area (and picture in the end). I tried defining the size of both picture and preview to 1:1 pixel ratios, but nothing seams to work. No matter what I does the picture looks "squashed" on a square.

Anyone who has any idea about how to resolve this?

code:

public class AddFromCameraActivity extends Activity implements SurfaceHolder.Callback {
private Camera mCamera;
private Parameters mParameters; 
private SurfaceView mCameraPreview;
private SurfaceHolder mSurfaceHolder; 

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.addimagefromcameramain); 
    initialise(); 
    //Testing area
    mCamera = Camera.open(); 
    mParameters = mCamera.getParameters(); 
    mParameters.setFlashMode(Camera.Parameters.FLASH_MODE_AUTO); 
    mParameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO); 
    mParameters.setJpegQuality(50); 
    mParameters.setJpegThumbnailQuality(50); 
    mParameters.setPictureSize(1024, 1024);
    //mParameters.setPreviewFormat(ImageFormat.JPEG); 
    mParameters.setJpegThumbnailSize(256, 256); 
    mParameters.setPreviewSize(500, 500); 
    mCamera.setParameters(mParameters);
}
private void initialise()
{
    mCameraPreview = (SurfaceView)findViewById(R.id.cameraSurfaceView); 
    mSurfaceHolder = mCameraPreview.getHolder(); 
    mSurfaceHolder.addCallback(this); 
    mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 
    mSurfaceHolder.setFixedSize(500, 500); 
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {

}
@Override
public void surfaceCreated(SurfaceHolder holder) {
    try {
        mCamera.setPreviewDisplay(mSurfaceHolder);
        mCamera.startPreview(); 
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    mCamera.stopPreview(); 
    mCamera.release(); 
}
@Override
public void onPause()
{
    mCamera.release(); 
}
}

thanks

like image 497
Kenneth Brodersen Avatar asked May 04 '11 11:05

Kenneth Brodersen


People also ask

How do you find the aspect ratio of a 1 to 1?

A 1:1 ratio is a square. This means that an image's width and height are equal. Some common 1:1 ratios are an 8 x 8-inch photo, a 1080 x 1080-pixel image, or general social media profile picture templates.


1 Answers

You can take a look at the CameraPreview sample code from the Android SDK. The getOptimalPreviewSize method shows how to deal with different camera sizes and the onLayout method shows how to layout the preview surface in the activity.

like image 107
Laurent Etiemble Avatar answered Oct 21 '22 14:10

Laurent Etiemble