优化Camera初始化相关策略,减少出现卡顿的可能性(#64,#65)
This commit is contained in:
@@ -110,7 +110,7 @@ public class CaptureHandler extends Handler implements ResultPointCallback {
|
||||
quit.sendToTarget();
|
||||
try {
|
||||
// Wait at most half a second; should be enough time, and onPause() will timeout quickly
|
||||
decodeThread.join(500L);
|
||||
decodeThread.join(100L);
|
||||
} catch (InterruptedException e) {
|
||||
// continue
|
||||
}
|
||||
@@ -198,4 +198,4 @@ public class CaptureHandler extends Handler implements ResultPointCallback {
|
||||
public void setSupportAutoZoom(boolean supportAutoZoom) {
|
||||
isSupportAutoZoom = supportAutoZoom;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -47,7 +47,7 @@ import androidx.fragment.app.Fragment;
|
||||
/**
|
||||
* @author <a href="mailto:jenly1314@gmail.com">Jenly</a>
|
||||
*/
|
||||
public class CaptureHelper implements CaptureLifecycle,CaptureTouchEvent,CaptureManager {
|
||||
public class CaptureHelper implements CaptureLifecycle,CaptureTouchEvent,CaptureManager, SurfaceHolder.Callback {
|
||||
|
||||
public static final String TAG = CaptureHelper.class.getSimpleName();
|
||||
|
||||
@@ -65,7 +65,6 @@ public class CaptureHelper implements CaptureLifecycle,CaptureTouchEvent,Capture
|
||||
|
||||
private ViewfinderView viewfinderView;
|
||||
private SurfaceHolder surfaceHolder;
|
||||
private SurfaceHolder.Callback callback;
|
||||
private View ivTorch;
|
||||
|
||||
private Collection<BarcodeFormat> decodeFormats;
|
||||
@@ -145,6 +144,8 @@ public class CaptureHelper implements CaptureLifecycle,CaptureTouchEvent,Capture
|
||||
*/
|
||||
private OnCaptureCallback onCaptureCallback;
|
||||
|
||||
private boolean hasCameraFlash;
|
||||
|
||||
/**
|
||||
* use {@link #CaptureHelper(Fragment, SurfaceView, ViewfinderView, View)}
|
||||
* @param fragment
|
||||
@@ -193,49 +194,8 @@ public class CaptureHelper implements CaptureLifecycle,CaptureTouchEvent,Capture
|
||||
beepManager = new BeepManager(activity);
|
||||
ambientLightManager = new AmbientLightManager(activity);
|
||||
|
||||
cameraManager = new CameraManager(activity);
|
||||
cameraManager.setFullScreenScan(isFullScreenScan);
|
||||
cameraManager.setFramingRectRatio(framingRectRatio);
|
||||
cameraManager.setFramingRectVerticalOffset(framingRectVerticalOffset);
|
||||
cameraManager.setFramingRectHorizontalOffset(framingRectHorizontalOffset);
|
||||
if(ivTorch !=null && activity.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)){
|
||||
ivTorch.setOnClickListener(v -> cameraManager.setTorch(!ivTorch.isSelected()));
|
||||
cameraManager.setOnSensorListener((torch, tooDark, ambientLightLux) -> {
|
||||
if(tooDark){
|
||||
if(ivTorch.getVisibility() != View.VISIBLE){
|
||||
ivTorch.setVisibility(View.VISIBLE);
|
||||
}
|
||||
}else if(!torch){
|
||||
if(ivTorch.getVisibility() == View.VISIBLE){
|
||||
ivTorch.setVisibility(View.INVISIBLE);
|
||||
}
|
||||
}
|
||||
});
|
||||
cameraManager.setOnTorchListener(torch -> ivTorch.setSelected(torch));
|
||||
|
||||
}
|
||||
callback = new SurfaceHolder.Callback() {
|
||||
@Override
|
||||
public void surfaceCreated(SurfaceHolder holder) {
|
||||
if (holder == null) {
|
||||
Log.e(TAG, "*** WARNING *** surfaceCreated() gave us a null surface!");
|
||||
}
|
||||
if (!hasSurface) {
|
||||
hasSurface = true;
|
||||
initCamera(holder);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void surfaceDestroyed(SurfaceHolder holder) {
|
||||
hasSurface = false;
|
||||
}
|
||||
};
|
||||
hasCameraFlash = activity.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
|
||||
initCameraManager();
|
||||
|
||||
onCaptureListener = (result, barcode, scaleFactor) -> {
|
||||
inactivityTimer.onActivity();
|
||||
@@ -257,17 +217,15 @@ public class CaptureHelper implements CaptureLifecycle,CaptureTouchEvent,Capture
|
||||
@Override
|
||||
public void onResume(){
|
||||
beepManager.updatePrefs();
|
||||
ambientLightManager.start(cameraManager);
|
||||
|
||||
inactivityTimer.onResume();
|
||||
|
||||
surfaceHolder.addCallback(callback);
|
||||
|
||||
if (hasSurface) {
|
||||
initCamera(surfaceHolder);
|
||||
} else {
|
||||
surfaceHolder.addCallback(callback);
|
||||
surfaceHolder.addCallback(this);
|
||||
}
|
||||
ambientLightManager.start(cameraManager);
|
||||
}
|
||||
|
||||
|
||||
@@ -282,7 +240,7 @@ public class CaptureHelper implements CaptureLifecycle,CaptureTouchEvent,Capture
|
||||
beepManager.close();
|
||||
cameraManager.closeDriver();
|
||||
if (!hasSurface) {
|
||||
surfaceHolder.removeCallback(callback);
|
||||
surfaceHolder.removeCallback(this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -327,6 +285,35 @@ public class CaptureHelper implements CaptureLifecycle,CaptureTouchEvent,Capture
|
||||
return false;
|
||||
}
|
||||
|
||||
private void initCameraManager(){
|
||||
cameraManager = new CameraManager(activity);
|
||||
cameraManager.setFullScreenScan(isFullScreenScan);
|
||||
cameraManager.setFramingRectRatio(framingRectRatio);
|
||||
cameraManager.setFramingRectVerticalOffset(framingRectVerticalOffset);
|
||||
cameraManager.setFramingRectHorizontalOffset(framingRectHorizontalOffset);
|
||||
if(ivTorch !=null && hasCameraFlash){
|
||||
ivTorch.setOnClickListener(v -> {
|
||||
if(cameraManager!=null){
|
||||
cameraManager.setTorch(!ivTorch.isSelected());
|
||||
}
|
||||
});
|
||||
cameraManager.setOnSensorListener((torch, tooDark, ambientLightLux) -> {
|
||||
if(tooDark){
|
||||
if(ivTorch.getVisibility() != View.VISIBLE){
|
||||
ivTorch.setVisibility(View.VISIBLE);
|
||||
}
|
||||
}else if(!torch){
|
||||
if(ivTorch.getVisibility() == View.VISIBLE){
|
||||
ivTorch.setVisibility(View.INVISIBLE);
|
||||
}
|
||||
}
|
||||
});
|
||||
cameraManager.setOnTorchListener(torch -> ivTorch.setSelected(torch));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 初始化Camera
|
||||
* @param surfaceHolder
|
||||
@@ -357,6 +344,27 @@ public class CaptureHelper implements CaptureLifecycle,CaptureTouchEvent,Capture
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void surfaceCreated(SurfaceHolder holder) {
|
||||
if (holder == null) {
|
||||
Log.e(TAG, "*** WARNING *** surfaceCreated() gave us a null surface!");
|
||||
}
|
||||
if (!hasSurface) {
|
||||
hasSurface = true;
|
||||
initCamera(holder);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void surfaceDestroyed(SurfaceHolder holder) {
|
||||
hasSurface = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理变焦缩放
|
||||
* @param isZoomIn
|
||||
@@ -485,7 +493,8 @@ public class CaptureHelper implements CaptureLifecycle,CaptureTouchEvent,Capture
|
||||
onResult(result);
|
||||
}
|
||||
|
||||
/**
|
||||
/**';, mnb
|
||||
*
|
||||
* 接收扫码结果,想支持连扫时,可将{@link #continuousScan(boolean)}设置为{@code true}
|
||||
* 如果{@link #isContinuousScan}支持连扫,则默认重启扫码和解码器;当连扫逻辑太复杂时,
|
||||
* 请将{@link #autoRestartPreviewAndDecode(boolean)}设置为{@code false},并手动调用{@link #restartPreviewAndDecode()}
|
||||
|
||||
@@ -50,7 +50,7 @@ final class InactivityTimer {
|
||||
onActivity();
|
||||
}
|
||||
|
||||
synchronized void onActivity() {
|
||||
void onActivity() {
|
||||
cancel();
|
||||
inactivityTask = new InactivityAsyncTask(activity);
|
||||
try {
|
||||
@@ -60,7 +60,7 @@ final class InactivityTimer {
|
||||
}
|
||||
}
|
||||
|
||||
synchronized void onPause() {
|
||||
void onPause() {
|
||||
cancel();
|
||||
if (registered) {
|
||||
activity.unregisterReceiver(powerStatusReceiver);
|
||||
@@ -70,7 +70,7 @@ final class InactivityTimer {
|
||||
}
|
||||
}
|
||||
|
||||
synchronized void onResume() {
|
||||
void onResume() {
|
||||
if (registered) {
|
||||
Log.w(TAG, "PowerStatusReceiver was already registered?");
|
||||
} else {
|
||||
@@ -80,7 +80,7 @@ final class InactivityTimer {
|
||||
onActivity();
|
||||
}
|
||||
|
||||
private synchronized void cancel() {
|
||||
private void cancel() {
|
||||
AsyncTask<?,?,?> task = inactivityTask;
|
||||
if (task != null) {
|
||||
task.cancel(true);
|
||||
|
||||
@@ -90,7 +90,7 @@ public final class CameraManager {
|
||||
* @param holder The surface object which the camera will draw preview frames into.
|
||||
* @throws IOException Indicates the camera driver failed to open.
|
||||
*/
|
||||
public synchronized void openDriver(SurfaceHolder holder) throws IOException {
|
||||
public void openDriver(SurfaceHolder holder) throws IOException {
|
||||
OpenCamera theCamera = camera;
|
||||
if (theCamera == null) {
|
||||
theCamera = OpenCameraInterface.open(requestedCameraId);
|
||||
@@ -147,7 +147,7 @@ public final class CameraManager {
|
||||
/**
|
||||
* Closes the camera driver if still in use.
|
||||
*/
|
||||
public synchronized void closeDriver() {
|
||||
public void closeDriver() {
|
||||
if (camera != null) {
|
||||
camera.getCamera().release();
|
||||
camera = null;
|
||||
@@ -161,7 +161,7 @@ public final class CameraManager {
|
||||
/**
|
||||
* Asks the camera hardware to begin drawing preview frames to the screen.
|
||||
*/
|
||||
public synchronized void startPreview() {
|
||||
public void startPreview() {
|
||||
OpenCamera theCamera = camera;
|
||||
if (theCamera != null && !previewing) {
|
||||
theCamera.getCamera().startPreview();
|
||||
@@ -173,7 +173,7 @@ public final class CameraManager {
|
||||
/**
|
||||
* Tells the camera to stop drawing preview frames.
|
||||
*/
|
||||
public synchronized void stopPreview() {
|
||||
public void stopPreview() {
|
||||
if (autoFocusManager != null) {
|
||||
autoFocusManager.stop();
|
||||
autoFocusManager = null;
|
||||
|
||||
Reference in New Issue
Block a user