6

Image Straightening を実装する必要があるプロジェクトに取り組んでいます。私はこれを行うためのアイデアを持っています。SeekBar の画像を -10 ~ +10 度回転させています。回転すると白い背景が見えるように動作しています。そのため、以下に示すように、画像がまっすぐに見えるように、ズーム機能も実装する必要があります。あなたの提案でアドバイスしてください。

ここに画像の説明を入力

ここに画像の説明を入力

サンプルコード

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);
4

2 に答える 2

11

下の図では、緑色の四角形が回転した画像の有効な部分です。決定する必要があるのは、緑色の領域を元の画像と同じサイズにする倍率です。図から、この倍率が に対する の比率であることがわかりlen2ますlen1

ここに画像の説明を入力

図といくつかの基本的な三角法を使用して、 と を見つけることができlen1ますlen2。次の C ライクな疑似コードは、ソリューションを説明しています。

// 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 green region
len1 = (width/2)/cos(a-abs(theta));
// the length from the center to the corner of original image
len2 = sqrt(pow(width/2,2) + pow(height/2,2));
// compute the scaling factor
scale = len2 / len1;

それでおしまい。scale画像の中心に関してすべての変換が行われたと仮定すると、回転を実行した後にの値で画像を単純にスケーリングします。

注: 提供される方程式は、 を想定してheight > widthいます。それ以外の場合は、式widthをに置き換えます。heightlen1

更新: Amulya Khare がここに実装例を投稿しました

于 2013-09-18T09:46:12.357 に答える