Android高效旋转图片的方式

背景

在项目开发中涉及到比较多的图片处理,如摄像头预览图片的旋转,送入人脸识别sdk的图片数据,上传到后台识别记录的抓拍图片等等,以前常用的方式都是直接使用matrix来处理或者重复创建多个bitmap,这样会造成大量的资源浪费,并且效率低下,通过查找一些资料,发现了可以使用renderscript来处理,这样的方式会更快,因此在此记录下。

不知道renderscript是什么的同学可以看下这篇文章:Android高效计算——RenderScript

renderscript方式的优缺点

优点:

1、速度更快(对比matrix的方式速度2-5倍);

2、cpu和内存降低(使用同一份缓存);

缺点:

1、可能存在适配问题。
2、实现复杂。

代码

  • ScriptUtils
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.support.v8.renderscript.*;
import android.util.Log;
public class ScriptUtils {
   
	private static final String TAG = "ScriptUtil";
	private static RenderScript mRs;
	private static Allocation mInAllocation;
	private static Allocation mOutAllocation;
	private static ScriptC_rotatemirror mRotateRotateMirrorScript;
	private static ScriptC_rotate mRotateRotateScript;
	
	public static void getRotateBitmap(Context context, Bitmap bitmap,Bitmap outBitmap,
			boolean isFrontCamera) {
   
		if(isFrontCamera){
   
			getRotateAndMirrorBitmap(context, bitmap,outBitmap);
			return;
		}
		getRotateBitmap(context, bitmap,outBitmap);
	} 
	public static void getRotateBitmap(Context context, Bitmap bitmap,Bitmap outBitmap){
   
		if(bitmap == null || bitmap.isRecycled() || outBitmap == null || outBitmap.isRecycled()){
   
			Log.w(TAG, "getRotateBitmap bitmap is empty!");
		}
		initRsIfNeed(context,bitmap,outBitmap);	
		mInAllocation.copyFrom
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值