Bitmap旋转方法

最近在做一个ORC图片解析的功能,要求解析出数字

但是发现他解析只能解析横着的图片,然后我拍照的时候拍的是竖直照片,当然你也可以去旋转照相机的屏幕

但是我这里为了方便选择的是竖直拍出来

然后,旋转下咯(ps:网上找的通用方法)

方法1. 利用Bitmap.createBitmap

   Bitmap
adjustPhotoRotation(Bitmap bm, final int orientationDegree)
{ Matrix m = new Matrix();
m.setRotate(orientationDegree, (float) bm.getWidth() / 2, (float) bm.getHeight() / 2); try { Bitmap bm1 = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), m, true); return bm1; } catch (OutOfMemoryError ex) {
} return null; } 方法2. 利用Canvas.drawBitmap Bitmap
adjustPhotoRotation(Bitmap bm, final int orientationDegree)
{ Matrix m = new Matrix();
m.setRotate(orientationDegree, (float) bm.getWidth() / 2, (float) bm.getHeight() / 2);
float targetX, targetY;
if (orientationDegree == 90) {
targetX = bm.getHeight();
targetY = 0;
} else {
targetX = bm.getHeight();
targetY = bm.getWidth();
} final float[] values = new float[9];
m.getValues(values); float x1 = values[Matrix.MTRANS_X];
float y1 = values[Matrix.MTRANS_Y]; m.postTranslate(targetX - x1, targetY - y1); Bitmap bm1 = Bitmap.createBitmap(bm.getHeight(), bm.getWidth(), Bitmap.Config.ARGB_8888); Paint paint = new Paint();
Canvas canvas = new Canvas(bm1);
canvas.drawBitmap(bm, m, paint); return bm1;
}

其中方法2的耗时是方法一的一半~

建议是方法二

上一篇:爬虫之pyquery库


下一篇:Flex box弹性布局 及 响应式前端设计的优化