Android 矩阵旋转后的拉伸位图

Stretched bitmap after matrix rotation in Android

我正在尝试旋转 JPEG 数据数组,将其转换为位图并旋转矩阵。它工作正常,但是当我尝试旋转 16:9 图像时出现了我的问题(我认为 4:3 也会发生这种情况,但我非常欣赏它)。因为矩阵旋转图像但不调整它的大小,所以我的位图看起来被拉伸到宽度或高度,具体取决于我用 dataArray 发送的方向。

  protected Bitmap doInBackground(Void... params) {

        // Decode image in background.

        Bitmap bitmap = CameraUtil.downSample(mData, mDownSampleFactor);
        Matrix m = new Matrix();
        Log.i(TAG, "Bitmap=" + bitmap.getWidth() + "x" + bitmap.getHeight());
        if ((mOrientation != 0 || mMirror) && (bitmap != null)) {              

            if (mMirror) {

            m.setScale(1f, -1f);
        }

            m.postRotate(mOrientation);
            return Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),m,false);
        }

        return bitmap;

    }

 public static Bitmap downSample(final byte[] data, int downSampleFactor) {
        final BitmapFactory.Options opts = new BitmapFactory.Options();
        // Downsample the image
        opts.inSampleSize = downSampleFactor;

    return  BitmapFactory.decodeByteArray(data, 0, data.length, opts);
}

我已经尝试手动调整图像大小,还将数组转换为 NV21 图像并旋转数组以稍后将其转换为位图并发送。但这给我带来了问题(太慢了,有时它看起来完全是绿色和粉红色(彩虹!))。我也尝试使用 Pre、Set 和 PostRotate,但它并没有给我所做的测试之间的任何不同。

我已经找到修复它的方法了。这是crop方法内部的问题,它使用静态措施来创建BitMap,我将方法更改为根据宽高比添加适应。

 if (bmp.getWidth() != radius || bmp.getHeight() != radius) {
            //Depending of the Height and Width we get the Aspect Ratio to scale the image before crop it
            if (bmp.getHeight() > bmp.getWidth()) {
                mAspectRatio = (float) bmp.getHeight() / (float) bmp.getWidth();
                sbmp = Bitmap.createScaledBitmap(bmp, radius, (int) (radius * mAspectRatio), false);
            } else {
                mAspectRatio = (float) bmp.getWidth() / (float) bmp.getHeight();
                sbmp = Bitmap.createScaledBitmap(bmp, (int) (radius * mAspectRatio), radius, false);

            }
        }
    } else {
        sbmp = bmp;
    }
    //output Bitmap will have the final Scale of the Thumbnail, which will be radius x radius
    Bitmap output = Bitmap.createBitmap(radius,radius, Config.ARGB_8888);
    Canvas canvas = new Canvas(output);
//.....Draw canvas, etc etc