【问题标题】:Image Straightening in Android SampleAndroid 示例中的图像矫正
【发布时间】:2015-04-16 05:54:27
【问题描述】:

我想实现拉直,以便用户可以旋转图像。有人可以提供代码 sn-p 或图像矫正的示例吗?我试图找到一个例子,但失败了。

提前致谢

【问题讨论】:

标签: android image-rotation


【解决方案1】:

这是一个示例实现:

HomeActivity.java

public class HomeActivity extends Activity implements SeekBar.OnSeekBarChangeListener {

    private ImageView mImageView;
    private Matrix mMatrix;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        // load bitmap from resource
        Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.sample2);

        // calculate suitable width / height
        int WIDTH = getWindowManager().getDefaultDisplay().getWidth();
        int HEIGHT = (int)((image.getHeight() / (image.getWidth() * 1.0f)) * WIDTH);

        // scale image to be visible on screen
        image = Bitmap.createScaledBitmap(image, WIDTH, HEIGHT, false);

        // configure image view accordingly
        mImageView = (ImageView) findViewById(R.id.imageView);
        mImageView.setLayoutParams(new RelativeLayout.LayoutParams(WIDTH, HEIGHT));
        mImageView.setImageBitmap(image);

        SeekBar seekbar = (SeekBar) findViewById(R.id.rotate_bar);
        seekbar.setOnSeekBarChangeListener(this);
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        float angle = (progress - 45);
        float width = mImageView.getDrawable().getIntrinsicWidth();
        float height = mImageView.getDrawable().getIntrinsicHeight();

        if (width > height) {
            width = mImageView.getDrawable().getIntrinsicHeight();
            height = mImageView.getDrawable().getIntrinsicWidth();
        }

        float a = (float) Math.atan(height/width);

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

        Matrix matrix = mImageView.getImageMatrix();
        if (mMatrix == null) {
            mMatrix = new Matrix(matrix);
        }
        matrix = new Matrix(mMatrix);

        float newX = (mImageView.getWidth() / 2) * (1 - scale);
        float newY = (mImageView.getHeight() / 2) * (1 - scale);
        matrix.postScale(scale, scale);
        matrix.postTranslate(newX, newY);
        matrix.postRotate(angle, mImageView.getWidth() / 2, mImageView.getHeight() / 2);
        mImageView.setImageMatrix(matrix);
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) { }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) { }
}

activity_home.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:context=".HomeActivity">

    <ImageView
        android:id="@+id/imageView"
        android:scaleType="matrix"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"/>

    <SeekBar
        android:id="@+id/rotate_bar"
        android:max="90"
        android:progress="45"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"/>

</RelativeLayout>

注意:您需要一个名为 sample2drawable 才能使上述代码正常工作。

【讨论】:

  • 可以360度拉直吗?
  • 如果你想要360度,你应该去旋转。拉直通常意味着轻微(缩放 + 向右旋转)/(缩放 + 向左旋转)。上述矫直方程不适用于 +90/-90 度以上。
  • 好的..感谢您的回复...如果图像高度> =宽度,那么我还有一个问题,那么它工作正常,否则图像位置正在改变...
  • 我的错误。如果宽度较大,则将其与高度交换。请参阅更新的答案。谢谢:)
  • 我在拉直后出现另一个问题,当我拖动原始图像时,拖动不是结果图像你知道吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-06-14
  • 2011-08-13
  • 1970-01-01
  • 2011-06-16
  • 1970-01-01
  • 2012-09-04
  • 2014-10-31
相关资源
最近更新 更多