Android 实现像仪器一样的图像校正
使用此链接: 我已经成功地根据倾斜程度将图像放大到足够大。但现在我想旋转图像,同时保持纵横比为1:1。可能吗?更新后的Instagram应用程序提供了这种缩放+旋转功能。有人知道如何做到这一点吗Android 实现像仪器一样的图像校正,android,image,rotation,zooming,instagram,Android,Image,Rotation,Zooming,Instagram,使用此链接: 我已经成功地根据倾斜程度将图像放大到足够大。但现在我想旋转图像,同时保持纵横比为1:1。可能吗?更新后的Instagram应用程序提供了这种缩放+旋转功能。有人知道如何做到这一点吗 public void setImageStraighten ( Bitmap bitmap, int bmpHeight, int bmpWidth, double theta ) { float a = ( float ) Math.atan( bmpHeight / bmpWidth )
public void setImageStraighten ( Bitmap bitmap, int bmpHeight, int bmpWidth, double theta ) {
float a = ( float ) Math.atan( bmpHeight / bmpWidth );
// the length from the center to the corner of the green
double len1 = ( ( bmpWidth / 2 ) / Math.cos( a - Math.abs( theta ) ) );
// the length from the center to the corner of the black (^ = power)
double len2 = Math.sqrt( Math.pow( ( bmpWidth / 2 ) , 2 ) + Math.pow(( bmpHeight / 2 ) , 2 ));
// compute the scaling factor
float curScale = ( float ) ( len2 / len1 );
Matrix matrix = new Matrix();
matrix.postScale( curScale, curScale );
Matrix rotateMtrix = new Matrix( );
rotateMtrix.postRotate( 5 );
Bitmap resizedRotatedBitmap = Bitmap.createBitmap( bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), rotateMtrix, false );
Bitmap bmp = ScalingUtilities.createScaledBitmap( resizedRotatedBitmap , 720,720, ScalingUtilities.ScalingLogic.CROP);
int startHeight = (int)((curScale*bmpHeight)- bmpHeight);
Bitmap resizedBitmap = cropBitmap1( bmp , bmpHeight);
imgSwap.setImageBitmap( resizedBitmap );
imgSwap.setScaleType( ImageView.ScaleType.CENTER_CROP );
}
是的。因此,发布一些你已经尝试过的代码!祝你一切顺利。因此,在
rotateMtrix.postRotate中(5)代码>此处旋转5度(不明显),旋转角度为度,而上面的数学(你真的需要那么多三角吗?)将以弧度为单位(文档:供参考)。为什么要创建新的矩阵()。应该只需要postScale
和postRotate
一个矩阵。此外,如果您是postclassing
矩阵,为什么需要使用ScalingUtilities
。可能是这些问题的好答案,但为了给出答案,了解这些可能会有所帮助。谢谢好吧,对不起,我没怎么解释。我在问题中提供的链接显示了我想要实现的目标。我有一张长宽比为1:1的图像。我想在上面添加一个拉直功能,同时保持纵横比以及宽度和高度。为了达到这一效果,Instagram在向内旋转时会放大图像(从-5度旋转到+5度是最小的)。从我上面提供的代码中,我得到的是旋转和缩放已经完成,但图像不再是正方形。但这是一个平行图。这是可以理解的,因为我已经旋转了位图。但我想实现旋转并保持相同的正方形图像。唯一的办法是我提供一个秤。我无法以某种方式获得我试图达到的效果。我希望我能澄清我的问题。对。知道了。缩放是绝对正确的,这是唯一的方法。平行四边形是指正方形图像的边界是旋转的,对吗?为什么要创建和旋转位图,而不是使用自定义图像视图和画布?是否需要保存这些位图。