2017-01-09 39 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