I'm writing an Android application that uses the camera. I'm setting camera display orientation to 90, my activity is in a portrait orientation:
camera.setDisplayOrientation(90);
I'm getting a well oriented preview picture, but the resulting image is rotated to -90 degrees (counter clockwise) and
exif.getAttribute(ExifInterface.TAG_ORIENTATION)
returns ORIENTATION_NORMAL
Is it expected behavior? Should I rotate resulted image after the capture?
Device - Nexus S, API - 10
Find and turn on the "Auto-rotate" tile in the quick-setting panel. You can also go to Settings > Display > Auto-rotate screen to turn it on. Your phone screen should rotate automatically now if nothing is wrong with the sensors.
Answer: Most phone cameras are landscape, meaning if you take the photo in portrait, the resulting photos will be rotated 90 degrees. In this case, the camera software should populate the Exif data with the orientation that the photo should be viewed in.
EXIF (Exchangeable Image File Format) data provides supplemental metadata, such as the date, time, camera settings, and image orientation. If an image contains the wrong image orientation EXIF data, or if that data is stripped in the upload process for whatever reason, it will display as rotated.
Try this
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"); }
It will work
Problem is the camera orientation is a complete disaster (as is capturing an image) because OEMs do not adhere to the standard. HTC phones do things one way, Samsung phones do it a different way, the Nexus line seems to adhere no matter which vendor, CM7 based ROMs I think follow the standard no matter which hardware, but you get the idea. You sort of have to determine what to do based on the phone/ROM. See discussion here: Android camera unexplainable rotation on capture for some devices (not in EXIF)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With