这是我的代码。这个问题与这个问题有些相关:Trying to scale down a Bitmap in Android not working
我注释掉了 options.inSampleSize
并且我仍然得到旋转(看似逆时针 90 度)。从 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);
}
byte[] 最初来自Commonsware CWAC Camera library ,我试过查看:Android Reduce Size Of Camera Picture
更新:
我已经开始抽取更多代码,试图让这种轮换的来源更加明显。我把它缩小到这个。 (这段代码仍然会导致旋转)
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);
}
最佳答案
这里的问题很可能是图像本身没有旋转,而是图像旋转显示并且您的转换正在清除(或至少不结转)操作系统将使用的元数据以正确显示它的方向。您对此无能为力,因为操作系统不会将该数据放入图像本身
如果不是这种情况,那么您遇到的就是字节顺序问题。一种格式是从左到右为您提供数据,另一种格式是从上到下。您需要重新排序字节。
关于java - 我的位图在缩放后在 Android 中旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21049914/