Android: rotate and display image from file

I have a very simple layout with ImageView. My application opens the camera, saves the image, and then displays the image in ImageView using BitmapFactory.decodeFile . The only problem is that it rotates. I understand that a) this is due to the fact that the phone’s camera does not have a landscape by default, therefore it needs to be processed in code, and b) image processing should be performed in a separate thread from the user interface.

The Android tutorial seems to be designed to load images from a resource identifier, not a file path. It just throws the key to things, because I can follow the textbooks until a certain point, but in the end I can’t come to terms with the differences.

Of course, I am new to this, so if someone can just give me a high-level overview of what needs to be done for this, I would really appreciate it. The following code is where I am now adding the bitmap to the ImageView. I assume my single thread call will go to onCreate ?

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_display_image); Bitmap bm = BitmapFactory.decodeFile(MainActivity.image_path); displayImageView = (ImageView) findViewById(R.id.myImageView); displayImageView.setImageBitmap(bm); } 
+5
source share
4 answers

Use ExifInterface to rotate

 picturePath = getIntent().getStringExtra("path"); Bitmap bitmap = BitmapFactory.decodeFile(picturePath); ExifInterface exif = new ExifInterface(picturePath); int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED); Matrix matrix = new Matrix(); 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: break; } myImageView.setImageBitmap(bitmap); bitmap.recycle(); 

Note: picturePath Image path from Gallery selected here.

+4
source

I managed to get it to work with the following approach. I think you could do it without AsyncTask, but it seems that in best practice, you should do image processing in a thread other than the UI (please feel free to fix it), I'm brand new in this).

An improvement that could be made with this would be more detailed handling of scaling. inSampleSize is a great tool, but it will only scale with degrees 2. Another improvement would be to only read meta / EXIF ​​data in bmOriginal , since the only use of this variable is to get the height and width.

ImageView XML is pretty standard. android: layout_width / height is set to fill_parent .

From java activity file -

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_display_image); new ImageRotator().execute(MainActivity.image_path); } private class ImageRotator extends AsyncTask<String, Void, Bitmap>{ protected Bitmap doInBackground(String... image_path){ BitmapFactory.Options bitmapOptions = new BitmapFactory.Options(); bitmapOptions.inSampleSize = 4; Bitmap bmOriginal = BitmapFactory.decodeFile(image_path[0], bitmapOptions); Matrix matrix = new Matrix(); matrix.postRotate(90); bmOriginal = Bitmap.createBitmap(bmOriginal, 0, 0, bmOriginal.getWidth(), bmOriginal.getHeight(), matrix, true); return bmOriginal; } protected void onPostExecute(Bitmap result) { myImageView = (ImageView) findViewById(R.id.myImageView); myImageView.setImageBitmap(result); } } 

The scaling process was found here and there is a great explanation of the AsyncTask features.

Thanks to everyone who answered and again - please feel free to let me know if this is far from the base or even if there is only the best way to do this.

+1
source

Why rotate the image. As you said, "ImageView itself is correctly oriented, but just that the image inside it is always displayed 90 degrees counterclockwise," and then just make ImageView rotated 90 degrees clockwise, the image will display correctly.

  displayImageView.setRotation(90); 
0
source

You can rotate the image using the matrix or set the rotation in xml. However, if you want more control, you can check out this guide written to rotate images and save to disk:

How to rotate an image in android

I hope this helps.

0
source

Source: https://habr.com/ru/post/1201529/


All Articles