Skip to content

Commit

Permalink
扫描页面添加权限请求获取
Browse files Browse the repository at this point in the history
  • Loading branch information
maning0303 committed Apr 22, 2019
1 parent 8225b9d commit 90be16b
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 11 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@
```

## 版本记录:
v2.0.1:
1.再次修复8.0-Only fullscreen ...错误

v2.0.0:
1.添加缩放控制器,可以缩放相机焦距
2.缩放控制器方向:左,右,下
Expand All @@ -129,10 +132,6 @@
v1.1.7:
1.图片识别二维码对图片进行压缩处理,防止OOM
2.优化界面显示
v1.1.6:
1.修复8.0-Only fullscreen opaque activities can request orientation错误
2.优化启动退出动画


## 感谢:
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/com/maning/zxingcodedemo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ protected void onCreate(Bundle savedInstanceState) {
editText = (EditText) findViewById(R.id.editText);
checkbox = (CheckBox) findViewById(R.id.checkbox);

requestCameraPerm();
}

public void requestCameraPerm() {
Expand All @@ -53,7 +54,7 @@ public void requestCameraPerm() {
}

public void scanCodeDefault(View view) {
requestCameraPerm();
//需要判断有没有权限
MNScanManager.startScan(this, new MNScanCallback() {
@Override
public void onActivityResult(int resultCode, Intent data) {
Expand Down
3 changes: 1 addition & 2 deletions libraryzxing/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@
<activity
android:name=".CaptureActivity"
android:screenOrientation="portrait"
android:theme="@style/MNScanCaptureTheme"
android:windowSoftInputMode="stateAlwaysHidden" />
android:theme="@style/MNScanCaptureTheme" />

</application>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@

package com.google.zxing.client.android;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Rect;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
Expand Down Expand Up @@ -65,6 +68,7 @@
public final class CaptureActivity extends Activity implements SurfaceHolder.Callback {

private static final String TAG = CaptureActivity.class.getSimpleName();
private static final int REQUEST_CODE_CAMERA = 10010;

private CameraManager cameraManager;
private CaptureActivityHandler handler;
Expand Down Expand Up @@ -193,7 +197,6 @@ public void run() {
@Override
public void run() {
btn_dialog_bg.setVisibility(View.GONE);
Log.i(TAG, "decodeQRCode:" + decodeQRCodeFromBitmap);
if (TextUtils.isEmpty(decodeQRCodeFromBitmap)) {
Toast.makeText(CaptureActivity.this, "未发现二维码", Toast.LENGTH_SHORT).show();
} else {
Expand All @@ -218,7 +221,6 @@ public void run() {
@Override
protected void onResume() {
super.onResume();
Log.i(TAG, "onResume");
// CameraManager must be initialized here, not in onCreate(). This is necessary because we don't
// want to open the camera driver and measure the screen size if we're going to show the help on
// first launch. That led to bugs where the scanning rectangle was the wrong size and partially
Expand Down Expand Up @@ -342,8 +344,17 @@ public void surfaceChanged(SurfaceHolder holder, int format, int width, int heig
}

private void initCamera(SurfaceHolder surfaceHolder) {
//检查相机权限
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
//没有相机权限
requestPermissions(new String[]{Manifest.permission.CAMERA}, REQUEST_CODE_CAMERA);
return;
}
}
if (surfaceHolder == null) {
displayFrameworkBugMessageAndExit("SurfaceHolder 不存在");
displayFrameworkBugMessageAndExit("初始化相机失败");
return;
}
if (cameraManager.isOpen()) {
Log.w(TAG, "initCamera() while already open -- late SurfaceView callback?");
Expand All @@ -356,12 +367,33 @@ private void initCamera(SurfaceHolder surfaceHolder) {
handler = new CaptureActivityHandler(this, decodeFormats, decodeHints, characterSet, cameraManager);
}
} catch (Exception e) {
displayFrameworkBugMessageAndExit("open camera fail:" + e.toString());
Log.e(TAG, "open camera fail:" + e.toString());
displayFrameworkBugMessageAndExit("初始化相机失败");
}
//刷新控制器
updateZoomController();
}


@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case REQUEST_CODE_CAMERA:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission Granted 授予权限
onResume();
} else {
// Permission Denied 权限被拒绝
displayFrameworkBugMessageAndExit("初始化相机失败,权限被拒绝");
}
break;
default:
break;
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults);

}

private void displayFrameworkBugMessageAndExit(String errorMessage) {
finishFailed(errorMessage);
}
Expand Down

0 comments on commit 90be16b

Please sign in to comment.