Log.w(TAG, “Cannot reset the input stream”, e);
}
}
}
…
}
紧接着就用到了 mark / reset 功能 , 读取头部信息里面存储的方向信息
关于 orientation 值代表的详细含义参考这篇文章: EXIF 方向参数 Orientation
// DownSampler.decode
final int[] inDimens = getDimensions(invalidatingStream, bufferedStream, options);
final int inWidth = inDimens[0];
final int inHeight = inDimens[1];
public int[] getDimensions(MarkEnforcingInputStream is, RecyclableBufferedInputStream bufferedStream,
BitmapFactory.Options options) {
options.inJustDecodeBounds = true;
decodeStream(is, bufferedStream, options);
options.inJustDecodeBounds = false;
return new int[] { options.outWidth, options.outHeight };
}
这里获取了待解析图片的宽高
计算缩放比例
// DownSampler.decode
// 计算图片被旋转的角度
final int degreesToRotate = TransformationUtils.getExifOrientationDegrees(orientation);
final int sampleSize = getRoundedSampleSize(degreesToRotate, inWidth, inHeight, outWidth, outHeight);
private int getRoundedSampleSize(int degreesToRotate, int inWidth, int inHeight, int outWidth, int outHeight) {
int targetHeight = outHeight == Target.SIZE_ORIGINAL ? inHeight : outHeight;
int targetWidth = outWidth == Target.SIZE_ORIGINAL ? inWidth : outWidth;
final int exactSampleSize;
if (degreesToRotate == 90 || degreesToRotate == 270) {
// 90 和 270 度 需要把长宽对调来计算缩放比例
exactSampleSize = getSampleSize(inHeight, inWidth, targetWidth, targetHeight);
} else {
exactSampleSize = getSampleSize(inWidth, inHeight, targetWidth, targetHeight);
}
// 去一个最大的 且 <= exactSampleSize 且 是2的次方
final int powerOfTwoSampleSize = exactSampleSize == 0 ? 0 :
Integer.highestOneBit(exactSampleSize);
// powerOfTwoSampleSize == 0 代表不缩放,也就是返回 1 倍
return Math.max(1, powerOfTwoSampleSize);
}
public static final Downsampler AT_LEAST = new Downsampler() {