Using Android Camera API, snapped photo's orientation is always Undefined - android

I use the camera api and the photo taken is always rotated by 90 degree, and i would like to rotate it.
So first of all i would like to know the picture's orientation and this point im stuck.
I always getting UNDEFINDED orientation in both ways.
Here is the code:
#Override
public void onPictureTaken(byte[] data, Camera camera) {
//Bitmap Options for lowering quality the bitmap to save memory
Options options = new BitmapFactory.Options();
options.inSampleSize = 4;
options.inPreferredConfig = Bitmap.Config.RGB_565;
//Make the bitmap
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, options);
//Making the path, the root will be fine for tests
String path = Environment.getExternalStorageDirectory().toString();
//output stream
OutputStream outputStream = null;
//Making the file as a jpg
File file = new File(path, "tmp_pic" + ".jpg"); // the File to save to
try {
//Writing the file
outputStream = new FileOutputStream(file);
outputStream.flush();
outputStream.close(); // do not forget to close the stream
//Getting the orientation in both possible ways
int ori = getOrientationFromExif(file.getPath());
int ori2 = getOrientationFromUri(Uri.fromFile(file));
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
if (bitmap == null) {
Toast.makeText(getApplicationContext(), "Error: Cant make photo.", Toast.LENGTH_SHORT).show();
}
else {
PhotoTapView.photoViews.get(index).setPhotoImage(bitmap);
finish();
}
cameraObject.release();
}
The functions for orientation:
//Getting orientation from file URI
private int getOrientationFromUri(Uri imageUri) {
String[] orientationColumn = { MediaStore.Images.Media.ORIENTATION };
Cursor cur = getContentResolver().query(imageUri, orientationColumn, null, null, null);
int orientation = -1;
if (cur != null && cur.moveToFirst()) {
orientation = cur.getInt(cur.getColumnIndex(orientationColumn[0]));
}
Log.i("Orientation from Uri", orientation + "");
return orientation;
}
//Getting orientation from ExifInterface
private static int getOrientationFromExif(String imagePath) {
int orientation = -1;
try {
ExifInterface exif = new ExifInterface(imagePath);
int exifOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
Log.i("Orientation from Exif: ", exifOrientation + "");
switch (exifOrientation) {
case ExifInterface.ORIENTATION_ROTATE_270:
orientation = 270;
Log.i("Orientation from Exif", "270");
break;
case ExifInterface.ORIENTATION_ROTATE_180:
orientation = 180;
Log.i("Orientation from Exif", "180");
break;
case ExifInterface.ORIENTATION_ROTATE_90:
Log.i("Orientation from Exif", "90");
orientation = 90;
break;
case ExifInterface.ORIENTATION_NORMAL:
orientation = 0;
Log.i("Orientation from Exif", "0 - Normal");
break;
case ExifInterface.ORIENTATION_UNDEFINED:
orientation = -1;
Log.e("Orientation from Exif", "UNDEFINED");
}
}
catch (IOException e) {}
return orientation;
}
Log output:
01-14 19:46:09.468: E/Orientation from Exif(12411): UNDEFINED
01-14 19:46:09.468: I/Orientation from Uri(12411): -1
What could be the problem?

I have been decoding/observing Android JPEG images since early 2011 because I published an image viewing application. In the 'early' days, the images were encoded in the sensor's native orientation and the actual orientation of the device was written into the EXIF metadata. Starting about 2 years ago, I noticed that the orientation was no longer being written into the EXIF data and instead, the camera app was rotating the image pixels before encoding the JPEG files. My guess is that this occurred because some photo viewers (* cough * Windows * cough *) ignore the EXIF orientation when displaying JPEG files and instead of waiting for Microsoft to fix it and blaming Android for doing something wrong, they decided to make use of the faster CPU/memory and just rotate the image.
The result is that without knowing the EXIF orientation, one can only determine that a photo was captured in landscape or portrait orientation. This bit of info is only an assumption because the majority of camera sensors are wider than they are tall.

