IAmGroot
IAmGroot

Reputation: 13855

Android: Photos force captured in Landscape mode

Hi have built on Camera Preview.

Origionally it displayed a landscape preview even in portrait mode. I changed this with some extra code. However, no matter if your landscape/portrait, it always saves images in landscape mode.

Ive also now forced it to portrait mode:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

Basically any image I take comes out in landscape. (rotates a portrait taken image).

How would I make it save photos in the orientated mode, or locked to portrait assumption?

Ive considered taking the photo array and rotating 90 degrees. But that would require drawing the image to a bitmap, rotating and then storing back to an array. Overkill much? Unless you can rotate an image array directly?

Upvotes: 1

Views: 5041

Answers (1)

Venky
Venky

Reputation: 11107

First Check the Camera Orientation using below Snippet :

    private int lookupRotation() {       
       WindowManager mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
       Display mDisplay = mWindowManager.getDefaultDisplay();
       int rotation = mDisplay.getRotation();
       Log.v(LOG_TAG, "rotation: " + rotation);
       return rotation;
   }

then check your desired rotation using and set your orientation :

if (rotation == Surface.ROTATION_0) {
    int degreesRotate = 90;
}

Resize the Bitmap and rotate your Bitmap based on orientation using below Snippet :

    private Bitmap createBitmap(byte[] imageData, int maxWidth, int maxHeight,
        int rotationDegrees) throws FileNotFoundException {

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 2;
    options.inDensity = 240;
    int imageWidth = 0;
    int imageHeight = 0;

    Bitmap image = BitmapFactory.decodeByteArray(imageData, 0,
            imageData.length, options);

    imageWidth = image.getWidth();
    imageHeight = image.getHeight();

    if (imageWidth > maxWidth || imageHeight > maxHeight) {

        double imageAspect = (double) imageWidth / imageHeight;
        double desiredAspect = (double) maxWidth / maxHeight;
        double scaleFactor;

        if (imageAspect < desiredAspect) {
            scaleFactor = (double) maxHeight / imageHeight;
        } else {
            scaleFactor = (double) maxWidth / imageWidth;
        }

        float scaleWidth = ((float) scaleFactor) * imageWidth;
        float scaleHeight = ((float) scaleFactor) * imageHeight;

        Bitmap scaledBitmap = Bitmap.createScaledBitmap(image,
                (int) scaleWidth, (int) scaleHeight, true);
        image = scaledBitmap;
    }

    if (rotationDegrees != 0) {

        int w = image.getWidth();
        int h = image.getHeight();
        mtx.postRotate(rotationDegrees);
        Bitmap rotatedBMP = Bitmap.createBitmap(image, 0, 0, w, h, mtx,
                true);
        image = rotatedBMP;
    }

    return image;
}

The above method will returns bitmap based on Orientation.

Upvotes: 9

Related Questions