Saving camera images android .... they're roated 90 degrees - android

I am using commonware's camera sample to try and create an android app that takes a picture. The preview shows the camera rotated 90 degrees and I've corrected that by applying:
camera.setDisplayOrientation(90);
However, the images are still saved rotated 90 degrees the other way. How can I adjust this so that when the images are saved, they're saved in the same "aspect" as that of the viewer?
Please advise,
TIA

According to this post , this happens only in some devices and it depends on the manufacturer.
I managed to workaround this issue by detecting the orientation angle of the device and apply the rotation accordingly, like this:
ExifInterface exitInterface = new ExifInterface(imagePath);
int orientation = exitInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
int degree = 0;
switch (orientation){
case ExifInterface.ORIENTATION_ROTATE_90:
degree = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
degree = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
degree = 270;
break;
}
Then I rotate my bitmap using a custom method:
public static Bitmap rotateImage(Bitmap src, float degree)
{
// create new matrix
Matrix matrix = new Matrix();
// setup rotation degree
matrix.postRotate(degree);
Bitmap bmp = Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), matrix, true);
return bmp;
}

Related

Getting Orientation as 0 and still getting the rotated image in ImageView in android

I have tried most of the code on stackoverflow but none of them are working.
I am using moto x4 for uploading picture using camera. when I use back camera it gets rotated 90 degree left and when I use front camera it gets rotated 90 degree right. but in debug mode, in both case I found the orientation = 0;
else if (requestCode == CAMERA) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
String imagePath = saveImage(thumbnail);
File imageFile = new File(imagePath);
ExifInterface exif = null;
try {
exif = new ExifInterface(imageFile.getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
}
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
bmap = GetandSetBitmap.rotateBitmap(thumbnail,orientation);
mImageView.setBackgroundResource(0);
mImageView.setImageBitmap(bmap);
}
This probably has to do with the fact that one camera is by default in landscape and the other in reverse landscape, so orientation = 0, as the orientation is detected to be the normal one in both cases. Unfortunately, I don't have a solution that wouldn't involve manually rotating the image to cover all cases. Personally I've used a switch to cater for the cases where my photo was saved with a 90 degrees rotation:
ExifInterface exifInterface = new ExifInterface(pictureFile.getPath());
int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
Bitmap correctedBitmap;
switch(orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
correctedBitmap = bitmap;
capturedImageHolder.setImageBitmap(correctedBitmap);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
correctedBitmap = rotateImage(bitmap, 90);
capturedImageHolder.setImageBitmap(correctedBitmap);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
correctedBitmap = rotateImage(bitmap, 180);
capturedImageHolder.setImageBitmap(correctedBitmap);
break;
case ExifInterface.ORIENTATION_NORMAL:
default:
correctedBitmap = rotateImage(bitmap, 270);
capturedImageHolder.setImageBitmap(correctedBitmap);
break;
}
} catch (IOException e) {
}
public static Bitmap rotateImage(Bitmap source, float angle) {
Matrix matrix = new Matrix();
matrix.postRotate(angle);
return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(),
matrix, true);
}
In your case, you would need to detect if the photo is taken from the front-facing camera or the back and adjust the values accordingly.

How to rotate image after capture from camera

Issues facing on Samsung, when I am capturing the image, the captured image get rotated. I am trying to rotate the image in the vertical direction 90 degree. On Back Facing the image get rotated with below code. But two images get stored on the device. Also issues facing while I am capturing the image on front face the image get rotated, how I can handle the image rotation in below code for both the front and back facing. Any ideas ?
try {
upload_bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
large_bitmap = scaleDownLargeImageWithAspectRatio(upload_bitmap);
String fileNameLarge = "myapp_" + System.currentTimeMillis() + ".jpg";
large_bitmap_path = MediaStore.Images.Media.insertImage(getContentResolver(), large_bitmap, fileNameLarge, null);
//Start change orientation
String device_name = Build.MANUFACTURER;
if(device_name.equals("samsung"))
{
int rotate=0;
try {
String realPath = getRealPathFromUri(CameraActivity.this, Uri.parse(large_bitmap_path));
exif = new ExifInterface(realPath);
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_270:
rotate = 270;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotate = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_90:
rotate = 90;
break;
case ExifInterface.ORIENTATION_NORMAL:
case ExifInterface.ORIENTATION_UNDEFINED:
break;
}
} catch (Exception e) {
e.printStackTrace();
}
Matrix matrix = new Matrix();
matrix.postRotate(90);
large_bitmap = Bitmap.createBitmap(large_bitmap, 0, 0, large_bitmap.getWidth(), large_bitmap.getHeight(), matrix, true);
horizontalList.add(large_bitmap_path);
horizontal_rv.smoothScrollBy(1000, 10);
sqLiteHelper.insertPath(large_bitmap_path, "jpg", "fileName");
}
use this library from git hub it handles all these thing related to image library link

