Подтвердить что ты не робот

Как повернуть растровое изображение на 90 градусов

В андроиде есть инструкция canvas.drawBitmap(visiblePage, 0, 0, paint);

Когда я добавляю canvas.rotate(90), эффекта нет. Но если я пишу

canvas.rotate(90)
canvas.drawBitmap(visiblePage, 0, 0, paint);

Я не получаю никакого растрового изображения. Так что я не делаю правильно?

4b9b3361

Ответ 1

Вы также можете попробовать это

Matrix matrix = new Matrix();

matrix.postRotate(90);

Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmapOrg, width, height, true);

Bitmap rotatedBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix, true);

Затем вы можете использовать повернутое изображение для установки в вашем изображении через

imageView.setImageBitmap(rotatedBitmap);

Ответ 2

public static Bitmap RotateBitmap(Bitmap source, float angle)
{
      Matrix matrix = new Matrix();
      matrix.postRotate(angle);
      return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
}

Получить битмап из ресурсов:

Bitmap source = BitmapFactory.decodeResource(this.getResources(), R.drawable.your_img);

Ответ 3

Краткое расширение для Kotlin

fun Bitmap.rotate(degrees: Float): Bitmap {
    val matrix = Matrix().apply { postRotate(degrees) }
    return Bitmap.createBitmap(this, 0, 0, width, height, matrix, true)
}

И использование:

val rotatedBitmap = bitmap.rotate(90F) // value must be float

Ответ 4

Ниже приведен код для поворота или изменения размера изображения в android

public class bitmaptest extends Activity {
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        LinearLayout linLayout = new LinearLayout(this);

        // load the origial BitMap (500 x 500 px)
        Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),
               R.drawable.android);

        int width = bitmapOrg.width();
        int height = bitmapOrg.height();
        int newWidth = 200;
        int newHeight = 200;

        // calculate the scale - in this case = 0.4f
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;

        // createa matrix for the manipulation
        Matrix matrix = new Matrix();
        // resize the bit map
        matrix.postScale(scaleWidth, scaleHeight);
        // rotate the Bitmap
        matrix.postRotate(45);

        // recreate the new Bitmap
        Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,
                          width, height, matrix, true);

        // make a Drawable from Bitmap to allow to set the BitMap
        // to the ImageView, ImageButton or what ever
        BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);

        ImageView imageView = new ImageView(this);

        // set the Drawable on the ImageView
        imageView.setImageDrawable(bmd);

        // center the Image
        imageView.setScaleType(ScaleType.CENTER);

        // add ImageView to the Layout
        linLayout.addView(imageView,
                new LinearLayout.LayoutParams(
                      LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT
                )
        );

        // set LinearLayout as ContentView
        setContentView(linLayout);
    }
}

Вы также можете проверить эту ссылку: http://www.anddev.org/resize_and_rotate_image_-_example-t621.html

Ответ 5

По умолчанию точкой вращения является точка Canvas (0,0), и я предполагаю, что вы можете повернуть ее по центру. Я сделал это:

protected void renderImage(Canvas canvas)
{
    Rect dest,drawRect ;

    drawRect = new Rect(0,0, mImage.getWidth(), mImage.getHeight());
    dest = new Rect((int) (canvas.getWidth() / 2 - mImage.getWidth() * mImageResize / 2), // left
                    (int) (canvas.getHeight()/ 2 - mImage.getHeight()* mImageResize / 2), // top
                    (int) (canvas.getWidth() / 2 + mImage.getWidth() * mImageResize / 2), //right
                    (int) (canvas.getWidth() / 2 + mImage.getHeight()* mImageResize / 2));// bottom

    if(!mRotate) {
        canvas.drawBitmap(mImage, drawRect, dest, null);
    } else {
        canvas.save(Canvas.MATRIX_SAVE_FLAG); //Saving the canvas and later restoring it so only this image will be rotated.
        canvas.rotate(90,canvas.getWidth() / 2, canvas.getHeight()/ 2);
        canvas.drawBitmap(mImage, drawRect, dest, null);
        canvas.restore();
    }
}

Ответ 6

Если вы поворачиваете растровое изображение, 90 180 270 360 в порядке, но для других градусов холст будет рисовать растровое изображение с разным размером.

Итак, лучший способ -

canvas.rotate(degree,rotateCenterPoint.x,rotateCenterPoint.y);  
canvas.drawBitmap(...);
canvas.rotate(-degree,rotateCenterPoint.x,rotateCenterPoint.y);//rotate back

Ответ 7

Я бы еще больше упростил функцию расширения Kotlin comm1x:

fun Bitmap.rotate(degrees: Float) =
    Bitmap.createBitmap(this, 0, 0, width, height, Matrix().apply { postRotate(degrees) }, true)

Ответ 8

Если ваша цель состоит в том, чтобы повернуть изображение в imageView или файле, вы можете использовать Exif для этого. Библиотека поддержки теперь предлагает следующее: https://android-developers.googleblog.com/2016/12/introduction-the-exifinterface-support-library.html

Ниже приводится его использование, но для достижения вашей цели вы должны проверить документацию API библиотеки для этого. Я просто хотел дать подсказку, что вращение растрового изображения не всегда лучший способ.

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

зависимость

скомпилировать "com.android.support:exifinterface:25.1.0"

Ответ 9

Просто будьте осторожны с растровым типом из вызова платформы Java, как из ответов comm1x и Gnzlt, потому что он может возвращать ноль. Я думаю, что это также более гибко, если параметр может быть любым числом и использовать инфикс для удобства чтения, зависит от вашего стиля кодирования.

infix fun Bitmap.rotate(degrees: Number): Bitmap? {
    return Bitmap.createBitmap(
        this,
        0,
        0,
        width,
        height,
        Matrix().apply { postRotate(degrees.toFloat()) },
        true
    )
}

Как пользоваться?

bitmap rotate 90
// or
bitmap.rotate(90)

Ответ 10

Используя метод Java createBitmap(), вы можете передавать градусы.

Bitmap bInput /*your input bitmap*/, bOutput;
float degrees = 45; //rotation degree
Matrix matrix = new Matrix();
matrix.setRotate(degrees);
bOutput = Bitmap.createBitmap(bInput, 0, 0, bInput.getWidth(), bInput.getHeight(), matrix, true);