[英]My bitmap gets rotated in Android after scaling it
This is my code. 这是我的代码。 This question is somewhat related from this question: Trying to scale down a Bitmap in Android not working 这个问题与这个问题有些相关: 尝试缩小Android中的位图无法正常工作
I commented out the options.inSampleSize
and I still get the rotation (counter-clockwise 90 degrees seemingly). 我注释掉了options.inSampleSize
,我仍然得到了旋转(似乎是逆时针旋转90度)。 This seems like a fairly simple scaling down of an image, from Google documentation, and I'm not sure how I'm getting a rotated image. 从Google文档看,这似乎是按比例缩小图像的相当简单的方法,我不确定如何获得旋转的图像。
Bitmap myBitmap = null;
@Override
protected byte[] doInBackground(Object... params) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
//myImageByteArray is 4016 wide
myBitmap = BitmapFactory.decodeByteArray(myImageByteArray, 0, myImageByteArray.length, options);
if (options.outHeight > options.outWidth) {
//options.inSampleSize = calculateInSampleSize(options, 640, 960);
} else {
//options.inSampleSize = calculateInSampleSize(options, 960, 640);
}
options.inJustDecodeBounds = false;
//myImageByteArray is 4016 wide
myBitmap = BitmapFactory.decodeByteArray(myImageByteArray, 0, myImageByteArray.length, options);
//This log statement outputs around 1000 now.
Log.d("bitmap", myBitmap.getWidth()+"");
ByteArrayOutputStream bAOS = new ByteArrayOutputStream();
myBitmap.compress(CompressFormat.JPEG, 50, bAOS);
}
The byte[] is originally from Commonsware CWAC Camera library , and I've tried taking a look at: Android Reduce Size Of Camera Picture byte []最初来自Commonsware CWAC相机库 ,我尝试查看一下: Android缩小相机图片的大小
I have started pulling away more code to try to make it more apparent where this rotation could be coming from. 我已经开始删除更多的代码,以试图使这种旋转的来源更加明显。 I have it narrowed down to this. 我已经缩小到这个范围。 (This code still causes rotation) (此代码仍会导致旋转)
Bitmap myBitmap = null;
@Override
protected byte[] doInBackground(Object... params) {
final BitmapFactory.Options options = new BitmapFactory.Options();
myBitmap = BitmapFactory.decodeByteArray(myImageByteArray, 0, myImageByteArray.length, options);
ByteArrayOutputStream bAOS = new ByteArrayOutputStream();
myBitmap.compress(CompressFormat.JPEG, 50, bAOS);
}
It's probable that the issue here is that the image is not being rotated per-se, but that the image is being displayed with a rotation and your transform is clearing (or at least not carrying forward) the metadata the OS would use to show it oriented correctly. 这里的问题很可能是图像本身并没有旋转,而是图像正在旋转显示,并且您的转换正在清除(或至少不继承)操作系统用于显示图像的元数据方向正确。 There's not a lot you can do about that, because the OS doesn't put that data in the image itself 您对此无能为力,因为操作系统不会将数据放入映像本身
If that's not the case, then what you have is byte-order issue. 如果不是这种情况,那么您遇到的是字节顺序问题。 One format is giving you data left-to-right and the other is top-to-bottom. 一种格式是从左到右提供数据,另一种格式是从上到下。 You need to reorder the bytes. 您需要重新排序字节。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.