In this code
outputStream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, outputStream); // Add this line
outputStream.flush();
outputStream.close(); // do not forget to close the stream
You forgot to actually write the bytes to the output stream. So, the file is 0 length and naturally doesn't contain any EXIF information.
Also be careful as you are calling decodeByteArray and then saving it out again, this will also lose the EXIF information! You need to parse the EXIF from the original image bytes.

Related

Using exif to rotate an image

i was following this link Android get Orientation of a camera Bitmap? And rotate back -90 degrees to rotate my image if/when necessary, but its not working for me, i get this error
05-28 23:29:30.049 9735-9735/ss.sealstudios.com.socialstories E/JHEAD: can't open '/document/508'
but checking it like below looks like there pointing in the right place
05-28 23:29:30.049 9735-9735/ss.sealstudios.com.socialstories I/System.out: bitmap uri content://com.android.providers.downloads.documents/document/508 0
//the 0 on the end is me checking for the rotation
i was wondering if this was build version specific like getting the real path from a URI as running this code on my marshmallow device gives me an entirely different result (this result, ie: error codes, is from a kitkat device), but this has defeated me for some weeks now following all sorts of answers to no avail, can anyone weigh in and help me please, here's what I'm trying to do
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data
!= null
&& data.getData() != null)
{
Uri uri = data.getData();
try {
BitmapFactory.Options bitmapOptions = new
BitmapFactory.Options();
bitmapOptions.inSampleSize = (int) 4;
InputStream inputStream =
getContentResolver().openInputStream(uri);
Bitmap scaledBitmap = BitmapFactory.decodeStream(inputStream,
null, bitmapOptions);
ExifInterface exif = new ExifInterface(uri.getPath());
int rotation =
exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
int rotationInDegrees = exifToDegrees(rotation);
System.out.println("bitmap uri " + uri + " " + rotation);
Matrix matrix = new Matrix();
if (rotation != 0f){
matrix.preRotate(rotationInDegrees);
Bitmap.createBitmap(scaledBitmap, 0,
0,scaledBitmap.getWidth(),scaledBitmap.getHeight(),
matrix, true);
imageView.setImageBitmap(scaledBitmap);
}else
imageView.setImageBitmap(scaledBitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static int exifToDegrees(int exifOrientation) {
if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) {
return 90;
}
else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) {
return 180; }
else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) {
return 270; }
return 0;
}
many thanks
ExifInterface exif = new ExifInterface(uri.getPath());
This will not work. If you call getPath() on a Uri, you are doing it wrong, as that is meaningless for most Uri values, particularly those with a content scheme, as your has.
Since the SDK's ExifInterface only works with local files, you will need other EXIF code. I use some code from the AOSP for this. That version of ExifInterface can work with an InputStream, so you can get a fresh InputStream for the content identified by the Uri and pass that to the alternative ExifInterface.

How to rotate a JPEG file on Android without losing quality and gaining file size?

