giozh
giozh

Reputation: 10068

Camera on SurfaceView: set orientation of picture taken

this is mt code for take picture in surfaceView:

@Override
public void surfaceCreated(SurfaceHolder holder) {
    mCamera = Camera.open();

    try {
        mCamera.setPreviewDisplay(holder);
        mCamera.setDisplayOrientation(90);


    } catch (IOException exception) {
        mCamera.release();
        mCamera = null;
    }

}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {
    Camera.Parameters params = mCamera.getParameters();
    Camera.Size result = getBestPreviewSize(params, width, height);
    params.setPreviewSize(result.width, result.height);

    params.setPictureFormat(ImageFormat.JPEG);
    params.setJpegQuality(100);
    mCamera.setParameters(params);

    mCamera.startPreview();

}

the preview of picture is in portrait mode, but picture is saved on my storage rotated. How can i save picture with the same orientation of preview ?

Upvotes: 2

Views: 1309

Answers (3)

Pir Fahim Shah
Pir Fahim Shah

Reputation: 10633

I had the same problem but i solve it while putting a single line of code in surfaceChanged method.

params.setRotation(90);

change your code to this:

public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {
    Camera.Parameters params = mCamera.getParameters();
    Camera.Size result = getBestPreviewSize(params, width, height);
    params.setPreviewSize(result.width, result.height);

    params.setRotation(90)  ***//Just add this single line of code***

    params.setPictureFormat(ImageFormat.JPEG);
    params.setJpegQuality(100);
    mCamera.setParameters(params);

    mCamera.startPreview();

}

Upvotes: 1

Alex Cohn
Alex Cohn

Reputation: 57203

It is explicitly written in docs for setRotation() that the system may choose to only set an EXIF flag that the image was rotated. Actually, on many devices, e.g. Samsung this is exactly what happens.

You can use the Android port of a Java open source library which provides a class for lossless Jpeg rotation.

Upvotes: 0

Hari_krish4
Hari_krish4

Reputation: 149

In your Activity declare rotation as a static int variable

rotation = getWindowManager().getDefaultDisplay().getRotation();

Then add this lines in camera preview class

 if(YourActivityname.rotation == 0 || YourActivityname.rotation == 180)
        this.mCamera.setDisplayOrientation(90);
   else
        this.mCamera.setDisplayOrientation(0);

Refer this http://developer.android.com/reference/android/hardware/Camera.Parameters.html#setRotation%28int%29

public void setRotation (int rotation)

Upvotes: 0

Related Questions