Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Camera preview is sideways

People also ask

How do I change camera preview orientation on Android?

To force portrait orientation: set android:screenOrientation="portrait" in your AndroidManifest. xml and call camera. setDisplayOrientation(90); before calling camera.

Why is my camera showing sideways?

The reason your photo would appear this way is because the photo was taken that way (either with the phone sideways or upside down) and the image file itself is in this orientation. For example, if you hold your phone upright and take a photo, the photo is saved in portrait mode or "sideways".

What is a camera preview screen?

PreviewView is a custom View that enables the display of a camera feed. It was built to offload the burden of setting up and handling the preview surface used by the camera.


This issue appeared to start out as a bug with certain hardware see here but can be overcome by using the call to mCamera.setDisplayOrientation(degrees) available in API 8. So this is how I implement it:

public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {            
    if (isPreviewRunning) {
        mCamera.stopPreview();
    }

    Parameters parameters = mCamera.getParameters();
    Display display = ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay();

    if(display.getRotation() == Surface.ROTATION_0) {
        parameters.setPreviewSize(height, width);                           
        mCamera.setDisplayOrientation(90);
    }

    if(display.getRotation() == Surface.ROTATION_90) {
        parameters.setPreviewSize(width, height);                           
    }

    if(display.getRotation() == Surface.ROTATION_180) {
        parameters.setPreviewSize(height, width);               
    }

    if(display.getRotation() == Surface.ROTATION_270) {
        parameters.setPreviewSize(width, height);
        mCamera.setDisplayOrientation(180);
    }

    mCamera.setParameters(parameters);
    previewCamera();                      
}

And the previewCamera method :

public void previewCamera() {        
    try {           
        mCamera.setPreviewDisplay(mSurfaceHolder);          
        mCamera.startPreview();
        isPreviewRunning = true;
    } catch(Exception e) {
        Log.d(APP_CLASS, "Cannot start preview", e);    
    }
}

This was on an HTC Desire and I had to initially put in logging statements in each of the rotation checks to say what the rotation was and then debugged on the device and watched the logCat output while I rotated the device. For the HTC Desire, 0 was the phone as you would have expected (portrait), 90 degrees was turning the phone 90 degrees COUNTER-CLOCKWISE (I had assumed it would have been clockwise). In the code you'll see I didn't need to do any display rotation when the phone was at 90 or 180 degrees - the device seemed to handle this itself. Only one point not working properly: The 270 degree rotation is when you turn the device 90 degrees clockwise and the display rotation counters that ok but if you rotate the device 270 degrees counter-clockwise, it doesn't appear to compensate it properly.

P.S. Note the swapover of width and height in the appropriate rotations.


try to set the display orientation. It solves my problem.

 mCamera.setDisplayOrientation(90);

 public void surfaceCreated(SurfaceHolder holder) {
     mCamera = Camera.open();
     mCamera.setDisplayOrientation(90);
     try {
         mCamera.setPreviewDisplay(holder);
         mCamera.setPreviewCallback(new PreviewCallback() {

             @Override
             public void onPreviewFrame(byte[] data, Camera camera) {
             }
         });

     } catch (Exception e) {
         e.printStackTrace();
     }
}

try this code


I did it taking advice of mCamera.setDisplayOrientation(90); but also rotated the bitmap because for some reason the others approaches doesnt work for me in version 2.3.3.

For rotate the bitmap i did this:

Matrix matrix = new Matrix();
matrix.postRotate(90);
imageView1 = new ImageView(this);
Bitmap bitmap = BitmapFactory.decodeFile(files[i].getAbsolutePath());
Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap , 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
Bitmap scaledBitmap = Bitmap.createScaledBitmap(rotatedBitmap, 80, 80, true);
imageView1.setImageBitmap(scaledBitmap);