Photo taken by the camera returns inverted

4

Is there any way I can not invert the photo after updating it in an ImageView? For example, the application I'm developing accesses the device's camera and after taking the picture, updates the ImageView with the photo reversed. I wanted to leave it the same way it is viewed in the phone's photo library.

In my code, I'm already able to change the image size after updating in ImageView.

Code

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MainActivity extends ActionBarActivity {

static final int REQUEST_IMAGE_CAPTURE = 1;

String mCurrentPhotoPath;
ImageView imageCapturedView;

private Bitmap bitmap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    imageCapturedView = (ImageView) findViewById(R.id.imageView);

    imageCapturedView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            abrirCamera();
        }

    });
}

public void abrirCamera(){
    Intent captureImageIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if(captureImageIntent.resolveActivity(getPackageManager())!=null) {
        File photoFile=null;

        try {
            photoFile = createImageFile();
        } catch(IOException e){

        }

        if(photoFile != null) {
            captureImageIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
            startActivityForResult(captureImageIntent, REQUEST_IMAGE_CAPTURE);
        }
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
        //Bundle extras = data.getExtras();
        //Bitmap imageBitmap = (Bitmap) extras.get("data");
        //imageCapturedView.setImageBitmap(imageBitmap);
        galleryAddPic();
        setPic();
    }
}

private File createImageFile() throws IOException {
    String TimeStamp = new SimpleDateFormat("yyyyMMDdd_HHmmss").format(new Date());
    String ImageFile = "JPEG_" + TimeStamp + "_";
    File StorageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);

    File image = File.createTempFile(ImageFile, ".jpg", StorageDir);

    mCurrentPhotoPath = image.getAbsolutePath();

    return image;

}

private void galleryAddPic()
{
    /*Intent mediaScan = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    File f= new File(mCurrentPhotoPath);
    Uri contentUri = Uri.fromFile(f);
    mediaScan.setData(contentUri);
    this.sendBroadcast(mediaScan);*/
}

private void setPic() throws ArithmeticException{
    int scaleFactor;
    Bitmap bitmap;
    // Get the dimensions of the View
    int targetW = imageCapturedView.getWidth();
    int targetH = imageCapturedView.getHeight();

    // Get the dimensions of the bitmap
    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inJustDecodeBounds = true;
    //bmOptions.inSampleSize = 4;
    BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
    int photoW = bmOptions.outWidth;
    int photoH = bmOptions.outHeight;

    if(targetH == 0) targetH = 1;
    if(targetW == 0) targetW = 1;

    // Determine how much to scale down the image
    scaleFactor= Math.min(photoW/targetW, photoH/targetH);

    // Decode the image file into a Bitmap sized to fill the View
    bmOptions.inJustDecodeBounds = false;
    bmOptions.inSampleSize = scaleFactor;
    bmOptions.inPurgeable = true;

    bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);

    Toast.makeText(getApplicationContext(), mCurrentPhotoPath, Toast.LENGTH_LONG).show();
    imageCapturedView.setImageBitmap(bitmap);
}

/* ----------------------------------- */

public boolean onCreateOptionsMenu(Menu menu) {
    // adiciona itens para a barra de ação, se ela estiver presente.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;

}
}
    
asked by anonymous 13.01.2015 / 13:43

1 answer

3

Depending on the camera's mobile phone drive, it rotates the picture before recording or saves the EXIF rotation value in TAG_ORIENTATION.

So, in order for our program to display the photo correctly, we have to read the EXIF associated with the photo and rotate it according to TAG_ORIENTATION.

Code to get the rotation saved in EXIF:

ExifInterface exif = new ExifInterface(filename);  
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);  

Code to rotate the photo:

Bitmap bmRotated = rotateBitmap(bitmap, orientation);  

Method to run the bitmap:

public static Bitmap rotateBitmap(Bitmap bitmap, int orientation) {

    try{
        Matrix matrix = new Matrix();
        switch (orientation) {
            case ExifInterface.ORIENTATION_NORMAL:
                return bitmap;
            case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
                matrix.setScale(-1, 1);
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                matrix.setRotate(180);
                break;
            case ExifInterface.ORIENTATION_FLIP_VERTICAL:
                matrix.setRotate(180);
                matrix.postScale(-1, 1);
                break;
            case ExifInterface.ORIENTATION_TRANSPOSE:
                matrix.setRotate(90);
                matrix.postScale(-1, 1);
                break;
           case ExifInterface.ORIENTATION_ROTATE_90:
               matrix.setRotate(90);
               break;
           case ExifInterface.ORIENTATION_TRANSVERSE:
               matrix.setRotate(-90);
               matrix.postScale(-1, 1);
               break;
           case ExifInterface.ORIENTATION_ROTATE_270:
               matrix.setRotate(-90);
               break;
           default:
               return bitmap;
        }
        try {
            Bitmap bmRotated = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
            bitmap.recycle();
            return bmRotated;
        }
        catch (OutOfMemoryError e) {
            e.printStackTrace();
            return null;
        }
    }
    catch (IOException e) {
        e.printStackTrace();
        return null;
    }
    return bitmap;
}
    
13.01.2015 / 15:53