2 * Copyright (C) 2008 ZXing authors
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package cz.cvut.fel.dce.barcodescanner;
19 import android.annotation.SuppressLint;
20 import android.content.Context;
21 import android.content.res.Resources;
22 import android.graphics.Bitmap;
23 import android.graphics.Canvas;
24 import android.graphics.Paint;
25 import android.graphics.Rect;
26 import android.util.AttributeSet;
27 import android.view.View;
29 import com.google.zxing.ResultPoint;
30 import cz.cvut.fel.dce.barcodescanner.camera.CameraManager;
32 import java.util.ArrayList;
33 import java.util.List;
36 * This view is overlaid on top of the camera preview. It adds the viewfinder rectangle and partial
37 * transparency outside it, as well as the laser scanner animation and result points.
39 * @author dswitkin@google.com (Daniel Switkin)
41 public final class ViewfinderView extends View {
43 private static final int[] SCANNER_ALPHA = {0, 64, 128, 192, 255, 192, 128, 64};
44 private static final long ANIMATION_DELAY = 80L;
45 private static final int CURRENT_POINT_OPACITY = 0xA0;
46 private static final int MAX_RESULT_POINTS = 20;
47 private static final int POINT_SIZE = 6;
49 private CameraManager cameraManager;
50 private final Paint paint;
51 private Bitmap resultBitmap;
52 private final int maskColor;
53 private final int resultColor;
54 private final int laserColor;
55 private final int resultPointColor;
56 private int scannerAlpha;
57 private List<ResultPoint> possibleResultPoints;
58 private List<ResultPoint> lastPossibleResultPoints;
60 // This constructor is used when the class is built from an XML resource.
61 public ViewfinderView(Context context, AttributeSet attrs) {
62 super(context, attrs);
64 // Initialize these once for performance rather than calling them every time in onDraw().
65 paint = new Paint(Paint.ANTI_ALIAS_FLAG);
66 Resources resources = getResources();
67 maskColor = resources.getColor(R.color.viewfinder_mask);
68 resultColor = resources.getColor(R.color.result_view);
69 laserColor = resources.getColor(R.color.viewfinder_laser);
70 resultPointColor = resources.getColor(R.color.possible_result_points);
72 possibleResultPoints = new ArrayList<>(5);
73 lastPossibleResultPoints = null;
76 public void setCameraManager(CameraManager cameraManager) {
77 this.cameraManager = cameraManager;
80 @SuppressLint("DrawAllocation")
82 public void onDraw(Canvas canvas) {
83 if (cameraManager == null) {
84 return; // not ready yet, early draw before done configuring
86 Rect frame = cameraManager.getFramingRect();
87 Rect previewFrame = cameraManager.getFramingRectInPreview();
88 if (frame == null || previewFrame == null) {
91 int width = canvas.getWidth();
92 int height = canvas.getHeight();
94 // Draw the exterior (i.e. outside the framing rect) darkened
95 paint.setColor(resultBitmap != null ? resultColor : maskColor);
96 canvas.drawRect(0, 0, width, frame.top, paint);
97 canvas.drawRect(0, frame.top, frame.left, frame.bottom + 1, paint);
98 canvas.drawRect(frame.right + 1, frame.top, width, frame.bottom + 1, paint);
99 canvas.drawRect(0, frame.bottom + 1, width, height, paint);
101 if (resultBitmap != null) {
102 // Draw the opaque result bitmap over the scanning rectangle
103 paint.setAlpha(CURRENT_POINT_OPACITY);
104 canvas.drawBitmap(resultBitmap, null, frame, paint);
107 // Draw a red "laser scanner" line through the middle to show decoding is active
108 paint.setColor(laserColor);
109 paint.setAlpha(SCANNER_ALPHA[scannerAlpha]);
110 scannerAlpha = (scannerAlpha + 1) % SCANNER_ALPHA.length;
111 int middle = frame.height() / 2 + frame.top;
112 canvas.drawRect(frame.left + 2, middle - 1, frame.right - 1, middle + 2, paint);
114 float scaleX = frame.width() / (float) previewFrame.width();
115 float scaleY = frame.height() / (float) previewFrame.height();
117 List<ResultPoint> currentPossible = possibleResultPoints;
118 List<ResultPoint> currentLast = lastPossibleResultPoints;
119 int frameLeft = frame.left;
120 int frameTop = frame.top;
121 if (currentPossible.isEmpty()) {
122 lastPossibleResultPoints = null;
124 possibleResultPoints = new ArrayList<>(5);
125 lastPossibleResultPoints = currentPossible;
126 paint.setAlpha(CURRENT_POINT_OPACITY);
127 paint.setColor(resultPointColor);
128 synchronized (currentPossible) {
129 for (ResultPoint point : currentPossible) {
130 canvas.drawCircle(frameLeft + (int) (point.getX() * scaleX),
131 frameTop + (int) (point.getY() * scaleY),
136 if (currentLast != null) {
137 paint.setAlpha(CURRENT_POINT_OPACITY / 2);
138 paint.setColor(resultPointColor);
139 synchronized (currentLast) {
140 float radius = POINT_SIZE / 2.0f;
141 for (ResultPoint point : currentLast) {
142 canvas.drawCircle(frameLeft + (int) (point.getX() * scaleX),
143 frameTop + (int) (point.getY() * scaleY),
149 // Request another update at the animation interval, but only repaint the laser line,
150 // not the entire viewfinder mask.
151 postInvalidateDelayed(ANIMATION_DELAY,
152 frame.left - POINT_SIZE,
153 frame.top - POINT_SIZE,
154 frame.right + POINT_SIZE,
155 frame.bottom + POINT_SIZE);
159 public void drawViewfinder() {
160 Bitmap resultBitmap = this.resultBitmap;
161 this.resultBitmap = null;
162 if (resultBitmap != null) {
163 resultBitmap.recycle();
169 * Draw a bitmap with the result points highlighted instead of the live scanning display.
171 * @param barcode An image of the decoded barcode.
173 public void drawResultBitmap(Bitmap barcode) {
174 resultBitmap = barcode;
178 public void addPossibleResultPoint(ResultPoint point) {
179 List<ResultPoint> points = possibleResultPoints;
180 synchronized (points) {
182 int size = points.size();
183 if (size > MAX_RESULT_POINTS) {
185 points.subList(0, size - MAX_RESULT_POINTS / 2).clear();