Controlling the Camera to Take Pictures in Portrait Doesn't Rotate the Final Images

Controlling the camera to take pictures in portrait doesn't rotate the final images

The problem is when I saved the image I didn't do well.

@Override
public void onPictureTaken(byte[] data, Camera camera) {

String timeStamp = new SimpleDateFormat( "yyyyMMdd_HHmmss").format( new Date( ));
output_file_name = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) + File.separator + timeStamp + ".jpeg";

File pictureFile = new File(output_file_name);
if (pictureFile.exists()) {
pictureFile.delete();
}

try {
FileOutputStream fos = new FileOutputStream(pictureFile);

Bitmap realImage = BitmapFactory.decodeByteArray(data, 0, data.length);

ExifInterface exif=new ExifInterface(pictureFile.toString());

Log.d("EXIF value", exif.getAttribute(ExifInterface.TAG_ORIENTATION));
if(exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("6")){
realImage= rotate(realImage, 90);
} else if(exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("8")){
realImage= rotate(realImage, 270);
} else if(exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("3")){
realImage= rotate(realImage, 180);
} else if(exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("0")){
realImage= rotate(realImage, 90);
}

boolean bo = realImage.compress(Bitmap.CompressFormat.JPEG, 100, fos);

fos.close();

((ImageView) findViewById(R.id.imageview)).setImageBitmap(realImage);

Log.d("Info", bo + "");

} catch (FileNotFoundException e) {
Log.d("Info", "File not found: " + e.getMessage());
} catch (IOException e) {
Log.d("TAG", "Error accessing file: " + e.getMessage());
}
}

public static Bitmap rotate(Bitmap bitmap, int degree) {
int w = bitmap.getWidth();
int h = bitmap.getHeight();

Matrix mtx = new Matrix();
// mtx.postRotate(degree);
mtx.setRotate(degree);

return Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true);
}

Android - Ensuring that photo orientation is preserved when taking photos via Camera Intent?

Is there a reliable way of making sure that the orientation of the camera gets preserved when saving the photo?

No.

First, you are delegating the picture taking to a third-party app, one of thousands of possible camera apps. Those developers can do whatever they want.

Second, the underlying cause of this comes from camera hardware and firmware. You have no means of telling some chipset to rotate the image instead of using the EXIF orientation header.

How to fix wrong rotation of photo from camera in flutter?

You can use package https://pub.dev/packages/flutter_exif_rotation

Support iOS and Android

In some devices the exif data shows picture in landscape mode when they're actually in portrait.

This plugin fixes the orientation for pictures taken with those devices.

For Android

Add this in your AndroidManifest.xml

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

code snippet

image = await FlutterExifRotation.rotateImage(path: image.path);

//Note : iOS not implemented
image = await FlutterExifRotation.rotateAndSaveImage(path: image.path);

Android camera and portrait rotation

Since I didn't find any complete answer to my question, I post my solution.

1. Get photo from the camera

protected File pictureFromCamera;
protected Uri photoUri;
...
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null)
{
// Create the File where the photo should go
pictureFromCamera = generateFile();
if (pictureFromCamera != null)
{
String authority = BuildConfig.APPLICATION_ID + ".provider";
photoUri = FileProvider.getUriForFile(DermatoPhotoCollectionActivity.this, authority, pictureFromCamera);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
startActivityForResult(takePictureIntent, REQUEST_PHOTO_FROM_CAMERA);
}
}

2. Get orientation of the photo

Here is the code to get the orientation, it is inspired by this article. Be aware that this require a dependency :
compile "com.android.support:exifinterface:27.1.1"

Getting the orientation of the photo depends of the device and might not work on some device (for example it works fine on my Xperia but not on Android Emulator).

