package com.byxc.scanning;
import android.Manifest;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.Display;
import android.view.Surface;
import android.view.Window;
import android.view.WindowManager;
import com.google.zxing.ResultMetadataType;
import com.google.zxing.ResultPoint;
import com.google.zxing.client.android.BeepManager;
import com.google.zxing.client.android.Intents;
import com.journeyapps.barcodescanner.BarcodeCallback;
import com.journeyapps.barcodescanner.BarcodeResult;
import com.journeyapps.barcodescanner.CameraPreview;
import com.journeyapps.barcodescanner.CaptureManager;
import com.journeyapps.barcodescanner.DecoratedBarcodeView;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;
/**
* 原CaptureManager不满足持续扫描的需求,所以进行修改
* Created by ypp on 2018/5/24.
*/
public class CustomerCaptureManager {
private static final String TAG = CaptureManager.class.getSimpleName();
private static int cameraPermissionReqCode = 250;
private Activity activity;
private DecoratedBarcodeView barcodeView;
private int orientationLock = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
private static final String SAVED_ORIENTATION_LOCK = "SAVED_ORIENTATION_LOCK";
private boolean returnBarcodeImagePath = false;
private boolean destroyed = false;
// Delay long enough that the beep can be played.
// TODO: play beep in background
private static final long DELAY_BEEP = 150;
// private InactivityTimer inactivityTimer;
private BeepManager beepManager;
private Handler handler;
private ScanningListener scanningListener;
private BarcodeCallback callback = new BarcodeCallback() {
@Override
public void barcodeResult(final BarcodeResult result) {
barcodeView.pause();
beepManager.playBeepSoundAndVibrate();
handler.postDelayed(new Runnable() {
@Override
public void run() {
returnResult(result);
}
}, DELAY_BEEP);
}
@Override
public void possibleResultPoints(List<ResultPoint> resultPoints) {
}
};
private final CameraPreview.StateListener stateListener = new CameraPreview.StateListener() {
@Override
public void previewSized() {
}
@Override
public void previewStarted() {
HLog.v(TAG,"previewStarted","预览开始");
}
@Override
public void previewStopped() {
HLog.v(TAG,"previewStarted","预览停止");
}
@Override
public void cameraError(Exception error) {
displayFrameworkBugMessageAndExit();
}
};
public CustomerCaptureManager(Activity activity, DecoratedBarcodeView barcodeView, ScanningListener scanningListener) {
this.activity = activity;
this.barcodeView = barcodeView;
this.scanningListener=scanningListener;
barcodeView.getBarcodeView().pause();//先停止,再重启
barcodeView.getBarcodeView().resume();
barcodeView.getBarcodeView().addStateListener(stateListener);
handler = new Handler();
// inactivityTimer = new InactivityTimer(activity, new Runnable() {
// @Override
// public void run() {
// Log.d(TAG, "Finishing due to inactivity");
// finish();
// }
// });
beepManager = new BeepManager(activity);
}
/**
* Perform initialization, according to preferences set in the intent.
*
* @param intent the intent containing the scanning preferences
* @param savedInstanceState saved state, containing orientation lock
*/
public void initializeFromIntent(Intent intent, Bundle savedInstanceState) {
Window window = activity.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
if (savedInstanceState != null) {
// If the screen was locked and unlocked again, we may start in a different orientation
// (even one not allowed by the manifest). In this case we restore the orientation we were
// previously locked to.
this.orientationLock = savedInstanceState.getInt(SAVED_ORIENTATION_LOCK, ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
}
if(intent != null) {
if (orientationLock == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
// Only lock the orientation if it's not locked to something else yet
boolean orientationLocked = intent.getBooleanExtra(Intents.Scan.ORIENTATION_LOCKED, true);
if (orientationLocked) {
lockOrientation();
}
}
if (Intents.Scan.ACTION.equals(intent.getAction())) {
barcodeView.initializeFromIntent(intent);
}
if (!intent.getBooleanExtra(Intents.Scan.BEEP_ENABLED, true)) {
beepManager.setBeepEnabled(false);
beepManager.updatePrefs();
}
if (intent.hasExtra(Intents.Scan.TIMEOUT)) {
Runnable runnable = new Runnable() {
@Override
public void run() {
returnResultTimeout();
}
};
handler.postDelayed(runnable, intent.getLongExtra(Intents.Scan.TIMEOUT, 0L));
}
if (intent.getBooleanExtra(Intents.Scan.BARCODE_IMAGE_ENABLED, false)) {
returnBarcodeImagePath = true;
}
}
}
/**
* Lock display to current orientation.
*/
protected void lockOrientation() {
// Only get the orientation if it's not locked to one yet.
if (this.orientationLock == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
// Adapted from http://stackoverflow.com/a/14565436
Display display = activity.getWindowManager().getDefaultDisplay();
int rotation = display.getRotation();
int baseOrientation = activity.getResources().getConfiguration().orientation;
int orientation = 0;
if (baseOrientation == Configuration.ORIENTATION_LANDSCAPE) {
if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_90) {
orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
} else {
orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
}
} else if (baseOrientation == Configuration.ORIENTATION_PORTRAIT) {
if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_270) {
orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
} else {
orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
}
}
this.orientationLock = orientation;
}
//noinspection ResourceType
activity.setRequestedOrientation(this.orientationLock);
}
/**
* Start decoding.
*/
public void decode() {
barcodeView.decodeSingle(callback);
}
/**
* Call from Activity#onResume().
*/
public void onResume() {
if(Build.VERSION.SDK_INT >= 23) {
openCameraWithPermission();