赞
踩
AndroidManifest.xml 中必不可少的权限,摄像头、网络、写入本地化存储
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
package edu.sfsu.cs.orange.ocr; import android.Manifest; import android.app.Activity; import android.content.Intent; import android.content.pm.PackageManager; import android.os.Build; import android.os.Bundle; import edu.sfsu.cs.orange.ocr.utils.PermissionHelper; public class WelcomeActivity extends Activity { /********** is above android 6.0 request permission*******************************************************/ static final int PERMISSION_REQUEST_CODE = 5; private String[] permissions= new String[]{Manifest.permission.CAMERA, Manifest.permission.INTERNET, Manifest.permission.WRITE_EXTERNAL_STORAGE}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if(PermissionHelper.checkPermission(this,permissions)){ startActivity(new Intent(this, CaptureActivity.class)); }else{ PermissionHelper.requestPermission(this,permissions , PERMISSION_REQUEST_CODE); } }else{ startActivity(new Intent(this, CaptureActivity.class)); } } @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (requestCode == PERMISSION_REQUEST_CODE) { boolean isAllGranted = true; for (int grant : grantResults) { if (grant != PackageManager.PERMISSION_GRANTED) { isAllGranted = false; break; } } if (isAllGranted) { startActivity(new Intent(this, CaptureActivity.class)); } else { PermissionHelper.requestPermission(this, permissions, PERMISSION_REQUEST_CODE); } } } }
package edu.sfsu.cs.orange.ocr.utils; import android.annotation.TargetApi; import android.app.Activity; import android.content.pm.PackageManager; import android.os.Build; public class PermissionHelper { @TargetApi(Build.VERSION_CODES.M) public static void requestPermission(Activity context, String[] permissions, int requestCode) { for (int i = 0; i < permissions.length; i++) { if (context.checkSelfPermission(permissions[i]) != PackageManager.PERMISSION_GRANTED) { context.requestPermissions(new String[]{permissions[i]}, requestCode); } } } @TargetApi(Build.VERSION_CODES.M) public static boolean checkPermission(Activity context, String[] permissions) { for (String permission : permissions) { if (context.checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) { return false; } } return true; } }
具体项目地址:持续更新
https://github.com/jambestwick/android-ocr
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。