Automatic camera rotation up to 90 degrees

In my code below, I try to take a picture using my own camera and upload it to the server, but when I perceive it as a portrait and view it in the gallery as a landscape, which means it is rotated 90 degrees. Help Pls: -

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(intent, REQUEST_CAMERA); @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == Activity.RESULT_OK) { if (requestCode == REQUEST_CAMERA) { handleCameraPhoto(); } private void handleCameraPhoto() { Intent mediaScanIntent = new Intent( "android.intent.action.MEDIA_SCANNER_SCAN_FILE"); File f = new File(mCurrentPhotoPath); Uri contentUri = Uri.fromFile(f); mediaScanIntent.setData(contentUri); getActivity().sendBroadcast(mediaScanIntent); } 

How can I rotate the image before saving to the SD card?

+7
android
source share
2 answers

I also encountered such a problem, showing images in a list. But using EXIF ​​data, I managed to find a job to set the images in the correct orientation.

This was prepared raster object for display:

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

This is the method used in the second line above the code to rotate the orientation of images.

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

It may not be the exact answer to your question, it worked for me and I hope that it will be useful for you.

+22
source share
 Matrix matrix=new Matrix(); imageView.setScaleType(ScaleType.MATRIX); //required matrix.postRotate((float) angle, pivX, pivY); imageView.setImageMatrix(matrix); This method does not require creating a new bitmap each time.. Hope this works. 
+2
source share

All Articles