Android中矩阵旋转后的拉伸位图
我正在尝试旋转JPEG数据数组,将其转换为位图并旋转矩阵。它工作得很好,但当我尝试旋转一个16:9的图像时,我的问题出现了(4:3,我想它也会发生,但我能很好地欣赏它)。因为矩阵会旋转图像,但不会调整其大小,所以我的位图看起来会拉伸到宽度或高度,这取决于我随dataArray发送的方向Android中矩阵旋转后的拉伸位图,android,image,matrix,android-camera,android-bitmap,Android,Image,Matrix,Android Camera,Android Bitmap,我正在尝试旋转JPEG数据数组,将其转换为位图并旋转矩阵。它工作得很好,但当我尝试旋转一个16:9的图像时,我的问题出现了(4:3,我想它也会发生,但我能很好地欣赏它)。因为矩阵会旋转图像,但不会调整其大小,所以我的位图看起来会拉伸到宽度或高度,这取决于我随dataArray发送的方向 protected Bitmap doInBackground(Void... params) { // Decode image in background. Bitma
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,但是我做的测试之间没有任何不同 我已经找到了修复它的方法。这是裁剪方法内部的问题,该方法使用静态度量创建位图,我根据纵横比将该方法更改为addapt
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
我已经找到了修复它的方法。这是裁剪方法内部的问题,该方法使用静态度量创建位图,我根据纵横比将该方法更改为addapt
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