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

标签 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,即无效图像。我错过了什么吗?

谢谢,
拉贾特

最佳答案

下面是我用于解决我的问题的解决方案,以备将来有人需要。

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

关于android - 使用矩阵在android中旋转和缩放图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5415960/

相关文章:

android - 有 Intent 地启动短信应用程序

java - 存储图像图标

java - 查看管道技术示例

python - 通过 Pillow 的 Image.frombytes 创建的图像与预期不同

OpenCV - 如何将像素跟随轮廓转换为顶点跟随轮廓

java - 如何在 Android eclipse 中导入外部 JAR 文件

android - WebView : webpage not available but I load it from an html string

c++ - 将 OpenGL 窗口添加到 GTK 构建器中

c++ - 如何去除图像中的黑色部分?

Android - 为什么 IMarketBillingService 文件是 iadl 文件而不是 .java