最近做的项目是拍照识别,同事写的,老板反映拍的照片没有全屏,找了半天才发现是,相机返回的照片在创建bitmap的时候,同事用的是这个Bitmap.createScaledBitmap,大家看看源码
public static Bitmap createScaledBitmap(@NonNull Bitmap src, int dstWidth, int dstHeight, boolean filter) { Matrix m = new Matrix(); final int width = src.getWidth(); final int height = src.getHeight(); if (width != dstWidth || height != dstHeight) { final float sx = dstWidth / (float) width; final float sy = dstHeight / (float) height; m.setScale(sx, sy); } return Bitmap.createBitmap(src, 0, 0, width, height, m, filter); }
final float sx = dstWidth / (float) width;
final float sy = dstHeight / (float) height;
m.setScale(sx, sy);
这三行代码对你传进去的宽高和bitmap的宽高进行了比例缩放,所以图片显示到 Imageview上不是实际bitmap的宽高,所以造成上面所说的拍出来的是全屏,实际显示不是全屏。
后面我用了下面这个方法就好了
public static Bitmap createBitmap(@NonNull Bitmap src) { return createBitmap(src, 0, 0, src.getWidth(), src.getHeight()); }