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 android.content.Context;
20 import android.content.res.Resources;
21 import android.graphics.Bitmap;
22 import android.graphics.Canvas;
23 import android.graphics.Paint;
24 import android.graphics.Rect;
25 import android.util.AttributeSet;
26 import android.view.View;
29 * This view is overlaid on top of the camera preview. It adds the viewfinder rectangle and partial
30 * transparency outside it, as well as the laser scanner animation and result points.
32 * @author dswitkin@google.com (Daniel Switkin)
34 public final class ViewfinderView extends View {
35 private static final int[] SCANNER_ALPHA = {0, 64, 128, 192, 255, 192, 128, 64};
36 private static final long ANIMATION_DELAY = 100L;
38 private final Paint paint;
39 private final Rect box;
40 private Bitmap resultBitmap;
41 private final int maskColor;
42 private final int resultColor;
43 private final int frameColor;
44 private final int laserColor;
45 private int scannerAlpha;
47 // This constructor is used when the class is built from an XML resource.
48 public ViewfinderView(Context context, AttributeSet attrs) {
49 super(context, attrs);
51 // Initialize these once for performance rather than calling them every time in onDraw().
54 Resources resources = getResources();
55 maskColor = resources.getColor(R.color.viewfinder_mask);
56 resultColor = resources.getColor(R.color.result_view);
57 frameColor = resources.getColor(R.color.viewfinder_frame);
58 laserColor = resources.getColor(R.color.viewfinder_laser);
63 public void onDraw(Canvas canvas) {
64 Rect frame = CameraManager.get().getFramingRect();
68 int width = canvas.getWidth();
69 int height = canvas.getHeight();
71 // Draw the exterior (i.e. outside the framing rect) darkened
72 paint.setColor(resultBitmap != null ? resultColor : maskColor);
73 box.set(0, 0, width, frame.top);
74 canvas.drawRect(box, paint);
75 box.set(0, frame.top, frame.left, frame.bottom + 1);
76 canvas.drawRect(box, paint);
77 box.set(frame.right + 1, frame.top, width, frame.bottom + 1);
78 canvas.drawRect(box, paint);
79 box.set(0, frame.bottom + 1, width, height);
80 canvas.drawRect(box, paint);
82 if (resultBitmap != null) {
83 // Draw the opaque result bitmap over the scanning rectangle
85 canvas.drawBitmap(resultBitmap, frame.left, frame.top, paint);
87 // Draw a two pixel solid black border inside the framing rect
88 paint.setColor(frameColor);
89 box.set(frame.left, frame.top, frame.right + 1, frame.top + 2);
90 canvas.drawRect(box, paint);
91 box.set(frame.left, frame.top + 2, frame.left + 2, frame.bottom - 1);
92 canvas.drawRect(box, paint);
93 box.set(frame.right - 1, frame.top, frame.right + 1, frame.bottom - 1);
94 canvas.drawRect(box, paint);
95 box.set(frame.left, frame.bottom - 1, frame.right + 1, frame.bottom + 1);
96 canvas.drawRect(box, paint);
98 // Draw a red "laser scanner" line through the middle to show decoding is active
99 paint.setColor(laserColor);
100 paint.setAlpha(SCANNER_ALPHA[scannerAlpha]);
101 scannerAlpha = (scannerAlpha + 1) % SCANNER_ALPHA.length;
102 int middle = frame.height() / 2 + frame.top;
103 box.set(frame.left + 2, middle - 1, frame.right - 1, middle + 2);
104 canvas.drawRect(box, paint);
106 // Request another update at the animation interval, but only repaint the laser line,
107 // not the entire viewfinder mask.
108 postInvalidateDelayed(ANIMATION_DELAY, box.left, box.top, box.right, box.bottom);
112 public void drawViewfinder() {
118 * Draw a bitmap with the result points highlighted instead of the live scanning display.
120 * @param barcode An image of the decoded barcode.
122 public void drawResultBitmap(Bitmap barcode) {
123 resultBitmap = barcode;