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

來源:互聯網
上載者:User

標籤:cas   hybrid   TE   增加   androi   iso   reader   結果   需要   

   最新業務開發二維碼識別的功能,這個功能,在很多應用上都有,比如長按圖片識別二維碼,如果圖片中存在可以識別的二維碼時,可以增加一個選項 識別二維碼。那麼如何去實現這個功能呢。這裡其實也非常簡單,首先對圖片進行二維碼識別,識別結果返回的時候判斷是否有二維碼,有則增加識別二維碼選項。

   對於識別二維碼,目前主流的就是zxing和zbar,對於這兩者的選型,一般來說移動智能手機更多選擇zxing,因為zxing更適合,zbar適合嵌入式硬體,兩者對於QR圖形碼能力相差無幾,但是zxing的資料明顯更多一些。各位根據自己的情況進行調整。

    二維條碼/二維碼(2-dimensional bar code)是用某種特定的幾何圖形按一定規律在平面(二維方向上)分布的黑白相間的圖形記錄資料符號資訊的。
 在代碼編製上巧妙地利用構成電腦內部邏輯基礎的“0”、“1”位元流的概念,使用若干個與二進位相對應的幾何形體來表示文字數值資訊,通過圖象輸入裝置或光電掃描裝置自動識讀以實現資訊自動處理:它具有條碼技術的一些共性:每種碼制有其特定的字元集。
 每個字元佔有一定的寬度;具有一定的校正功能等。同時還具有對不同行的資訊自動識別功能、及處理圖形旋轉變化點。

  對於資訊量越大的二維碼圖形點越多,多到一定程度識別就困難了,二維碼有一定的錯誤修正能力,但是不是萬能的。

 

  首先給出識別二維碼的實現,二維碼識別一組資料。

 /**     * 對二維碼圖片解碼結果進行編碼     *     * @param str     * @return     */    public static String recode(String str) {        String formart = "";        try {            boolean ISO = Charset.forName("ISO-8859-1").newEncoder()                    .canEncode(str);            if (ISO) {                formart = new String(str.getBytes("ISO-8859-1"), "UTF-8");            } else {                formart = str;            }        } catch (UnsupportedEncodingException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } catch (Exception e){        }        return formart;    }

其次,二維碼產生的程式碼如下:

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 = new RGBLuminanceSource(bitmap);        BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source));         QRCodeReader reader2 = new QRCodeReader();        Result result = null;        try {            result = reader2.decode(bitmap1, hints);        } catch (Exception e) {            e.printStackTrace();            if (source != null) {                BinaryBitmap bitmap2 = new BinaryBitmap(new GlobalHistogramBinarizer(source));                 try {                    result = reader2.decode(bitmap2, hints);                } catch (Exception e1) {                    e1.printStackTrace();                }            }        }        return result;    }

對返回的result進行處理,如果未能識別,彈框中則無識別二維碼選項。

而對於圖片會有儲存的功能,圖片手進行進行命名,然後設定儲存路徑,建立好路徑,最後將圖片存入檔案,根據需要是否需要進行壓縮,可以設定相關參數。儲存以後通知系統更新圖片庫。

儲存圖片的代碼如下:

public static void saveImageToGallery(Context context, Bitmap bmp) {        String fileName = System.currentTimeMillis() + ".jpg";        String filePath = FileUtils.getFilePath(context, "/pictures/qrcode/");        // 首先儲存圖片        File appDir = new File(filePath);        if (!appDir.exists()) {            appDir.mkdir();        }        File file = new File(appDir, fileName);        try {            FileOutputStream fos = new FileOutputStream(file);            bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);            fos.flush();            fos.close();        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        } catch (Exception e){        }        // 其次把檔案插入到系統圖庫        ContentValues values = new ContentValues();        values.put(MediaStore.Images.Media.DATA, file.getAbsolutePath());        values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");        Uri uri = context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);        // 最後通知圖庫更新        context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)));    }

到這裡二維碼識別和圖片儲存的功能就算基本完成了。

但是,經過實際的測試發現,這種方式只管功能,沒有兼顧效能,進行多次操作導致記憶體使用量增加30%,下一篇將描述分析過程和解決方案。

 

 

 

 

 

 

 

 

 

  

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.