Skip to content

Commit

Permalink
提升Camera2稳定性,提升扫码速度
Browse files Browse the repository at this point in the history
  • Loading branch information
ailiwean committed Sep 13, 2020
1 parent a422878 commit b22f898
Show file tree
Hide file tree
Showing 15 changed files with 469 additions and 71 deletions.
2 changes: 1 addition & 1 deletion app/src/main/java/com/android/NBZxing/CusScanView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class CusScanView @JvmOverloads constructor(context: Context, attributeSet: Attr
* 5 ScanTypeConfig.ONE_DIMENSION 所有一维码格式
*/
override fun getScanType(): ScanTypeConfig {
return ScanTypeConfig.HIGH_FREQUENCY
return ScanTypeConfig.ONLY_QR_CODE
}

fun toParse(string: String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ class Camera2 extends CameraViewImpl {

@Override
public void onOpened(@NonNull CameraDevice camera) {

mCamera = camera;
mCallback.onCameraOpened();
startCaptureSession();
Expand All @@ -95,12 +94,14 @@ public void onDisconnected(@NonNull CameraDevice camera) {

@Override
public void onError(@NonNull CameraDevice camera, int error) {
Log.e(TAG, "onError: " + camera.getId() + " (" + error + ")");
restart();
mCamera = null;
}

};

boolean isTrue;

private final CameraCaptureSession.StateCallback mSessionCallback
= new CameraCaptureSession.StateCallback() {

Expand All @@ -113,18 +114,17 @@ public void onConfigured(@NonNull CameraCaptureSession session) {
updateAutoFocus();
updateFlash();
try {
mCaptureSession.setRepeatingRequest(mPreviewRequestBuilder.build(),
CaptureRequest request = mPreviewRequestBuilder.build();
mCaptureSession.setRepeatingRequest(request,
mCaptureCallback, null);
} catch (CameraAccessException e) {
Log.e(TAG, "Failed to start camera preview because it couldn't access camera", e);
} catch (IllegalStateException e) {
Log.e(TAG, "Failed to start camera preview.", e);
} catch (Exception e) {
restart();
}
}

@Override
public void onConfigureFailed(@NonNull CameraCaptureSession session) {
Log.e(TAG, "Failed to configure capture session.");
restart();
}

@Override
Expand All @@ -147,8 +147,8 @@ public void onPrecaptureRequired() {
mCaptureSession.capture(mPreviewRequestBuilder.build(), this, null);
mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER,
CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER_IDLE);
} catch (CameraAccessException e) {
Log.e(TAG, "Failed to run precapture sequence.", e);
} catch (Exception e) {
restart();
}
}

Expand Down Expand Up @@ -264,8 +264,7 @@ void setFacing(int facing) {
}
mFacing = facing;
if (isCameraOpened()) {
stop();
start();
restart();
}
}

Expand Down Expand Up @@ -314,8 +313,9 @@ void setAutoFocus(boolean autoFocus) {
try {
mCaptureSession.setRepeatingRequest(mPreviewRequestBuilder.build(),
mCaptureCallback, null);
} catch (CameraAccessException e) {
} catch (Exception e) {
mAutoFocus = !mAutoFocus; // Revert
restart();
}
}
}
Expand All @@ -339,8 +339,9 @@ void setFlash(int flash) {
try {
mCaptureSession.setRepeatingRequest(mPreviewRequestBuilder.build(),
mCaptureCallback, null);
} catch (CameraAccessException e) {
} catch (Exception e) {
mFlash = saved; // Revert
restart();
}
}
}
Expand Down Expand Up @@ -380,17 +381,22 @@ void toZoomMin() {
@Override
void setZoom(float percent) {
synchronized (Camera2.class) {
if (!isCameraOpened())
return;
try {
mPreviewRequestBuilder.set(CaptureRequest.SCALER_CROP_REGION, CameraHelper.getZoomRect(mCameraCharacteristics, percent));
mCaptureSession.setRepeatingRequest(mPreviewRequestBuilder.build(), mCaptureCallback, null);
} catch (Exception e) {
restart();
}
}
}

@Override
void lightOperator(boolean isOpen) {
synchronized (Camera2.class) {
if (!isCameraOpened())
return;
if (isOpen)
setFlash(Constants.FLASH_TORCH);
else setFlash(Constants.FLASH_OFF);
Expand Down Expand Up @@ -450,7 +456,7 @@ private boolean chooseCameraIdByFacing() {
// We treat it as facing back.
mFacing = Constants.FACING_BACK;
return true;
} catch (CameraAccessException e) {
} catch (Exception e) {
return false;
}
}
Expand Down Expand Up @@ -581,9 +587,8 @@ void startCaptureSession() {
, mYuvReader.getSurface()
),
mSessionCallback, null);
} catch (CameraAccessException ignored) {
stop();
start();
} catch (Exception ignored) {
restart();
}
}
}
Expand All @@ -600,7 +605,7 @@ private Size chooseOptimalSize() {
if (surfaceWidth == 0 || surfaceHeight == 0) {
try {
Thread.sleep(2000);
} catch (InterruptedException ignored) {
} catch (Exception ignored) {
} finally {
surfaceWidth = mPreview.getWidth();
surfaceHeight = mPreview.getHeight();
Expand Down Expand Up @@ -703,8 +708,8 @@ private void lockFocus() {
try {
mCaptureCallback.setState(PictureCaptureCallback.STATE_LOCKING);
mCaptureSession.capture(mPreviewRequestBuilder.build(), mCaptureCallback, null);
} catch (CameraAccessException e) {
Log.e(TAG, "Failed to lock focus.", e);
} catch (Exception e) {
restart();
}
}

Expand Down Expand Up @@ -763,8 +768,8 @@ public void onCaptureCompleted(@NonNull CameraCaptureSession session,
unlockFocus();
}
}, null);
} catch (CameraAccessException e) {
Log.e(TAG, "Cannot capture a still picture.", e);
} catch (Exception e) {
restart();
}
}

