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;
}
}