Background
I need to rotate images taken by the camera so that they will always have a normal orientation.
for this, I use the next code (used this post to get the image orientation)
//<= get the angle of the image , and decode the image from the file
final Matrix matrix = new Matrix();
//<= prepare the matrix based on the EXIF data (based on https://gist.github.com/9re/1990019 )
final Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(),matrix,false);
bitmap.recycle();
fileOutputStream = new FileOutputStream(tempFilePath);
rotatedBitmap.compress(CompressFormat.JPEG, 100, fileOutputStream);
rotatedBitmap.recycle();
here the compression rate (AKA "quality" parameter) is 100.
The problem
The code works fine, but the result is larger than the original, much much larger.
The original file is around 600-700 KB, while the resulting file is around 3MB ...
This is even though both the input file and the output file are of the same format (JPEG).
The camera settings are at "super fine" quality. not sure what it means, but I think it has something to do with the compression ratio.
What I've tried
I've tried to set the "filter" parameter to either false or true. both resulted in large files.
Even without the rotation itself (just decode and encode), I get much larger files sizes...
Only when I've set compression ratio to around 85, I get similar files sizes, but I wonder how the quality is affected compared to the original files.
The question
Why does it occur?
Is there a way to get the exact same size and quality of the input file ?
Will using the same compression rate as the original file make it happen? Is it even possible to get the compression rate of the original file?
What does it mean to have a 100% compression rate ?
EDIT: I've found this link talking about rotation of JPEG files without losing the quality and file size , but is there a solution for it on Android ?
Here's another link that says it's possible, but I couldn't find any library that allows rotation of jpeg files without losing their quality
I tried two methods but I found out those methods take too long in my case. I still share what I used.
Method 1: LLJTran for Android
Get the LLJTran from here:
https://github.com/bkhall/AndroidMediaUtil
The code:
public static boolean rotateJpegFileBaseOnExifWithLLJTran(File imageFile, File outFile){
try {
int operation = 0;
int degree = getExifRotateDegree(imageFile.getAbsolutePath());
//int degree = 90;
switch(degree){
case 90:operation = LLJTran.ROT_90;break;
case 180:operation = LLJTran.ROT_180;break;
case 270:operation = LLJTran.ROT_270;break;
}
if (operation == 0){
Log.d(TAG, "Image orientation is already correct");
return false;
}
OutputStream output = null;
LLJTran llj = null;
try {
// Transform image
llj = new LLJTran(imageFile);
llj.read(LLJTran.READ_ALL, false); //don't know why setting second param to true will throw exception...
llj.transform(operation, LLJTran.OPT_DEFAULTS
| LLJTran.OPT_XFORM_ORIENTATION);
// write out file
output = new BufferedOutputStream(new FileOutputStream(outFile));
llj.save(output, LLJTran.OPT_WRITE_ALL);
return true;
} catch(Exception e){
e.printStackTrace();
return false;
}finally {
if(output != null)output.close();
if(llj != null)llj.freeMemory();
}
} catch (Exception e) {
// Unable to rotate image based on EXIF data
e.printStackTrace();
return false;
}
}
public static int getExifRotateDegree(String imagePath){
try {
ExifInterface exif;
exif = new ExifInterface(imagePath);
String orientstring = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
int orientation = orientstring != null ? Integer.parseInt(orientstring) : ExifInterface.ORIENTATION_NORMAL;
if(orientation == ExifInterface.ORIENTATION_ROTATE_90)
return 90;
if(orientation == ExifInterface.ORIENTATION_ROTATE_180)
return 180;
if(orientation == ExifInterface.ORIENTATION_ROTATE_270)
return 270;
} catch (IOException e) {
e.printStackTrace();
}
return 0;
}
Method 2: Using libjepg-turbo's jpegtran executable
1 Follow the step describe here:
https://stackoverflow.com/a/12296343/1099884
Except that you don't need obj/local/armeabi/libjpeg.a on ndk-build because I only want the jpegtran executable but not mess with JNI with libjepg.a .
2 Place the jpegtran executable on asset folder.
The code:
public static boolean rotateJpegFileBaseOnExifWithJpegTran(Context context, File imageFile, File outFile){
try {
int operation = 0;
int degree = getExifRotateDegree(imageFile.getAbsolutePath());
//int degree = 90;
String exe = prepareJpegTranExe(context);
//chmod ,otherwise premission denied
boolean ret = runCommand("chmod 777 "+exe);
if(ret == false){
Log.d(TAG, "chmod jpegTran failed");
return false;
}
//rotate the jpeg with jpegtran
ret = runCommand(exe+
" -rotate "+degree+" -outfile "+outFile.getAbsolutePath()+" "+imageFile.getAbsolutePath());
return ret;
} catch (Exception e) {
// Unable to rotate image based on EXIF data
e.printStackTrace();
return false;
}
}
public static String prepareJpegTranExe(Context context){
File exeDir = context.getDir("JpegTran", 0);
File exe = new File(exeDir, "jpegtran");
if(!exe.exists()){
try {
InputStream is = context.getAssets().open("jpegtran");
FileOutputStream os = new FileOutputStream(exe);
int bufferSize = 16384;
byte[] buffer = new byte[bufferSize];
int count;
while ((count=is.read(buffer, 0, bufferSize))!=-1) {
os.write(buffer, 0, count);
}
is.close();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return exe.getAbsolutePath();
}
public static boolean runCommand(String cmd){
try{
Process process = Runtime.getRuntime().exec(cmd);
BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
int read;
char[] buffer = new char[4096];
StringBuffer output = new StringBuffer();
while ((read = reader.read(buffer)) > 0) {
output.append(buffer, 0, read);
}
reader.close();
// Waits for the command to finish.
process.waitFor();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
Unfortunately, both take too long. It is 16 seconds on my Samsung Galaxy S1!!!! But I found out this app (https://play.google.com/store/apps/details?id=com.lunohod.jpegtool) only take 3-4 seconds. There must be some way to do.
Once you are done setting you bestPreviewSize You have to now set for bestPictureSize every phone supports different picture sizes so to get Best Picture quality you have to check supported picture sizes and then set best size to camera parameter. You have to set those parameters in surface changed to get the width and height. surfaceChanged will be called in start and thus your new parameters will be set.
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Camera.Parameters myParameters = camera.getParameters();
myPicSize = getBestPictureSize(width, height);
if (myBestSize != null && myPicSize != null) {
myParameters.setPictureSize(myPicSize.width, myPicSize.height);
myParameters.setJpegQuality(100);
camera.setParameters(myParameters);
Toast.makeText(getApplicationContext(),
"CHANGED:Best PICTURE SIZE:\n" +
String.valueOf(myPicSize.width) + " ::: " + String.valueOf(myPicSize.height),
Toast.LENGTH_LONG).show();
}
}
Now the getBestPictureSize ..
private Camera.Size getBestPictureSize(int width, int height)
{
Camera.Size result=null;
Camera.Parameters p = camera.getParameters();
for (Camera.Size size : p.getSupportedPictureSizes()) {
if (size.width>width || size.height>height) {
if (result==null) {
result=size;
} else {
int resultArea=result.width*result.height;
int newArea=size.width*size.height;
if (newArea>resultArea) {
result=size;
}
}
}
}
return result;
}
For rotation, try this..
final Matrix matrix = new Matrix();
matrix.setRotate(90):
final Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(),matrix,false);
I am using PNG images and it is working fine.... for JPEG images, please check the above code.
100% quality rate probably is a higher quality setting than the setting the files are originally saved with. This results in higher size but (almost) the same image.
I'm not sure how to get exactly the same size, maybe just setting the quality to 85% will do (Quick and Dirty).
However if you just want to rotate the pic in 90°-steps, you could edit just the JPEG-metadata without touching the pixel data itself.
Not sure how it's done in android, but this is how it works.

Image rotated when taking a photo in portrait mode

I have implemented a custom camera from which I take a picture,save it insert it into the media store and display it immediately after.I have been plagued by the problem of the saved image orientation,I have tried to fix this using ExifInterface using the filePath directly or by using the orientation from the Android Images content provider.
The orientation is always returned as 0.I have already used:
Android image selected from gallery Orientation is always 0 : Exif TAG
private int getExifOrientation(String pathName)
{
//for complete info on EXIF orientation visit: http://sylvana.net/jpegcrop/exif_orientation.html
ExifInterface exif=null;
try {
exif = new ExifInterface(pathName);
} catch (IOException e) {
// TODO Auto-generated catch block
Log.e("ImagePreviewActivity", "Exif data of the image could not be retreived");
}
int orientation=exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1);
return orientation;
}
private int getRotation(int orientation)
{
int rotation=0;
switch(orientation)
{
case ExifInterface.ORIENTATION_ROTATE_90:
//orientation values is 6
rotation=90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
//orientation value is 3
rotation=180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
//orientation value is 8
rotation=270;
break;
case -1:
Log.d("ImagePreviewActivity","Error getting orientation from Exif data.");
break;
case 1:
Log.d("ImagePreviewActivity", "Image is properly oriented");
default:
Log.d("ImagePreviewActivity", "The value of orientation is "+orientation);
}
return rotation;
}
private Bitmap rotateBitmap(String pathName,int rotation)
{
Bitmap bmp=BitmapFactory.decodeFile(pathName);
Matrix matrix=new Matrix();
matrix.postRotate(90);
//start from x=0,y=0 and filter=false
Bitmap rotatedBitmap=Bitmap.createBitmap(bmp,0,0,bmp.getWidth(),bmp.getHeight(),matrix,false);
return rotatedBitmap;
}
EDIT:
The output image has been displaying correctly when I take the picture in the Landscape mode,however it returns a rotated image(90 degrees) when taking a picture in portrait mode.
I am currently using the EXIF based method.
Make sure the pathname you are passing in does not have "file://" at the beginning of it. ExifInterface doesn't throw an error or anything if your path is prefixed with that, it just returns default for the orientation every time.

How to deal with Android devices that do not honor EXIF orientation data?

I'm testing my app on HTC Desire with Android 2.2. and it works exactly as I'd love to. I use Sherlock packages to have same style on older devices as on newer.
My AVD is set to use the latest android, and it also looks ok. Then I've placed it to Samsung Galaxy S2, and as I work with camera and gallery images, they are rotated wrong. It seams that something on Samsung (camera app, android it self) does not or it does check EXIF and my images are oriented wrong. Portrait images are loaded in landscape, and landscape images are loaded in portrait.
I guess I need to check EXIF somehow and ignore it in order to load images as they are?
Bigger problem is - how to know if there are any other devices (some HTC, some HUAWEI some whatsoever) that will do similar problem? I thought all android devices behave the same way beside having 4 screen size groups...
Tnx.
Without any code is hard to tell what's going on.
The most simple way i've found is to read the EXIF information and check if the image needs rotation. To read more on ExifInterface class on Android:
http://developer.android.com/intl/es/reference/android/media/ExifInterface.html
That said, here is some example code:
/** An URI and a imageView */
public void setBitmap(ImageView mImageView, String imageURI){
// Get the original bitmap dimensions
BitmapFactory.Options options = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeFile(imageURI, options);
float rotation = rotationForImage(getActivity(), Uri.fromFile(new File(imageURI)));
if(rotation!=0){
//New rotation matrix
Matrix matrix = new Matrix();
matrix.preRotate(rotation);
mImageView.setImageBitmap(Bitmap.createBitmap(bitmap, 0, 0, reqHeight, reqWidth, matrix, true));
} else {
//No need to rotate
mImageView.setImageBitmap(BitmapFactory.decodeFile(imageURI, options));
}
}
/** Returns how much we have to rotate */
public static float rotationForImage(Context context, Uri uri) {
try{
if (uri.getScheme().equals("content")) {
//From the media gallery
String[] projection = { Images.ImageColumns.ORIENTATION };
Cursor c = context.getContentResolver().query(uri, projection, null, null, null);
if (c.moveToFirst()) {
return c.getInt(0);
}
} else if (uri.getScheme().equals("file")) {
//From a file saved by the camera
ExifInterface exif = new ExifInterface(uri.getPath());
int rotation = (int) exifOrientationToDegrees(exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL));
return rotation;
}
return 0;
} catch (IOException e) {
Log.e(TAG, "Error checking exif", e);
return 0;
}
}
/** Get rotation in degrees */
private static float exifOrientationToDegrees(int exifOrientation) {
if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) {
return 90;
} else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) {
return 180;
} else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) {
return 270;
}
return 0;
}
If there is an error you will see the log "Error checking EXIF" on rotationForImage function.

