package com.tejia.lijin.app.ui.recommend;
|
|
import android.Manifest;
|
import android.content.DialogInterface;
|
import android.content.Intent;
|
import android.content.pm.FeatureInfo;
|
import android.content.pm.PackageManager;
|
import android.graphics.Color;
|
import android.net.Uri;
|
import android.os.Build;
|
import android.os.Bundle;
|
import android.os.Handler;
|
import android.provider.Settings;
|
import androidx.appcompat.app.AppCompatActivity;
|
import androidx.appcompat.app.AppCompatDelegate;
|
import androidx.appcompat.widget.AppCompatImageView;
|
import androidx.appcompat.widget.LinearLayoutCompat;
|
import android.util.Log;
|
import android.view.SurfaceHolder;
|
import android.view.SurfaceView;
|
import android.view.View;
|
import android.view.Window;
|
import android.view.WindowManager;
|
import android.widget.Toast;
|
|
import com.google.zxing.Result;
|
import com.tejia.lijin.app.R;
|
import com.tejia.lijin.app.ui.dialog.UnbindDialog;
|
import com.tejia.lijin.app.util.PermissionsActivity;
|
import com.tejia.lijin.app.util.PermissionsChecker;
|
import com.tejia.lijin.app.util.zxing.android.BeepManager;
|
import com.tejia.lijin.app.util.zxing.android.CaptureActivityHandler;
|
import com.tejia.lijin.app.util.zxing.android.InactivityTimer;
|
import com.tejia.lijin.app.util.zxing.bean.ZxingConfig;
|
import com.tejia.lijin.app.util.zxing.camera.CameraManager;
|
import com.tejia.lijin.app.util.zxing.common.Constant;
|
import com.tejia.lijin.app.util.zxing.decode.DecodeImgCallback;
|
import com.tejia.lijin.app.util.zxing.decode.DecodeImgThread;
|
import com.tejia.lijin.app.util.zxing.decode.ImageUtil;
|
import com.tejia.lijin.app.util.zxing.view.ViewfinderView;
|
|
import java.io.IOException;
|
|
|
/**
|
* @author: yzq
|
* @date: 2017/10/26 15:22
|
* @declare :扫一扫
|
*/
|
|
public class CaptureActivity extends AppCompatActivity implements SurfaceHolder.Callback, View.OnClickListener {
|
|
private static final String TAG = CaptureActivity.class.getSimpleName();
|
private ZxingConfig config;
|
private SurfaceView previewView;
|
// private ViewfinderView viewfinderView;
|
private ViewfinderView viewfinderView;
|
private AppCompatImageView backIv;
|
private LinearLayoutCompat flashLightLayout;
|
private LinearLayoutCompat albumLayout;
|
// private LinearLayoutCompat bottomLayout;
|
private boolean hasSurface;
|
private InactivityTimer inactivityTimer;
|
private BeepManager beepManager;
|
private CameraManager cameraManager;
|
private CaptureActivityHandler handler;
|
private SurfaceHolder surfaceHolder;
|
|
// 所需的全部权限
|
static final String[] PERMISSIONS = new String[]{
|
Manifest.permission.CAMERA
|
};
|
private static final int REQUEST_CODE = 1; // 请求码
|
private PermissionsChecker mPermissionsChecker; // 权限检测器
|
|
|
public ViewfinderView getViewfinderView() {
|
return viewfinderView;
|
}
|
|
public Handler getHandler() {
|
return handler;
|
}
|
|
public CameraManager getCameraManager() {
|
return cameraManager;
|
}
|
|
public void drawViewfinder() {
|
viewfinderView.drawViewfinder();
|
}
|
|
|
static {
|
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
|
}
|
|
@Override
|
protected void onCreate(Bundle savedInstanceState) {
|
super.onCreate(savedInstanceState);
|
// 保持Activity处于唤醒状态
|
Window window = getWindow();
|
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
window.setStatusBarColor(Color.BLACK);
|
}
|
|
/*先获取配置信息*/
|
try {
|
config = (ZxingConfig) getIntent().getExtras().get(Constant.INTENT_ZXING_CONFIG);
|
} catch (Exception e) {
|
|
Log.i("config", e.toString());
|
}
|
|
if (config == null) {
|
config = new ZxingConfig();
|
}
|
|
setContentView(R.layout.activity_capture);
|
mPermissionsChecker = new PermissionsChecker(this);
|
initView();
|
|
hasSurface = false;
|
|
inactivityTimer = new InactivityTimer(this);
|
beepManager = new BeepManager(this);
|
beepManager.setPlayBeep(config.isPlayBeep());
|
beepManager.setVibrate(config.isShake());
|
}
|
|
private void startPermissionsActivity() {
|
PermissionsActivity.startActivityForResult(this, REQUEST_CODE, PERMISSIONS);
|
}
|
|
private void initView() {
|
previewView = findViewById(R.id.preview_view);
|
previewView.setOnClickListener(this);
|
|
viewfinderView = findViewById(R.id.viewfinder_view);
|
viewfinderView.setOnClickListener(this);
|
|
backIv = findViewById(R.id.backIv);
|
backIv.setOnClickListener(this);
|
|
|
flashLightLayout = findViewById(R.id.flashLightLayout);
|
flashLightLayout.setOnClickListener(this);
|
albumLayout = findViewById(R.id.albumLayout);
|
albumLayout.setOnClickListener(this);
|
// bottomLayout = (LinearLayoutCompat) findViewById(R.id.bottomLayout);
|
|
|
// switchVisibility(bottomLayout, config.isShowbottomLayout());
|
switchVisibility(flashLightLayout, config.isShowFlashLight());
|
switchVisibility(albumLayout, config.isShowAlbum());
|
|
/*有闪光灯就显示手电筒按钮 否则不显示*/
|
if (isSupportCameraLedFlash(getPackageManager())) {
|
flashLightLayout.setVisibility(View.VISIBLE);
|
} else {
|
flashLightLayout.setVisibility(View.GONE);
|
}
|
findViewById(R.id.albumIv).setOnClickListener(this);
|
}
|
|
/**
|
* @param pm
|
* @return 是否有闪光灯
|
*/
|
public static boolean isSupportCameraLedFlash(PackageManager pm) {
|
if (pm != null) {
|
FeatureInfo[] features = pm.getSystemAvailableFeatures();
|
if (features != null) {
|
for (FeatureInfo f : features) {
|
if (f != null && PackageManager.FEATURE_CAMERA_FLASH.equals(f.name)) {
|
return true;
|
}
|
}
|
}
|
}
|
return false;
|
}
|
|
/**
|
* @param flashState 切换闪光灯图片
|
*/
|
public void switchFlashImg(int flashState) {
|
|
// if (flashState == Constant.FLASH_OPEN) {
|
// flashLightIv.setImageResource(R.drawable.ic_open);
|
// flashLightTv.setText("关闭闪光灯");
|
// } else {
|
// flashLightIv.setImageResource(R.drawable.ic_close);
|
// flashLightTv.setText("打开闪光灯");
|
// }
|
|
}
|
|
/**
|
* @param rawResult 返回的扫描结果
|
*/
|
public void handleDecode(Result rawResult) {
|
|
inactivityTimer.onActivity();
|
|
beepManager.playBeepSoundAndVibrate();
|
|
Intent intent = getIntent();
|
intent.putExtra(Constant.CODED_CONTENT, rawResult.getText());
|
setResult(RESULT_OK, intent);
|
this.finish();
|
|
|
}
|
|
|
private void switchVisibility(View view, boolean b) {
|
if (b) {
|
view.setVisibility(View.VISIBLE);
|
} else {
|
view.setVisibility(View.GONE);
|
}
|
}
|
|
|
@Override
|
protected void onResume() {
|
super.onResume();
|
if (mPermissionsChecker.lacksPermissions(PERMISSIONS)) {
|
startPermissionsActivity();
|
} else {
|
cameraManager = new CameraManager(getApplication());
|
viewfinderView.setCameraManager(cameraManager);
|
handler = null;
|
|
surfaceHolder = previewView.getHolder();
|
if (hasSurface) {
|
|
initCamera(surfaceHolder);
|
} else {
|
// 重置callback,等待surfaceCreated()来初始化camera
|
surfaceHolder.addCallback(this);
|
}
|
|
beepManager.updatePrefs();
|
inactivityTimer.onResume();
|
}
|
}
|
|
private void initCamera(SurfaceHolder surfaceHolder) {
|
if (surfaceHolder == null) {
|
throw new IllegalStateException("No SurfaceHolder provided");
|
}
|
if (cameraManager.isOpen()) {
|
return;
|
}
|
try {
|
// 打开Camera硬件设备
|
cameraManager.openDriver(surfaceHolder);
|
// 创建一个handler来打开预览,并抛出一个运行时异常
|
if (handler == null) {
|
handler = new CaptureActivityHandler(this, cameraManager);
|
}
|
} catch (IOException ioe) {
|
Log.w(TAG, ioe);
|
displayFrameworkBugMessageAndExit();
|
} catch (RuntimeException e) {
|
Log.w(TAG, "Unexpected error initializing camera", e);
|
displayFrameworkBugMessageAndExit();
|
}
|
}
|
|
private void displayFrameworkBugMessageAndExit() {
|
final UnbindDialog.Builder builder = new UnbindDialog.Builder(this);
|
builder.setMessage("相机权限已被拒绝如要正常使用,请设置相机权限");
|
|
// 拒绝, 退出应用
|
builder.setNegativeButton("不用了", new DialogInterface.OnClickListener() {
|
@Override
|
public void onClick(DialogInterface dialog, int which) {
|
dialog.dismiss();
|
finish();
|
}
|
});
|
|
builder.setPositiveButton("去设置", new DialogInterface.OnClickListener() {
|
@Override
|
public void onClick(DialogInterface dialog, int which) {
|
dialog.dismiss();
|
startAppSettings();//打开设置
|
}
|
});
|
|
builder.create().show();
|
}
|
|
@Override
|
protected void onPause() {
|
if (!mPermissionsChecker.lacksPermissions(PERMISSIONS)) {
|
if (handler != null) {
|
handler.quitSynchronously();
|
handler = null;
|
}
|
inactivityTimer.onPause();
|
beepManager.close();
|
cameraManager.closeDriver();
|
|
if (!hasSurface) {
|
|
surfaceHolder.removeCallback(this);
|
}
|
}
|
super.onPause();
|
}
|
|
@Override
|
protected void onDestroy() {
|
inactivityTimer.shutdown();
|
super.onDestroy();
|
}
|
|
@Override
|
public void surfaceCreated(SurfaceHolder holder) {
|
if (!hasSurface) {
|
hasSurface = true;
|
initCamera(holder);
|
}
|
}
|
|
|
@Override
|
public void surfaceDestroyed(SurfaceHolder holder) {
|
hasSurface = false;
|
}
|
|
@Override
|
public void surfaceChanged(SurfaceHolder holder, int format, int width,
|
int height) {
|
|
}
|
|
@Override
|
public void onClick(View view) {
|
|
int id = view.getId();
|
if (id == R.id.flashLightLayout) {
|
/*切换闪光灯*/
|
cameraManager.switchFlashLight(handler);
|
} else if (id == R.id.albumIv) {
|
/*打开相册*/
|
Intent intent = new Intent();
|
intent.setAction(Intent.ACTION_PICK);
|
intent.setType("image/*");
|
startActivityForResult(intent, Constant.REQUEST_IMAGE);
|
} else if (id == R.id.backIv) {
|
finish();
|
}
|
|
|
}
|
|
|
@Override
|
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
super.onActivityResult(requestCode, resultCode, data);
|
|
if (requestCode == Constant.REQUEST_IMAGE && resultCode == RESULT_OK) {
|
String path = ImageUtil.getImageAbsolutePath(this, data.getData());
|
new DecodeImgThread(path, new DecodeImgCallback() {
|
@Override
|
public void onImageDecodeSuccess(Result result) {
|
handleDecode(result);
|
}
|
|
@Override
|
public void onImageDecodeFailed() {
|
Toast.makeText(CaptureActivity.this, "抱歉,解析失败,换个图片试试.", Toast.LENGTH_SHORT).show();
|
}
|
}).run();
|
}
|
// 拒绝时, 关闭页面, 缺少主要权限, 无法运行
|
if (requestCode == REQUEST_CODE && resultCode == PermissionsActivity.PERMISSIONS_DENIED) {
|
finish();
|
}
|
}
|
|
// 启动应用的设置
|
private void startAppSettings() {
|
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
|
intent.setData(Uri.parse("package:" + getPackageName()));
|
startActivity(intent);
|
}
|
|
}
|