mirror of
https://github.com/zxing/zxing.git
synced 2025-03-05 20:48:51 -08:00
Better fix for concurrency issue
git-svn-id: https://zxing.googlecode.com/svn/trunk@1794 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
parent
c8166aec33
commit
87887ab18c
|
@ -30,7 +30,6 @@ import android.view.View;
|
|||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
/**
|
||||
* This view is overlaid on top of the camera preview. It adds the viewfinder rectangle and partial
|
||||
|
@ -53,8 +52,8 @@ public final class ViewfinderView extends View {
|
|||
private final int laserColor;
|
||||
private final int resultPointColor;
|
||||
private int scannerAlpha;
|
||||
private final AtomicReference<List<ResultPoint>> possibleResultPoints;
|
||||
private final AtomicReference<List<ResultPoint>> lastPossibleResultPoints;
|
||||
private List<ResultPoint> possibleResultPoints;
|
||||
private List<ResultPoint> lastPossibleResultPoints;
|
||||
|
||||
// This constructor is used when the class is built from an XML resource.
|
||||
public ViewfinderView(Context context, AttributeSet attrs) {
|
||||
|
@ -69,9 +68,8 @@ public final class ViewfinderView extends View {
|
|||
laserColor = resources.getColor(R.color.viewfinder_laser);
|
||||
resultPointColor = resources.getColor(R.color.possible_result_points);
|
||||
scannerAlpha = 0;
|
||||
possibleResultPoints = new AtomicReference<List<ResultPoint>>();
|
||||
lastPossibleResultPoints = new AtomicReference<List<ResultPoint>>();
|
||||
possibleResultPoints.set(new ArrayList<ResultPoint>(5));
|
||||
possibleResultPoints = new ArrayList<ResultPoint>(5);
|
||||
lastPossibleResultPoints = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -114,30 +112,34 @@ public final class ViewfinderView extends View {
|
|||
float scaleX = frame.width() / (float) previewFrame.width();
|
||||
float scaleY = frame.height() / (float) previewFrame.height();
|
||||
|
||||
List<ResultPoint> currentPossible = possibleResultPoints.get();
|
||||
List<ResultPoint> currentLast = lastPossibleResultPoints.get();
|
||||
List<ResultPoint> currentPossible = possibleResultPoints;
|
||||
List<ResultPoint> currentLast = lastPossibleResultPoints;
|
||||
if (currentPossible.isEmpty()) {
|
||||
lastPossibleResultPoints.set(null);
|
||||
lastPossibleResultPoints = null;
|
||||
} else {
|
||||
possibleResultPoints.set(new ArrayList<ResultPoint>(5));
|
||||
lastPossibleResultPoints.set(currentPossible);
|
||||
possibleResultPoints = new ArrayList<ResultPoint>(5);
|
||||
lastPossibleResultPoints = currentPossible;
|
||||
paint.setAlpha(CURRENT_POINT_OPACITY);
|
||||
paint.setColor(resultPointColor);
|
||||
synchronized (currentPossible) {
|
||||
for (ResultPoint point : currentPossible) {
|
||||
canvas.drawCircle(frame.left + (int) (point.getX() * scaleX),
|
||||
frame.top + (int) (point.getY() * scaleY),
|
||||
6.0f, paint);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (currentLast != null) {
|
||||
paint.setAlpha(CURRENT_POINT_OPACITY / 2);
|
||||
paint.setColor(resultPointColor);
|
||||
synchronized (currentLast) {
|
||||
for (ResultPoint point : currentLast) {
|
||||
canvas.drawCircle(frame.left + (int) (point.getX() * scaleX),
|
||||
frame.top + (int) (point.getY() * scaleY),
|
||||
3.0f, paint);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Request another update at the animation interval, but only repaint the laser line,
|
||||
// not the entire viewfinder mask.
|
||||
|
@ -161,11 +163,14 @@ public final class ViewfinderView extends View {
|
|||
}
|
||||
|
||||
public void addPossibleResultPoint(ResultPoint point) {
|
||||
List<ResultPoint> points = possibleResultPoints.get();
|
||||
List<ResultPoint> points = possibleResultPoints;
|
||||
synchronized (point) {
|
||||
points.add(point);
|
||||
if (points.size() > MAX_RESULT_POINTS) {
|
||||
int size = points.size();
|
||||
if (size > MAX_RESULT_POINTS) {
|
||||
// trim it
|
||||
points.subList(0, points.size() - MAX_RESULT_POINTS / 2).clear();
|
||||
points.subList(0, size - MAX_RESULT_POINTS / 2).clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue