zoukankan      html  css  js  c++  java
  • android 图片二维码识别和保存(二)

    续上一篇,开发图片二维码识别功能后,我们对功能进行性能分析内存占用显著提高了,不使用该功能内存占用大约是147M,使用这个功能多次以后,高达203M。

    因此对功能进行研究,发现每次生成的图片没有即时的释放,导致内存中的图片不断累积,内存占用不断攀升。因此,需要对图片进行释放,释放的时候需要特别关注的地方有:

    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时间又能节省内存开销。

    如果大家有其他更好的方案,欢迎提出。

  • 相关阅读:
    RMAN动态视图
    无归档模式下的备份
    验证备份集-使用DBVERIFY工具
    手工备份控制文件和参数文件
    针对发起alter tablespace test begin backup 断电情况的处理
    Jenkins一次任务构建中如何处理多个git仓库
    Element-ui Tree组件实现单选
    前端覆盖式发布引发的使用体验提升
    客户端localStorage命名冲突问题
    git 查看和删除分支
  • 原文地址:https://www.cnblogs.com/xilinch/p/9172518.html
Copyright © 2011-2022 走看看