Android与JS混编(js调用android相机)

时间:2022-07-31 07:14:01

Android与JS混编(js调用android相机) Android与JS混编(js调用android相机) Android与JS混编(js调用android相机)

参考android相机调用,http://blog.csdn.net/yanzi1225627/article/details/33028041/,谢谢

相机怎么调用就不做赘述了,下面是js调用相机的代码:

java:

 **
* Created by Administrator on 2016/6/24 0024.
*/
public class QRCodeHtmlActivity extends Activity {
/**
* 网页
*/
private WebView wvQRCode;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_html_qrcode);
initView();
setWebView();
} private void initView() {
wvQRCode = (WebView) findViewById(R.id.wv_qrcode);
} /**
* 设置网页
*/
private void setWebView(){
//允许JavaScript执行
wvQRCode.getSettings().setJavaScriptEnabled(true);
//向js传递对象
wvQRCode.addJavascriptInterface(new ShowCamera(),"camera");
//不会node的小伙伴可以保存到assets
//wvQRCode.loadUrl("file:///android_asset/QRCode.html");
//访问网页
wvQRCode.loadUrl("http://192.168.253.1:3000/htmls/QRCode.html");
}
public final class ShowCamera {
//Html调用此方法传递数据,注解一定要留着否则会出错
@JavascriptInterface
public void showCamera() {
Intent intent = new Intent(QRCodeHtmlActivity.this, QRCodeSurfaceActivity.class);
//调用打开相机的界面
QRCodeHtmlActivity.this.startActivity(intent);
}
}
}

html:

 <!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>二维码调用</title>
<script type="text/javascript">
function showQRCode() {
window.camera.showCamera();
}
</script>
</head>
<body>
<h3>本例子是点击按钮调用相机扫描二维码</h3>
<input type="button" name="" value="点击调用" onclick="javascript:showQRCode()"/>
</body>
</html>

github: https://github.com/weifengzz/AndroidJSQRCode