Skip to content

Commit aa4fca5

Browse files
Andre Ippischrkistner
Andre Ippisch
authored andcommitted
Let visibility of "laser scanner" be set (#503)
* Add laser visibility attribute * Add laser visibility attribute usage to sample project - set to true to stick with the current behavior - included for didactic reasons * Add laser visibility attribute to ViewfinderView and set it accordingly * Draw "laser scanner" only if wanted * Let laser visibility be changed programmatically * Add sample code to show how laser visibility can be changed programmatically
1 parent d22b581 commit aa4fca5

File tree

4 files changed

+23
-6
lines changed

4 files changed

+23
-6
lines changed

sample/src/main/java/example/zxing/CustomScannerActivity.java

+5
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ protected void onCreate(Bundle savedInstanceState) {
4848
capture.decode();
4949

5050
changeMaskColor(null);
51+
changeLaserVisibility(true);
5152
}
5253

5354
@Override
@@ -102,6 +103,10 @@ public void changeMaskColor(View view) {
102103
viewfinderView.setMaskColor(color);
103104
}
104105

106+
public void changeLaserVisibility(boolean visible) {
107+
viewfinderView.setLaserVisibility(visible);
108+
}
109+
105110
@Override
106111
public void onTorchOn() {
107112
switchFlashlightButton.setText(R.string.turn_off_flashlight);

sample/src/main/res/layout/custom_barcode_scanner.xml

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
app:zxing_possible_result_points="@color/zxing_custom_possible_result_points"
1818
app:zxing_result_view="@color/zxing_custom_result_view"
1919
app:zxing_viewfinder_laser="@color/zxing_custom_viewfinder_laser"
20+
app:zxing_viewfinder_laser_visibility="true"
2021
app:zxing_viewfinder_mask="@color/zxing_custom_viewfinder_mask"/>
2122

2223
<TextView

zxing-android-embedded/res/values/zxing_attrs.xml

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<attr name="zxing_result_view" format="color"/>
2222
<attr name="zxing_viewfinder_laser" format="color"/>
2323
<attr name="zxing_viewfinder_mask" format="color"/>
24+
<attr name="zxing_viewfinder_laser_visibility" format="boolean"/>
2425
</declare-styleable>
2526

2627
</resources>

zxing-android-embedded/src/com/journeyapps/barcodescanner/ViewfinderView.java

+16-6
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public class ViewfinderView extends View {
5454
protected final int resultColor;
5555
protected final int laserColor;
5656
protected final int resultPointColor;
57+
protected boolean laserVisibility;
5758
protected int scannerAlpha;
5859
protected List<ResultPoint> possibleResultPoints;
5960
protected List<ResultPoint> lastPossibleResultPoints;
@@ -84,6 +85,8 @@ public ViewfinderView(Context context, AttributeSet attrs) {
8485
resources.getColor(R.color.zxing_viewfinder_laser));
8586
this.resultPointColor = attributes.getColor(R.styleable.zxing_finder_zxing_possible_result_points,
8687
resources.getColor(R.color.zxing_possible_result_points));
88+
this.laserVisibility = attributes.getBoolean(R.styleable.zxing_finder_zxing_viewfinder_laser_visibility,
89+
true);
8790

8891
attributes.recycle();
8992

@@ -160,13 +163,16 @@ public void onDraw(Canvas canvas) {
160163
paint.setAlpha(CURRENT_POINT_OPACITY);
161164
canvas.drawBitmap(resultBitmap, null, frame, paint);
162165
} else {
166+
// If wanted, draw a red "laser scanner" line through the middle to show decoding is active
167+
if (laserVisibility) {
168+
paint.setColor(laserColor);
163169

164-
// Draw a red "laser scanner" line through the middle to show decoding is active
165-
paint.setColor(laserColor);
166-
paint.setAlpha(SCANNER_ALPHA[scannerAlpha]);
167-
scannerAlpha = (scannerAlpha + 1) % SCANNER_ALPHA.length;
168-
final int middle = frame.height() / 2 + frame.top;
169-
canvas.drawRect(frame.left + 2, middle - 1, frame.right - 1, middle + 2, paint);
170+
paint.setAlpha(SCANNER_ALPHA[scannerAlpha]);
171+
scannerAlpha = (scannerAlpha + 1) % SCANNER_ALPHA.length;
172+
173+
final int middle = frame.height() / 2 + frame.top;
174+
canvas.drawRect(frame.left + 2, middle - 1, frame.right - 1, middle + 2, paint);
175+
}
170176

171177
final float scaleX = this.getWidth() / (float) previewSize.width;
172178
final float scaleY = this.getHeight() / (float) previewSize.height;
@@ -247,4 +253,8 @@ public void addPossibleResultPoint(ResultPoint point) {
247253
public void setMaskColor(int maskColor) {
248254
this.maskColor = maskColor;
249255
}
256+
257+
public void setLaserVisibility(boolean visible) {
258+
this.laserVisibility = visible;
259+
}
250260
}

0 commit comments

Comments
 (0)