我已创建WebView活动并正在加载https://web.doar.zone/c冠状病毒
这个URL需要相机权限,这是我在Android中获得的运行时权限。
下面是mainactivity.java的完整代码:
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
Context context;
ActivityMainBinding binding;
private String url = "https://web.doar.zone/coronavirus";
@Override
protected void onResume() {
super.onResume();
checkCameraPermission();
}
private void checkCameraPermission() {
int writeExternalStorage = ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA);
if (writeExternalStorage != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, 1001);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 1001) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//Do your stuff
openWebView();
} else {
checkCameraPermission();
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
context = getApplicationContext();
openWebView();
}
@SuppressLint("SetJavaScriptEnabled")
void openWebView() {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkInfo networkInfo;
if (connectivityManager != null) {
networkInfo = connectivityManager.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnectedOrConnecting()) {
binding.internetTextView.setVisibility(View.INVISIBLE);
binding.webView.setVisibility(View.VISIBLE);
//binding.webView.getSettings().setUserAgentString("Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.3");
binding.webView.getSettings().setJavaScriptEnabled(true);
binding.webView.getSettings().setUseWideViewPort(true);
binding.webView.getSettings().setDomStorageEnabled(true);
binding.webView.setInitialScale(1);
binding.webView.setWebChromeClient(new MyWebChromeClient());
binding.webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView webview, String url) {
Uri uri = Uri.parse(url);
if (uri.getScheme().contains("whatsapp") || uri.getScheme().contains("tel")) {
try {
Intent intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
if (intent.resolveActivity(getPackageManager()) != null)
startActivity(intent);
return true;
} catch (URISyntaxException use) {
Log.e("TAG", use.getMessage());
}
} else {
webview.loadUrl(url);
}
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
});
binding.webView.loadUrl(url);
} else {
binding.internetTextView.setVisibility(View.VISIBLE);
binding.buttonTryAgain.setVisibility(View.VISIBLE);
binding.webView.setVisibility(View.INVISIBLE);
Toast.makeText(context, "Connect to Internet and Refresh Again", Toast.LENGTH_LONG).show();
}
} else {
binding.internetTextView.setVisibility(View.VISIBLE);
binding.buttonTryAgain.setVisibility(View.VISIBLE);
binding.webView.setVisibility(View.INVISIBLE);
Toast.makeText(context, "Connect to Internet and Refresh Again", Toast.LENGTH_LONG).show();
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (binding.webView.canGoBack()) {
binding.webView.goBack();
} else {
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
class MyWebChromeClient extends WebChromeClient {
MyWebChromeClient() {
// TODO Auto-generated constructor stub
binding.pb.setProgress(0);
}
@Override
public void onPermissionRequest(final PermissionRequest request) {
super.onPermissionRequest(request);
//request.grant(request.getResources());
}
public void onProgressChanged(WebView view, int progress) {
if (progress < 100 /* && pBar.getVisibility() == View.VISIBLE*/) {
binding.pb.setVisibility(View.VISIBLE);
}
binding.pb.setProgress(progress);
if (progress == 100) {
binding.pb.setVisibility(View.GONE);
}
}
}
}
request.grant(request.getResources());
java.lang.IllegalStateException: Either grant() or deny() has been already called.
at org.chromium.android_webview.permission.AwPermissionRequest.c(PG:3)
at org.chromium.android_webview.permission.AwPermissionRequest.b(PG:1)
at Cn.grant(PG:8)
at com.example.webviewapp.MainActivity$MyWebChromeClient.onPermissionRequest(MainActivity.java:164)
at org.chromium.android_webview.AwContents.onPermissionRequest(PG:8)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:326)
at android.os.Looper.loop(Looper.java:181)
at android.app.ActivityThread.main(ActivityThread.java:7078)
2020-04-29 11:52:21.813 4943-4943/com.example.webviewapp W/System.err: at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
您已经处理了android操作系统的运行时相机权限。我们还需要在webview内部授予相机权限(resource_video_capture
)。
首先调用下面的方法,在没有用户手势的情况下打开相机:
binding.webView.getSettings().setMediaPlaybackRequiresUserGesture(false);
像这样调用request.grant(request.getResources())
方法将授予所有请求的权限。要避免只授予所需的权限,即resource_video_capture
:
@Override
public void onPermissionRequest(final PermissionRequest request) {
final String[] requestedResources = request.getResources();
for (String r : requestedResources) {
if (r.equals(PermissionRequest.RESOURCE_VIDEO_CAPTURE)) {
request.grant(new String[]{PermissionRequest.RESOURCE_VIDEO_CAPTURE});
break;
}
}
}
表情测试 图片转文字 相框 拍摄图片
摄像头用于采集图像和影像信息,通过模块间的组合完成各种创意活动。 净重量:8.1g 体积:24×24×22mm 参数 分辨率:1280×720 像素大小:3.4um×3.4um 最大图像传输速率:全尺寸 @ 30fps 视场角:100° 镜片结构:4G+IR 焦比:2.97 有效焦距:2.4mm 功耗:100uA(待机)~240mW(工作) 电源:USB总线电源 抗跌落能力:1m 工作温度:-30
camera 对象提供对设备默认摄像头应用程序的访问。 方法: camera.getPicture 参数: cameraSuccess cameraError cameraOptions camera.getPicture 选择使用摄像头拍照,或从设备相册中获取一张照片。图片以base64编码的字符串或图片URI形式返回。 简单的范例: navigator.camera.getPicture( c
摄像头 打开摄像头 Tida.photo({ // 暂不支持参数传入 }, function (data) { if (data.errorCode) { console.error(data.errorCode, data.errorMessage); } else { var img = document.createElement('i
这是我第一次在Android上使用相机开发,我已经测试了我在链接中找到的相机演示https://thenewcircle.com/s/post/39/using__the_camera_api 但我对这段代码有一些问题:( 首先,这似乎有必要将这一行添加到Preview(上下文上下文)的代码中 (如果我不添加此测试,程序将崩溃) 就在这条线之前 因为setType()调用现在似乎被弃用了(这就是A
主要内容:第1步:加载OpenCV本机库,第2步:实例化视频捕获类,第3步:阅取帧,示例在本章中,我们将学习如何使用OpenCV使用系统摄像头捕获帧。包中的类包含使用相机捕获视频的类和方法。下面来一步一步学习如何捕捉帧 - 第1步:加载OpenCV本机库 在使用OpenCV库编写Java代码时,使用加载OpenCV本地库。加载OpenCV本机库,如下所示 - 第2步:实例化视频捕获类 使用本教程前面提到的函数来实例化类。 第3步:阅取帧 可以使用类的方法从相机读取帧。此方法接受类的对