2017-01-09 41 views
0

小提琴從Android原生相機服務器上傳圖像jsfiddle通過我的html頁面Javascript的

現在用這個我能在Android設備打開本機攝像頭。

但我無法保存圖像,然後將此圖像發送到服務器,以便我可以將其保存在我的數據庫中。

從設備庫中選擇圖像發送並將圖像保存在服務器上後,可以執行哪些操作?使用

開幕機攝像頭功能:

function choosePhoto() { 
     var file = Android.choosePhoto(); 
     window.alert("file = " + file); 
    } 
+0

你問如何將選定的圖像發送到數據庫?它是什麼類型的數據庫? – Bojje

回答

0

參考你問的相關問題, 檢查我的回答 https://stackoverflow.com/a/41570241/3518278

我的回答涵蓋了如何打開攝像頭並從設備獲取的圖像並將其設置在html中。

這對你的這種情況有什麼幫助,是你可以使用js/jQuery從視圖< img中獲取圖像,然後做一個ajax/http請求來發布/把它放到服務器上。在你的情況下,你不需要一個< img>來設置圖像,你可以將數據保存在一個變量,並直接發佈到服務器或設置圖像隱藏< img>然後繼續。

所以,你會用下面的代碼來獲取,並通過JSInterface將圖像發送到HTML

    // Get the image data in a byte array 
        File imageFile = new File(imagePath); 
        Bitmap bitmap = Util.convertImageFileToBitmap(imageFile.getAbsolutePath(), Constants.IMAGE_COMPRESSED_WIDTH, Constants.IMAGE_COMPRESSED_HEIGHT); 
        ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
        bitmap.compress(Bitmap.CompressFormat.JPEG, 70, stream); 
        byte[] bytes = stream.toByteArray(); 

        // get the base 64 string of the image which was converted to a byte array above 
        final String imgBase64String = Base64.encodeToString(bytes, Base64.NO_WRAP); 

        runOnUiThread(new Runnable() { 
         @Override 
         public void run() { 
          mWebView.loadUrl("javascript:imageData('" + imgBase64String + "')"); 
         } 
        }); 

你的HTML將包含以下片段

function imageData(data){ 
    document.getElementById('displayImage').setAttribute('src', 'data:image/png;base64,'+data);   
    } 

示例項目佔地你正在尋找的功能可以在這裏找到

https://drive.google.com/drive/folders/0BwRMp8dK9LMLeEo5cTlXVE9ZUW8?usp=sharing