在Android的图像直 [英] Image Straightening in Android

查看:149
本文介绍了在Android的图像直的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个项目,我需要实现图像直。我有一个想法,以做到这一点。我旋转的搜索栏的图像-10度到+10度。这是工作在白色背景是可见的,当我转动。因此,我们需要实现缩放功能也使得它看起来像拉直如下图所示。 请咨询您的建议。

样品code

 浮动A =(浮点)Math.atan(bmpHeight / bmpWidth);
//从中心到绿色的角长度
浮LEN1 =(浮子)((bmpWidth / 2)/Math.cos(一个-Math.abs(curRotate)));
//从中心到黑色的角落的长度(^ =功率)
浮动LEN2 =(浮动)的Math.sqrt((bmpWidth / 2)^ 2 +(bmpHeight / 2)^ 2);
//计算缩放因子
curScale = LEN2 / LEN1;
字模=新的Matrix();
matrix.postScale(curScale,curScale);
位图resizedBitmap = Bitmap.createBitmap(bitmaprotate,0,0,bmpWidth,bmpHeight,矩阵,真);
mainImage.setImageBitmap(resizedBitmap);
 

解决方案

所以,我想出了一个方程,做什么,我相信你想要的。在绿色矩形下面的图片是旋转的红色矩形的有效组成部分,我的假设是您要的缩放因子,这将使绿色的长方形黑色矩形的大小。

下面是C-式的伪code,因为我没有一个公式编辑器派上用场了。

  // THETA:图像的旋转角度
//宽度:图像的宽度(列数)
//高度:图像的高度(行数)

A = ATAN(高/宽);

//从中心到绿色的角长度
LEN1 =(宽度/ 2)/ COS(一个-ABS(THETA));
//从中心到黑色的角长度
LEN2 = SQRT(PO​​W(宽/ 2,2)+ POW(身高/ 2,2));
//计算缩放因子
规模= LEN2 / LEN1;
 

就是这样。假设所有的变革都是使用图像的中心作为参考只凭扩展旋转后的值缩放图像(理想的做法是结合的翻译,使用矩阵乘法,使得只有一个转换将被执行)的旋转,和比例缩放变换。

注:提供的等式假设高度>宽度。否则,与高度 LEN1 等式中替换的宽度。

黑色是原始图像。红色旋转图像(15度在这里)和绿色是红色图像的有效部分。要缩放红色图象所以绿色区域是尺寸相同的黑​​色图象。

I am working on a project where I need to implement Image Straightening. I have got an idea to do this. I am rotating the Image on SeekBar as -10 to +10 degrees. It is working by the white background is visible when I rotate. So, We need to implement zoom functionality also so that it looks like image straighten as shown in below. Please advice with your suggestions.

Sample code

float a = (float) Math.atan(bmpHeight/bmpWidth);
// the length from the center to the corner of the green
float len1 = (float) ((bmpWidth/2)/Math.cos(a-Math.abs(curRotate)));
// the length from the center to the corner of the black (^ = power)
float len2 = (float) Math.sqrt((bmpWidth/2)^2 + (bmpHeight/2)^2);
// compute the scaling factor
curScale = len2 / len1;
Matrix matrix = new Matrix();
matrix.postScale(curScale, curScale);
Bitmap resizedBitmap = Bitmap.createBitmap(bitmaprotate, 0, 0, bmpWidth, bmpHeight, matrix, true);
mainImage.setImageBitmap(resizedBitmap);

解决方案

So I've come up with an equation which does what I believe you want. In the picture below the green rectangle is the valid part of the rotated red rectangle and my assumption is that you want the scaling factor which will make the green rectangle the size of the black rectangle.

The following is c-esque pseudo code since I don't have an equation editor handy.

// theta  : the angle of rotation of the image
// width  : the width (number of columns) of the image
// height : the height (number of rows) of the image

a = atan(height/width);

// the length from the center to the corner of the green
len1 = (width/2)/cos(a-abs(theta));
// the length from the center to the corner of the black
len2 = sqrt(pow(width/2,2) + pow(height/2,2));
// compute the scaling factor
scale = len2 / len1;

That's it. Assuming all your transformations are done using the center of the image as a reference just scale the image by the value of scale after rotation (the ideal approach would be to combine the translations, rotation, and scaling transforms using matrix multiplication so that only one transformation would be performed).

Note: the equations provided assume height > width. Otherwise replace width with height in the len1 equation.

Black is the original image. Red is rotated image (by 15 degrees here) and green is the valid portion of the red image. You want to scale the red image so the green region is the same size as the black image.

这篇关于在Android的图像直的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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