how to rotate a bitmap 90 degrees

后端 未结 10 1576
离开以前
离开以前 2020-11-28 03:09

There is a statement in android canvas.drawBitmap(visiblePage, 0, 0, paint);

When I add canvas.rotate(90), there is no effect. But if I wri

10条回答
  •  再見小時候
    2020-11-28 03:54

    In case your goal is to have a rotated image in an imageView or file you can use Exif to achieve that. The support library now offers that: https://android-developers.googleblog.com/2016/12/introducing-the-exifinterface-support-library.html

    Below is its usage but to achieve your goal you have to check the library api documentation for that. I just wanted to give a hint that rotating the bitmap isn't always the best way.

    Uri uri; // the URI you've received from the other app
    InputStream in;
    try {
      in = getContentResolver().openInputStream(uri);
      ExifInterface exifInterface = new ExifInterface(in);
      // Now you can extract any Exif tag you want
      // Assuming the image is a JPEG or supported raw format
    } catch (IOException e) {
      // Handle any errors
    } finally {
      if (in != null) {
        try {
          in.close();
        } catch (IOException ignored) {}
      }
    }
    
    int rotation = 0;
    int orientation = exifInterface.getAttributeInt(
        ExifInterface.TAG_ORIENTATION,
        ExifInterface.ORIENTATION_NORMAL);
    switch (orientation) {
      case ExifInterface.ORIENTATION_ROTATE_90:
        rotation = 90;
        break;
      case ExifInterface.ORIENTATION_ROTATE_180:
        rotation = 180;
        break;
      case ExifInterface.ORIENTATION_ROTATE_270:
        rotation = 270;
        break;
    }
    

    dependency

    compile "com.android.support:exifinterface:25.1.0"

提交回复
热议问题