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 com.google.zxing.client.android;
19 import com.google.zxing.ResultPoint;
20 import com.google.zxing.client.android.camera.CameraManager;
22 import android.content.Context;
23 import android.content.res.Resources;
24 import android.graphics.Bitmap;
25 import android.graphics.Canvas;
26 import android.graphics.Paint;
27 import android.graphics.Rect;
28 import android.util.AttributeSet;
29 import android.view.View;
31 import java.util.Collection;
32 import java.util.HashSet;
35 * This view is overlaid on top of the camera preview. It adds the viewfinder rectangle and partial
36 * transparency outside it, as well as the laser scanner animation and result points.
38 * @author dswitkin@google.com (Daniel Switkin)
40 public final class ViewfinderView extends View {
42 private static final int[] SCANNER_ALPHA = {0, 64, 128, 192, 255, 192, 128, 64};
43 private static final long ANIMATION_DELAY = 100L;
44 private static final int OPAQUE = 0xFF;
46 private final Paint paint;
47 private Bitmap resultBitmap;
48 private final int maskColor;
49 private final int resultColor;
50 private final int frameColor;
51 private final int laserColor;
52 private final int resultPointColor;
53 private int scannerAlpha;
54 private Collection<ResultPoint> possibleResultPoints;
55 private Collection<ResultPoint> lastPossibleResultPoints;
57 // This constructor is used when the class is built from an XML resource.
58 public ViewfinderView(Context context, AttributeSet attrs) {
59 super(context, attrs);
61 // Initialize these once for performance rather than calling them every time in onDraw().
63 Resources resources = getResources();
64 maskColor = resources.getColor(R.color.viewfinder_mask);
65 resultColor = resources.getColor(R.color.result_view);
66 frameColor = resources.getColor(R.color.viewfinder_frame);
67 laserColor = resources.getColor(R.color.viewfinder_laser);
68 resultPointColor = resources.getColor(R.color.possible_result_points);
70 possibleResultPoints = new HashSet<ResultPoint>(5);
74 public void onDraw(Canvas canvas) {
75 Rect frame = CameraManager.get().getFramingRect();
79 int width = canvas.getWidth();
80 int height = canvas.getHeight();
82 // Draw the exterior (i.e. outside the framing rect) darkened
83 paint.setColor(resultBitmap != null ? resultColor : maskColor);
84 canvas.drawRect(0, 0, width, frame.top, paint);
85 canvas.drawRect(0, frame.top, frame.left, frame.bottom + 1, paint);
86 canvas.drawRect(frame.right + 1, frame.top, width, frame.bottom + 1, paint);
87 canvas.drawRect(0, frame.bottom + 1, width, height, paint);
89 if (resultBitmap != null) {
90 // Draw the opaque result bitmap over the scanning rectangle
91 paint.setAlpha(OPAQUE);
92 canvas.drawBitmap(resultBitmap, frame.left, frame.top, paint);
95 // Draw a two pixel solid black border inside the framing rect
96 paint.setColor(frameColor);
97 canvas.drawRect(frame.left, frame.top, frame.right + 1, frame.top + 2, paint);
98 canvas.drawRect(frame.left, frame.top + 2, frame.left + 2, frame.bottom - 1, paint);
99 canvas.drawRect(frame.right - 1, frame.top, frame.right + 1, frame.bottom - 1, paint);
100 canvas.drawRect(frame.left, frame.bottom - 1, frame.right + 1, frame.bottom + 1, paint);
102 // Draw a red "laser scanner" line through the middle to show decoding is active
103 paint.setColor(laserColor);
104 paint.setAlpha(SCANNER_ALPHA[scannerAlpha]);
105 scannerAlpha = (scannerAlpha + 1) % SCANNER_ALPHA.length;
106 int middle = frame.height() / 2 + frame.top;
107 canvas.drawRect(frame.left + 2, middle - 1, frame.right - 1, middle + 2, paint);
109 Collection<ResultPoint> currentPossible = possibleResultPoints;
110 Collection<ResultPoint> currentLast = lastPossibleResultPoints;
111 if (currentPossible.isEmpty()) {
112 lastPossibleResultPoints = null;
114 possibleResultPoints = new HashSet<ResultPoint>(5);
115 lastPossibleResultPoints = currentPossible;
116 paint.setAlpha(OPAQUE);
117 paint.setColor(resultPointColor);
118 for (ResultPoint point : currentPossible) {
119 canvas.drawCircle(frame.left + point.getX(), frame.top + point.getY(), 6.0f, paint);
122 if (currentLast != null) {
123 paint.setAlpha(OPAQUE / 2);
124 paint.setColor(resultPointColor);
125 for (ResultPoint point : currentLast) {
126 canvas.drawCircle(frame.left + point.getX(), frame.top + point.getY(), 3.0f, paint);
130 // Request another update at the animation interval, but only repaint the laser line,
131 // not the entire viewfinder mask.
132 postInvalidateDelayed(ANIMATION_DELAY, frame.left, frame.top, frame.right, frame.bottom);
136 public void drawViewfinder() {
142 * Draw a bitmap with the result points highlighted instead of the live scanning display.
144 * @param barcode An image of the decoded barcode.
146 public void drawResultBitmap(Bitmap barcode) {
147 resultBitmap = barcode;
151 public void addPossibleResultPoint(ResultPoint point) {
152 possibleResultPoints.add(point);