android 圖片二維碼識別和儲存(二)

來源:互聯網
上載者:User

標籤:src   回收   roi   .com   又能   ade   比例   識別   多次   

續上一篇,開發圖片二維碼識別功能後,我們對功能進行效能分析記憶體佔用顯著提高了,不使用該功能記憶體佔用大約是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時間又能節省記憶體開銷。

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

 

android 圖片二維碼識別和儲存(二)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.