android 图片二维码识别和保存

古之立大事者,不惟有超世之才,亦必有坚忍不拔之志。这篇文章主要讲述android 图片二维码识别和保存相关的知识,希望能为你提供帮助。
续上一篇,开发图片二维码识别功能后,我们对功能进行性能分析内存占用显著提高了,不使用该功能内存占用大约是147M,使用这个功能多次以后,高达203M。

android 图片二维码识别和保存

文章图片

【android 图片二维码识别和保存】因此对功能进行研究,发现每次生成的图片没有即时的释放,导致内存中的图片不断累积,内存占用不断攀升。因此,需要对图片进行释放,释放的时候需要特别关注的地方有:
1.释放注意图片的状态。
2.注意异常的捕获。
下面就是图片释放的有关代码。
/** * 回收bitmap */ public static void recycleBitmap(Bitmap bitmap ) { if(bitmap != null & & !bitmap.isRecycled()){ bitmap.recycle(); bitmap = null; }}

对于异常的捕获主要是需要关注图片在进行encode和decode过程中的处理,原来的方法应该改为如下:
public static Result handleQRCodeFormBitmap(Bitmap bitmap) {Map< DecodeHintType, Object> hints = new EnumMap< > (DecodeHintType.class); hints.put(DecodeHintType.CHARACTER_SET, "utf-8"); hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE); hints.put(DecodeHintType.POSSIBLE_FORMATS, BarcodeFormat.QR_CODE); RGBLuminanceSource source = null; QRCodeReader reader2 = null; Result result = null; try { source = new RGBLuminanceSource(bitmap); BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source)); reader2 = new QRCodeReader(); result = reader2.decode(bitmap1, hints); } catch (Exception e) { e.printStackTrace(); if (source != null & & reader2 != null) { BinaryBitmap bitmap2 = new BinaryBitmap(new GlobalHistogramBinarizer(source)); try { result = reader2.decode(bitmap2, hints); } catch (Exception e1) { e1.printStackTrace(); } } } return result; }

当然对于整个流程来说还有其他的优化方法,比如将保存的图片格式压缩比例都进行调整,在不影响识别的前提下,将图片进行处理,这样既能节省cpu时间又能节省内存开销。
如果大家有其他更好的方案,欢迎提出。
 

    推荐阅读