Skip to content

Commit

Permalink
Dev
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuriy Budiyev committed Jan 10, 2018
1 parent b4b92f9 commit 2d59b5f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ public void onPreviewFrame(byte[] data, Camera camera) {
return;
}
Decoder decoder = decoderWrapper.getDecoder();
if (decoder.isProcessing()) {
if (decoder.shouldSkipTask()) {
return;
}
Point imageSize = decoderWrapper.getImageSize();
Expand Down
16 changes: 9 additions & 7 deletions src/main/java/com/budiyev/android/codescanner/Decoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.SynchronousQueue;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
Expand All @@ -39,13 +39,14 @@
import com.google.zxing.Result;

final class Decoder {
private final BlockingQueue<DecodeTask> mDecodeQueue = new SynchronousQueue<>();
private static final int MAX_QUEUE_SIZE = 4;
private final BlockingQueue<DecodeTask> mDecodeQueue = new ArrayBlockingQueue<>(MAX_QUEUE_SIZE);
private final MultiFormatReader mReader;
private final DecoderThread mDecoderThread;
private final StateListener mStateListener;
private final Map<DecodeHintType, Object> mHints;
private volatile DecodeCallback mCallback;
private volatile boolean mProcessing;
private volatile boolean mProcessingResult;

public Decoder(@NonNull StateListener stateListener, @NonNull List<BarcodeFormat> formats,
@Nullable DecodeCallback callback) {
Expand Down Expand Up @@ -80,8 +81,8 @@ public void shutdown() {
mDecodeQueue.clear();
}

public boolean isProcessing() {
return mProcessing;
public boolean shouldSkipTask() {
return mProcessingResult || mDecodeQueue.remainingCapacity() == 0;
}

private final class DecoderThread extends Thread {
Expand All @@ -103,20 +104,21 @@ public void run() {
Result result = null;
try {
DecodeTask task = mDecodeQueue.take();
mProcessing = true;
mStateListener.onStateChanged(Decoder.State.DECODING);
result = task.decode(mReader);
} catch (ReaderException ignored) {
} finally {
if (result != null) {
mProcessingResult = true;
mDecodeQueue.clear();
if (mStateListener.onStateChanged(Decoder.State.DECODED)) {
DecodeCallback callback = mCallback;
if (callback != null) {
callback.onDecoded(result);
}
}
mProcessingResult = false;
}
mProcessing = false;
}
} catch (InterruptedException e) {
break;
Expand Down

0 comments on commit 2d59b5f

Please sign in to comment.