FaceDetector does not recognize any face taken with the intent ACTION_IMAGE_CAPTURE

I am trying to get the faces of an image taken using the ACTION_IMAGE_CAPTURE intent. But for some reason the FaceDetector never finds an image. This is my code
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.RGB_565;
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, options);
options.inSampleSize = calculateInSampleSize(options,desiredWidth,desiredHeight);
options.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeFile(filePath,options);
ExifInterface exif = new ExifInterface(filePath);
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
Matrix matrix = new Matrix();
boolean recycle = true;
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
matrix.postRotate(90);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
matrix.postRotate(180);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
matrix.postRotate(270);
break;
default:
recycle = false;
break;
}
Bitmap finalBM = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
if(recycle)
bitmap.recycle();
FaceDetector.Face[] faces = new FaceDetector.Face[1];
FaceDetector faceDetector = new FaceDetector(bitmap.getWidth(), bitmap.getHeight(),1);
int numFaces = faceDetector.findFaces(bitmap,faces);
return finalBM;
At this point all i am doing is checking the numFaces variable to see if any face was detected. But every single time is 0 even thought the picture is just my own face. Also I am rotating the image if necessary. I tried to detect the faces before the rotation and it did not work.
After some digging i was able to find the problem with this code. There are two requirements one the Bitmap should be decoded using RGB_565 and on whatever orientation the image has the face should be upward. The problem in this code is that first yes it was decoding using the right format but the face was sideways. Once the image was rotated it was then created not in RGB_565. So I had to convert the rotated image to RGB_565 which can be easily done with Bitmap.copy https://developer.android.com/reference/android/graphics/Bitmap.html#copy(android.graphics.Bitmap.Config, boolean)
Then just run the FaceDetector code.

how to automatically turn the picture in the right form (library cropper )?

I use library cropper here in my project.
Everything works cool, but if a photo was made in portrait mode, it flips and is displayed in landscape mode.
Here is an original photo - original
This is opened by the program - program
There is a method in the library cropImageView.rotateImage(90); I can add a button that rererses the picture when pressed. But what can I do to make it run once loaded in the right mode?
imageCrop = (CropImageView) v.findViewById(R.id.imageCrop);
String picturePath = getArguments().getString("image");
crudeImage = BitmapFactory.decodeFile(picturePath);
imageCrop.setImageBitmap(crudeImage);
You need to decode the original image into bitmap in its orientation. Detect image orientation using ExifInterface, then use Matrix to rotate your bitmap to image's original orientation:
crudeImage = BitmapFactory.decodeFile(picturePath);
float rotationDegrees = getRotationDegreesToOriginal(picturePath);
if (rotationDegrees != 0f) {
crudeImage = rotateImage(crudeImage, rotationDegrees);
}
/**
* Rotate a bitmap by given degrees
* #param bitmap original bitmap
* #param degrees rotation degrees
* #return rotated bitmap
*/
public static Bitmap rotateImage(Bitmap bitmap, float degrees) {
Matrix matrix = new Matrix();
matrix.postRotate(degrees);
return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
}
/**
* Get rotation degrees that need to be applied to rotate an image to its original orientation
* #param fileName full path to image
* #return rotation degrees, or 0 if file not found
*/
public static float getRotationDegreesToOriginal(String fileName) {
final ExifInterface exif;
try {
exif = new ExifInterface(fileName);
} catch (IOException e) {
return 0f;
}
// Detect original orientation to rotate back to
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_180:
return 180f;
case ExifInterface.ORIENTATION_ROTATE_90:
return 90f;
case ExifInterface.ORIENTATION_ROTATE_270:
return 270f;
default:
return 0f;
}
}

Android app sends pictures to web server and pictures rotate

I am making an android app that sends pictures the user explicitly took and send it to a web server. Next, i display those pictures in a web application.
However, the pictures taken from the smartphone in portrait appear in the server rotated as if they were taken in landscape mode and vice versa.
Any idea why this is happening?
There is a property of the image, "exif tag". Which tells about the orientation of the image. You can check the value of this tag before sending the image to server.
you can use below method to get the unrotated image
public final static Bitmap getUnRotatedImage(String imahePath, Bitmap rotattedBitmap)
{
int rotate = 0;
try
{
File imageFile = new File(imahePath);
ExifInterface exif = new ExifInterface(imageFile.getAbsolutePath());
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
switch (orientation)
{
case ExifInterface.ORIENTATION_ROTATE_270:
rotate = 270;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotate = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_90:
rotate = 90;
break;
}
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
Matrix matrix = new Matrix();
matrix.postRotate(rotate);
return Bitmap.createBitmap(rotattedBitmap, 0, 0, rotattedBitmap.getWidth(), rotattedBitmap.getHeight(), matrix,
true);
}
It takes two arguments, image path and the bitmap image.
Try this method before sending the images to the server.

Categories

Resources