6

I am trying to put the camera in a surfaceView in portrait orientation on android phone. I used http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/CameraPreview.html to put the camera in the surfaceView. However the camera is rotated 90 degrees, so I tried doing setDisplayOrientation(90) to fix it, but this squashes the image (it Probably isn't resizing the surfaceView properly??). How can I make it so the camera is not squished?

user1154920
  • 465
  • 1
  • 6
  • 21

4 Answers4

6

You need to resize the image according to the rotation. See here for an example.

Community
  • 1
  • 1
John J Smith
  • 11,435
  • 9
  • 53
  • 72
  • What should height and width be in that example? I want The preview to be about half of the screen, but I don't want to hardcode values so it can work on different screen sizes – user1154920 Mar 06 '12 at 19:45
  • This is the SurfaceHolder callback method so the height and width parameters of the method will be set by the hardware. If you want your preview to be half of the screen, then simply set them in the 'if' statements like: parameters.setPreviewSize(width/2, height/2); – John J Smith Mar 06 '12 at 23:30
2

If you were using Android API demos, you need to modify OnLayout() function.

Basically the Android API demo, sets the preview size in according to the aspect ratio so that the preview image is squished and display in center of screen with small size in portrait mode.

Even if we set-up screen orientation to Portrait mode, preview width and height are not changed. That's why the preview image was squished.

t0mm13b
  • 34,087
  • 8
  • 78
  • 110
Teddydino
  • 21
  • 4
0

The only thing that worked for me is

In the code mCamera.setDisplayOrientation(90);

The layout has fill_parent for both height and width.

And manifest file has android:screenOrientation="portrait"

Siddharth
  • 9,349
  • 16
  • 86
  • 148
-2

use this function for resize image it will be useful to you

public Bitmap resize(Bitmap img,int Width,int Height){

    int width = img.getWidth();

    int height = img.getHeight();

    int newWidth = (int) Width;

    int newHeight = (int) Height;

    // calculate the scale - in this case = 0.4f
    float scaleWidth = ((float) newWidth) / width;

    float scaleHeight = ((float) newHeight) / height;

    // createa matrix for the manipulation
    Matrix matrix = new Matrix();

    // resize the bit map
    matrix.postScale(scaleWidth, scaleHeight);

    // rotate the Bitmap
    //matrix.postRotate(45);

    // recreate the new Bitmap
    Bitmap resizedBitmap = Bitmap.createBitmap(img, 0, 0, width, height, matrix, true);

    return resizedBitmap;

} 
Aman Alam
  • 11,231
  • 7
  • 46
  • 81
Ramkumar
  • 93
  • 5