public static int getOrientation(Context context, Uri photoUri)
{
InputStream in = null;
int orientation = ExifInterface.ORIENTATION_NORMAL;
try
{
in = context.getContentResolver().openInputStream(photoUri);
if (in != null)
{
android.support.media.ExifInterface exifInterface = new android.support.media.ExifInterface(in);
orientation = exifInterface.getAttributeInt(android.support.media.ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
}
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
if (in != null)
{
try
{
in.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}

return orientation;
}

3. Rotate the picture if needed

I use asyncTask to rotate the photo

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == RESULT_OK)
{
final String pathPhotoFromCamera = pictureFromCamera.getAbsolutePath();

final ProgressDialog progressDialog = createProgressDialog();
progressDialog.show();

int orientation = getOrientation(this, photoUri);
int rotationAngle = 0;

if (orientation != ExifInterface.ORIENTATION_NORMAL)
{
switch (orientation)
{
case ExifInterface.ORIENTATION_ROTATE_90:
rotationAngle = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotationAngle = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
rotationAngle = 270;
break;
}
}

AsyncTaskRotatePicture.AsyncTaskParams params = new AsyncTaskRotatePicture.AsyncTaskParams(pathPhotoFromCamera, rotationAngle);

AsyncTaskRotatePicture taskRotatePicture = new AsyncTaskRotatePicture(new CallBack()
{
@Override
public void onPostExecute()
{
progressDialog.dismiss();
}
});

taskRotatePicture.execute(params);
}
}

4. Finally the code that rotate the photo

The function overwrites the initial photo.

public static void rotatePhoto(String photoFilePath, int rotationAngle)
{
Bitmap bm = BitmapFactory.decodeFile(photoFilePath);
Matrix matrix = new Matrix();
matrix.setRotate(rotationAngle, (float) bm.getWidth() / 2, (float) bm.getHeight() / 2);
byte[] extractedBitmap = extractByteFromBitmap(Bitmap.createBitmap(bm, 0, 0, bm.getWidth() - 1, bm.getHeight() - 1, matrix, true));
saveBitmapOnSdcard(extractedBitmap, photoFilePath);
}

Android Camera Intent Saving Image Landscape When Taken Portrait

The picture is always taken in the orientation the camera is built into the device. To get your image rotated correctly you'll have to read the orientation information that is stored into the picture (EXIF meta data). There it is stored how the device was oriented, when the image was taken.

Here is some code that reads the EXIF data and rotates the image accordingly:
file is the name of the image file.

BitmapFactory.Options bounds = new BitmapFactory.Options();
bounds.inJustDecodeBounds = true;
BitmapFactory.decodeFile(file, bounds);

BitmapFactory.Options opts = new BitmapFactory.Options();
Bitmap bm = BitmapFactory.decodeFile(file, opts);
ExifInterface exif = new ExifInterface(file);
String orientString = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
int orientation = orientString != null ? Integer.parseInt(orientString) : ExifInterface.ORIENTATION_NORMAL;

int rotationAngle = 0;
if (orientation == ExifInterface.ORIENTATION_ROTATE_90) rotationAngle = 90;
if (orientation == ExifInterface.ORIENTATION_ROTATE_180) rotationAngle = 180;
if (orientation == ExifInterface.ORIENTATION_ROTATE_270) rotationAngle = 270;

Matrix matrix = new Matrix();
matrix.setRotate(rotationAngle, (float) bm.getWidth() / 2, (float) bm.getHeight() / 2);
Bitmap rotatedBitmap = Bitmap.createBitmap(bm, 0, 0, bounds.outWidth, bounds.outHeight, matrix, true);

UPDATE 2017-01-16

With the release of the 25.1.0 Support Library, an ExifInterface Support Library was introduced, which should perhaps make the access to the Exif attributes easier. See the Android Developer's Blog for an article about it.

RxImagePicker not calling completion if orientation changes when taking image

It turned out to be something not-so-extraordinary.

I've simply forgot to update to the latest version and was using a few months old version of the library, which had the fix for the problem that I'm facing.

I've updated to the latest version and the problem went away.

setRotation(90) to take picture in portrait mode does not work on samsung devices

I try to answer this in relation to the Exif tag. This is what I did:

Bitmap realImage = BitmapFactory.decodeStream(stream);

ExifInterface exif=new ExifInterface(getRealPathFromURI(imagePath));

Log.d("EXIF value", exif.getAttribute(ExifInterface.TAG_ORIENTATION));
if(exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("6")){

realImage=ImageUtil.rotate(realImage, 90);
}else if(exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("8")){
realImage=ImageUtil.rotate(realImage, 270);
}else if(exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("3")){
realImage=ImageUtil.rotate(realImage, 180);
}

The ImageUtil.rotate():

public static Bitmap rotate(Bitmap bitmap, int degree) {
int w = bitmap.getWidth();
int h = bitmap.getHeight();

Matrix mtx = new Matrix();
mtx.postRotate(degree);

return Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true);
}

All vertical oriented images are strangely autorotated only on some devices

Problem is not in scaling but captured images work differently as per the hardware.Before start scaling that should be rotated as per suitable devices.
Here it is following code:

  Matrix matrix = new Matrix();
matrix.postRotate(getImageOrientation(url));
Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
bitmap.getHeight(), matrix, true)

public static int getImageOrientation(String imagePath){
int rotate = 0;
try {

File imageFile = new File(imagePath);
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 (IOException e) {
e.printStackTrace();
}
return rotate;
}


Related Topics



Leave a reply



Submit