使用矩阵在android中旋转和缩放图像

时间:2011-03-24 07:21:37

标签: android graphics image-processing

我正在尝试旋转,然后使用android.graphics.Matrix缩放图像。代码如下:

    int width = bmp1.getWidth();
    int height = bmp1.getHeight();
    int smallerVal = (height > width) ? width : height;
    int n = 0;
    while (smallerVal > 1)
    {
        smallerVal = smallerVal >> 1;
        n++;
    }

    int pow2 = (int) Math.pow(2, n);
    float scaleWidth = ((float) AppBase.width) / (float)pow2;
    float scaleHeight = ((float) AppBase.height) / (float)pow2;

    Matrix matrix = new Matrix();
    matrix.postRotate(90);
    matrix.postScale(scaleWidth, scaleHeight);
    Bitmap bmp2 = Bitmap.createBitmap(bmp1, 0, 0, pow2, pow2, matrix, true);

pow2是为了应用纹理而获得2平方幂的图像。 bmp2的高度和宽度为-1,即图像无效。我错过了什么吗?

谢谢,
Rajath

1 个答案:

答案 0 :(得分:1)

我正在使用我的问题解决方案如下,以防将来有人需要它。

            int pow2 = getSquareVal(bmp1);
            int width = bmp1.getWidth();
            int height = bmp1.getHeight();
            float scaleWidth = ((float) pow2) / (float)width;
            float scaleHeight = ((float) pow2) / (float)height;

            Matrix matrix = new Matrix();
            matrix.reset();

            matrix.postScale(scaleWidth, scaleHeight);
            if (width > height)
                matrix.postRotate(90);

            bmp2 = Bitmap.createBitmap(bmp1, 0, 0, width, height, matrix, true);