Expand All @@ -789,9 +794,19 @@ void unlockFocus() {
mCaptureSession.setRepeatingRequest(mPreviewRequestBuilder.build(), mCaptureCallback,
null);
mCaptureCallback.setState(PictureCaptureCallback.STATE_PREVIEW);
} catch (CameraAccessException e) {
Log.e(TAG, "Failed to restart camera preview.", e);
} catch (Exception e) {
restart();
}
}

private void restart() {
stop();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
start();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,22 +118,26 @@ public boolean offer(T t, long timeout, TimeUnit unit) throws InterruptedExcepti
*/
@Override
public T take() throws InterruptedException {
if (takeIndex++ % ratio == 0) {
T t = normalQueue.take();
if (t == null)
if (takeIndex++ % ratio != 0) {
T t = null;
if (normalQueue.size() != 0)
t = normalQueue.take();
else if (scaleQueue.size() != 0)
t = scaleQueue.take();
return t;
} else {
T t = scaleQueue.take();
if (t == null)
T t = null;
if (scaleQueue.size() != 0)
t = scaleQueue.take();
else if (normalQueue.size() != 0)
t = normalQueue.take();
return t;
}
}

@Override
public T poll(long timeout, TimeUnit unit) throws InterruptedException {
if (pollIndex++ % ratio == 0)
if (pollIndex++ % ratio != 0)
return normalQueue.poll(timeout, unit);
else return scaleQueue.poll(timeout, unit);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.ailiwean.core.able

import android.os.Handler
import android.os.HandlerThread
import android.util.Log
import com.ailiwean.core.Config
import com.ailiwean.core.TypeRunnable
import com.ailiwean.core.WorkThreadServer
Expand Down Expand Up @@ -47,8 +48,9 @@ class AbleManager private constructor(handler: Handler) : PixsValuesAble(handler
ableList.add(XQRScanZoomAble(handler))
ableList.add(XQRScanAbleRotate(handler))
ableList.add(LighSolveAble(handler))
// ableList.add(GrayscaleStrengAble(handler))
// ableList.add(XQRScanFastAble(handler))
// ableList.add(XQRScanAble(handler))
// ableList.add(GrayscaleStrengAble(handler))
// ableList.add(XQRScanFastAble(handler))
}

public override fun cusAction(data: ByteArray, dataWidth: Int, dataHeight: Int) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public GrayscaleStrengAble(Handler handler) {
protected void needParseDeploy(PlanarYUVLuminanceSource source) {
if (result != null)
return;

// if (!isNative)
// return;

i++;
if (i % 2 != 1) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import com.ailiwean.core.zxing.core.common.HybridBinarizer
import com.google.android.cameraview.AspectRatio
import com.google.android.cameraview.BaseCameraView
import com.google.android.cameraview.CameraView
import com.google.android.cameraview.R
import kotlinx.android.synthetic.*
import java.io.File
import java.lang.ref.WeakReference
Expand Down Expand Up @@ -228,7 +229,14 @@ abstract class FreeZxingView @JvmOverloads constructor(context: Context, attribu
override fun onCameraOpenBack(camera: CameraView) {
super.onCameraOpenBack(camera)
clearFindViewByIdCache()
LayoutInflater.from(context).inflate(provideFloorView(), this, true)
findViewById<View>(R.id.provideViewId)?.let {
removeView(it)
}
LayoutInflater.from(context).inflate(provideFloorView(), this, false)
.let {
it.id = R.id.provideViewId
addView(it)
}
cameraStartLaterConfig()
}

Expand Down
Loading

0 comments on commit b22f898

Please sign in to comment.