Android image orientation issue with custom camera activity

I wrote a custom camera activity to handle some issues I've been having with certain android devices when calling intent image capture. The user is able to either select save image or just use the data returned back from the OnPictureTakenCallback.
The problem I'm having is displaying the image correctly with respect to the orientation it was taken. I force the activity to be displayed in portrait by calling SetRequestedOrientation.
How would I know the correct Orientation the camera was in when the user took the picture?
i.e. The user could take the picture at a rotation of 90 (portrait).
I've tried to get to use the getRotation() on the window manager's default display, but with setting the requested orientation to portrait that only returns Surface.ROTATION_0.
Update:
To clarify my other issue, how could I determine the orientation from just the byte[] data in the picture callback if the user were to not save the image?
Update: After trying the answers below with this code all I'm getting is ExifInterface.ORIENTATION_NORMAL. I've also changed my code to just save the file returned from the camera as I'm not sure there is an easy way to determine the orientation with just having the byte[] data.
private PictureCallback mPicture = new PictureCallback()
{
#Override
public void onPictureTaken(byte[] data, Camera camera)
{
File directory = new File(android.os.Environment.getExternalStoragePublicDirectory(android.os.Environment.DIRECTORY_PICTURES),
"MyApp");
if(!directory.exists())
{
if(!directory.mkdirs())
{
Log.d("CAMERA", "Unable to create directory to save photos.");
return;
}
}
File file = new File(directory.getPath() + file.separator + "IMG_" + SimpleDateFormat.getDateTimeInstance().toString() + ".jpg");
FileOutputStream fos = new FileOutputStream(file);
fos.write(data);
fos.close();
ExifInterface exif = new ExifInterface(file.getCanonicalPath());
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
int rotate = 0;
switch (orientation)
{
case ExifInterface.ORIENTATION_ROTATE_90:
rotate = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotate = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
rotate = 270;
break;
case default:
break;
}
}
};
SO you are facing some issue with the orientation of the camera.
This link shows an example app of a simple camera capture activity :
http://labs.makemachine.net/2010/03/simple-android-photo-capture/
Maybe you should try fixing the orientation by doing something like this :
ExifInterface exif = new ExifInterface(_path);
int exifOrientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
int rotate = 0;
switch (exifOrientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
rotate = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotate = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
rotate = 270;
break;
}
if (rotate != 0) {
int w = bitmap.getWidth();
int h = bitmap.getHeight();
// Setting pre rotate
Matrix mtx = new Matrix();
mtx.preRotate(rotate);
// Rotating Bitmap & convert to ARGB_8888, required by tess
bitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, false);
bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
}
You will need to read the Metadata from the original JPEG to verify the orientation in which the picture was taken.
ExifInterface exif = new ExifInterface(SourceFileName);
String exifOrientation = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
Source: How to determine orientation of picture without ExifInterface?
Edit: Answering your edit, have you tried using the getCameraInfo() method that is available with the Camera object passed in the callback? Does it have the info you require?
Source: http://developer.android.com/reference/android/hardware/Camera.html
Removing the setRequestedOrientation() allowed getWindowManager().getDefaultDisplay().getRotation() to give the correct rotation. I guess setting the requested orientation prevents the activity from redrawing itself when the configuration changes thus the device doesn't know any kind of rotation changed. My only issue now is switching from landscape mode at 0 degrees orientation to landscape mode 180 degrees rotation does not fire this:
#Override
public void onConfigurationChanged(Configuration newconfig)
{
super.onConfigurationChanged(newconfig);
}
public void surfaceCreated(SurfaceHolder holder) {
try {
mCamera.setPreviewDisplay(holder);
Camera.Parameters parameters = mCamera.getParameters();
if (this.getResources().getConfiguration().orientation !=
Configuration.ORIENTATION_LANDSCAPE)
{
parameters.set("orientation", "portrait"); <----THis gets the job done!!!
// For Android Version 2.2 and above
mCamera.setDisplayOrientation(90);
// For Android Version 2.0 and above
parameters.setRotation(90);
}
// End Effects for Android Version 2.0 and higher
mCamera.setParameters(parameters);
}
catch (IOException exception)
{
mCamera.release();
}
}

Categories

Resources