2

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 ?

giozh
  • 9,868
  • 30
  • 102
  • 183

3 Answers3

1

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();

}
PEHLAJ
  • 9,980
  • 9
  • 41
  • 53
Pir Fahim Shah
  • 10,505
  • 1
  • 82
  • 81
0

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)

Hari_krish4
  • 149
  • 7
  • rotation on my device is locked, and also with your code, picture is always saved rotate. But with setDisplayOrientation, i just set orientation of camera preview on surfaceView – giozh Apr 09 '14 at 14:56
0

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.

Community
  • 1
  • 1
Alex Cohn
  • 56,089
  • 9
  • 113
  • 307