Question

I made a custom camera application, and I want to display pictures on ImageView. I launched my program on 3 different phones (HTC Desire Z, Galaxy S4, and Galaxy S2). On the HTC, Orientation of the picture is respected in ImageView, but not with the Galaxys.

My program works like this :

-> I take a picture with the custom camera app

-> I set the EXIF flags of the picture, depending on phone orientation (OrientationEventListener) :

switch(getOrientation(rot)){
                        case VERTICAL_ENDROIT : params.setRotation(90); break;
                        case HORIZONTAL_ENDROIT : params.setRotation(0); break;
                        case VERTICAL_ENVERS : params.setRotation(270); break;
                        case HORIZONTAL_ENVERS : params.setRotation(180); break;
                    }
camera.setParameters(params); 

This function is working well on all phones, they all detected and assign the right orientation.

-> I save it to JPG format

-> I send the picture to the ImageView

Bitmap bm2 = BitmapFactory.decodeFile(photoItem.getPathPhoto(),options);
imageView.setImageBitmap(bm2);

With the Galaxy, Orientation on ImageView is always the same, corresponding to params.setRotation(0). No problem with the HTC.

So i tried to look at the exif Flag of the picture, just before sending it to the ImageView :

int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1);

The weird thing is that with HTC, orientation is always to 0 value, and with Samsung orientation got the right values (1 3 6 8).

Conclusion : With Galaxy Phones EXIF orientation values are OK, but orientation on ImageView isnt good.

With HTC Phones EXIF orientation values are false, but orientation is OK. (wtf ?)

Thanks for the answers :)

PS : I dont want to use Matrix or Image Processing stuff because Im very low in CPU/memory ressources

Was it helpful?

Solution 3

Well I found why orientation wasnt the same with Galaxy and HTC. HTC doesnt use EXIF flags to orient the picture. The picture is natively saved with the right orientation.

OTHER TIPS

try {
        final File f = new File(directory, name);

        ExifInterface ei = new ExifInterface(f.getAbsolutePath());
        int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_NORMAL);

        try {
            switch (orientation) {
            case ExifInterface.ORIENTATION_ROTATE_90:
                b = RotateBitmap(b, 90);
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                b = RotateBitmap(b, 180);
                break;
            case ExifInterface.ORIENTATION_ROTATE_270:
                b = RotateBitmap(b, 270);
                break;
            // etc.
            }
        } catch (OutOfMemoryError err) {

        } catch (Exception e) {

        }

        return b;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

public static Bitmap RotateBitmap(Bitmap source, float angle) {
    Matrix matrix = new Matrix();
    matrix.postRotate(angle);
    source = Bitmap.createBitmap(source, 0, 0, source.getWidth(),
            source.getHeight(), matrix, true);
    if (source != null) {
        b = null;
    }
    return source;
}

This code will do the functionality that u required.

Try the below code

 try {
    File f = new File(imagePath);
    ExifInterface exif = new ExifInterface(f.getPath());
    int orientation = exif.getAttributeInt(
            ExifInterface.TAG_ORIENTATION,
            ExifInterface.ORIENTATION_NORMAL);
    int angle = 0;

    if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
        angle = 90;
    } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
        angle = 180;
    } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
        angle = 270;
    }

    Matrix mat = new Matrix();
    mat.postRotate(angle);
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 2;

    Bitmap bmp = BitmapFactory.decodeStream(new FileInputStream(f),
            null, options);
    bitmap = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(),
            bmp.getHeight(), mat, true);
    ByteArrayOutputStream outstudentstreamOutputStream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100,
            outstudentstreamOutputStream);
    imageView.setImageBitmap(bitmap);

} catch (IOException e) {
    Log.w("TAG", "-- Error in setting image");
} catch (OutOfMemoryError oom) {
    Log.w("TAG", "-- OOM Error in setting image");
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top