【问题标题】:How to open phone camera from HTML page?如何从 HTML 页面打开手机摄像头?
【发布时间】:2021-09-09 17:52:26
【问题描述】:

我的 android 手机通过 webview 连接到 web 服务器。在我的 HTML 中,我有一个上传照片的按钮。用户应该可以选择上传图像或从相机拍照。 我的 HTML 代码是:

 <div  class="takePhoto">
    <form id="take" action="" method="POST"> 
        <input type="file" id= "cap" name="personalPhoto" accept="image/*" capture ="camera" id="camera"><p>
    </form>
</div>

但是,当我单击按钮时,文件选择器会打开,我可以选择图像但不能使用相机。有什么解决办法吗?

附:在我的代码中,(捕获)这个词没有我觉得很奇怪的特殊样式或颜色,这可能是问题所在!

【问题讨论】:

  • 是的,通过单击此按钮,文件浏览器将成功打开....但与相机无关
  • 使用&lt;input type="file" id="myinput" accept="image/*;capture=camera" capture&gt;

标签: android html


【解决方案1】:

在 iPhone iOS6 和 Android ICS 之后,HTML5 具有以下标签,可让您从设备上拍照:

 <input type="file" accept="image/*" capture="camera">

你也可以试试

 <input type="file" accept="image/*" capture="capture">

<input type="file" accept="image/*;capture=camera">

【讨论】:

  • 第一个建议在我的代码中并且不起作用!其他两个选项也不起作用
  • 在 Adnroid 中,此解决方案还为我提供了相机和文件的选项。我只想在android中打开直相机。我该怎么做?
【解决方案2】:
<input type="button" value="Say hello" onClick="showCamera('Android! Start Camera')" />

<script type="text/javascript">
    function showCamera(toast) {
        Android.showCamera(toast);
    }
</script>

在 Java 文件中初始化 Webview

WebView webView = (WebView) findViewById(R.id.webview);
webView.addJavascriptInterface(new WebAppInterface(this), "android")

网页界面:

public class WebAppInterface {
    Context mContext;
    static final int REQUEST_IMAGE_CAPTURE = 1;

    /** Instantiate the interface and set the context */
    WebAppInterface(Context c) {
        mContext = c;
    }

    /** Show a toast from the web page and here tell to open camera */
    @JavascriptInterface
    public void showCamera(String toast) {
        Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
        dispatchTakePictureIntent();
    }


    /** Handle Camera result*/
    private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        mContext.startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
        }
    }
}

【讨论】:

  • 感谢您的建议。实际上我试过了,当我按下按钮时,除了消息'Android!启动相机'它显示在屏幕上。这意味着该函数已被调用,但并非一切正常!并在行中: mContext.startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE); Android studio 说:无法解析方法 startActivityForResult。到目前为止,我无法让它工作。
【解决方案3】:

您可以将 android 本机代码与 javascript 接口,以捕获对按钮的点击,并让 android 代码发送意图以打开相机、拍摄照片并将其存储在某个路径中。

然后,在 onActivityResult 中,您将负责从路径中获取照片并将其上传到网络服务器。要做到这一点,一种方法是将位图编码为 base64 字符串并使用 android http 客户端以 POST 形式发送。

我将展示如何做 javscript 和 android 之间的接口(这就是问题所涉及的),所有其他关于操作 foto 的内容都有其自身的复杂性,并且有很多关于它的教程。

主要活动

public class MainActivity extends AppCompatActivity {

    private WebView webView;
    private Bitmap imageBitmap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        webView = (WebView) findViewById(R.id.webview);


        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);

        webView.addJavascriptInterface(new WebAppInterface(this),"Android");

        webView.loadData(
                "<Button id=\"btnTakePhoto\" onClick=\"takePhoto()\">TakePhoto</Button><div  class=\"takePhoto\">\n" +
                        "<script type=\"text/javascript\">" +
                        "function takePhoto() {\n" +
                        "        Android.takePhoto();\n" +
                        "    }\n" +
                        "</script>" +
                        "</div>", "text/html","UTF-8");
    }

    public class WebAppInterface {
        Context mContext;

        /** Instantiate the interface and set the context */
        WebAppInterface(Context c) {
            mContext = c;
        }

        /** The android function calld from javascript */
        @JavascriptInterface
        public void takePhoto() {
            dispatchTakePictureIntent();
        }
    }


    /* This gests a Thumbnail only*/

    static final int REQUEST_IMAGE_CAPTURE = 1;



    private void dispatchTakePictureIntent() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
        }
    }



    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
            Bundle extras = data.getExtras();
            imageBitmap = (Bitmap) extras.get("data");

            /* Encode bitmap as base64 and transmit to server */

        }
    }

}

注意:这种接口在 17 岁以下的 Andriod 上是不安全的。

编辑 1

html

 <html> 
    <body> 
        <div class="takePhoto"> 
                <Button onClick="takePhoto()">Take Photo </Button> 
        </div>

        <script type="text/javascript"> 
            function takePhoto(){
                 Android.takePhoto(); 
            }
        </script> 
    </body> 
 </html> 

编辑 2

在清单中我只添加了:

<uses-feature android:name="android.hardware.camera"
    android:required="true" />

对于您的应用,您还需要 INTERNET 权限。

【讨论】:

  • 我已将此代码集成到我的 android studio MainActivity ... 但是如何从网页中调用它?你能给我一个简单的 HTML/Javascript 代码来调用这个函数吗?
  • 在示例代码中显示 webview.loadData,您有一个带有指向 javascript 函数的 onCllick 按钮。在此之下,调用 android 函数的 javascript 函数。您可以将 html/javascript 放在网页上,然后调用 loadUrl。
  • 很抱歉问了很多问题,但我对 android 很陌生,非常感谢您的帮助......实际上在 onCreate 函数中,我正在使用以下方法从服务器加载我的网页: webView.loadUrl("192.168.43.115/myPage.php"); 在这个页面中我有一个应该调用相机的按钮,那么如何将它与你的代码连接起来?
  • 按钮添加 onClick="takePhoto()" 并在
【解决方案4】:

我现在有这种问题。我认为我找到了解决方案。但是你必须对我的解决方案做更多的事情。 你可以检查一下。 https://simpl.info/imagecapture/ 核对后,请与我分享您的经验。谢谢

【讨论】:

    【解决方案5】:

    我已经测试过这个方法,它适用于移动设备

    <input type="file" capture="user" accept="image/"/>
    

    【讨论】:

      猜你喜欢
      • 2018-05-07
      • 2021-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多