Android的位图旋转90度,结果压扁的图像。需要纵向和横向之间真正的旋转 [英] Android rotate bitmap 90 degrees results in squashed image. Need a true rotate between portrait and landscape

查看:219
本文介绍了Android的位图旋转90度,结果压扁的图像。需要纵向和横向之间真正的旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想旋转位图图像90度,它从横向格式更改为纵向格式。例如:

[A,B,C,D]
[E,F,G,H]
[I,J,K,L]

旋转90度顺时针变为

[I,E,A]
[D,F,B]
[K,G,C]
[升,H,D]

使用下面的code(从在线例子)图像旋转90度,但保留了景观长宽比,所以你最终有一个垂直压扁的图像。难道我做错了什么?有没有我需要使用其他方法?我也愿意旋转,我使用创建位图,如果是比较容易的JPEG文件。

  //创建一个矩阵操作
 字模=新的Matrix();
 //调整位图
 matrix.postScale(scaleWidth,scaleHeight);
 //旋转位图
 matrix.postRotate(90);

 //重新创建新的位图
 位图resizedBitmap = Bitmap.createBitmap(bitmapOriginal,0,0,widthOriginal,heightOriginal,矩阵,真);
 

解决方案

这是所有你需要旋转图像:

 矩阵矩阵=新的Matrix();
matrix.postRotate(90);
旋转= Bitmap.createBitmap(原始,0,0,
                              original.getWidth(),original.getHeight(),
                              矩阵,真正的);
 

在你的code样品,你提供一个呼叫后分频器。莫非是你的形象被拉伸的原因是什么?也许需要一个走出去,做一些更多的测试。

I am trying to rotate a bitmap image 90 degrees to change it from a landscape format to a portrait format. Example:

[a, b, c, d]
[e, f, g, h]
[i, j, k, l]

rotated 90 degree clockwise becomes

[i,e,a]
[j,f,b]
[k,g,c]
[l,h,d]

Using the code below (from an online example) the image is rotated 90 degrees but retains the landscape aspect ratio so you end up with a vertically squashed image. Am I doing something wrong? Is there another method I need to use? I am also willing to rotate the jpeg file that I'm using to create the bitmap if that is easier.

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

 // recreate the new Bitmap
 Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOriginal, 0, 0, widthOriginal, heightOriginal, matrix, true); 

解决方案

This is all you need to rotate the image:

Matrix matrix = new Matrix();
matrix.postRotate(90);
rotated = Bitmap.createBitmap(original, 0, 0, 
                              original.getWidth(), original.getHeight(), 
                              matrix, true);

In your code sample, you included a call to postScale. Could that be the reason your image is being stretched? Perhaps take that one out and do some more testing.

这篇关于Android的位图旋转90度,结果压扁的图像。需要纵向和横向之间真正的旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