]> rtime.felk.cvut.cz Git - hornmich/skoda-qr-demo.git/blob - BarCodeScanner/mobile/src/main/java/cz/cvut/fel/dce/barcodescanner/ViewfinderView.java
Initial commit
[hornmich/skoda-qr-demo.git] / BarCodeScanner / mobile / src / main / java / cz / cvut / fel / dce / barcodescanner / ViewfinderView.java
1 /*
2  * Copyright (C) 2008 ZXing authors
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package cz.cvut.fel.dce.barcodescanner;
18
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;
28
29 import com.google.zxing.ResultPoint;
30 import cz.cvut.fel.dce.barcodescanner.camera.CameraManager;
31
32 import java.util.ArrayList;
33 import java.util.List;
34
35 /**
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.
38  *
39  * @author dswitkin@google.com (Daniel Switkin)
40  */
41 public final class ViewfinderView extends View {
42
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;
48
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;
59
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);
63
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);
71     scannerAlpha = 0;
72     possibleResultPoints = new ArrayList<>(5);
73     lastPossibleResultPoints = null;
74   }
75
76   public void setCameraManager(CameraManager cameraManager) {
77     this.cameraManager = cameraManager;
78   }
79
80   @SuppressLint("DrawAllocation")
81   @Override
82   public void onDraw(Canvas canvas) {
83     if (cameraManager == null) {
84       return; // not ready yet, early draw before done configuring
85     }
86     Rect frame = cameraManager.getFramingRect();
87     Rect previewFrame = cameraManager.getFramingRectInPreview();    
88     if (frame == null || previewFrame == null) {
89       return;
90     }
91     int width = canvas.getWidth();
92     int height = canvas.getHeight();
93
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);
100
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);
105     } else {
106
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);
113       
114       float scaleX = frame.width() / (float) previewFrame.width();
115       float scaleY = frame.height() / (float) previewFrame.height();
116
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;
123       } else {
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),
132                               POINT_SIZE, paint);
133           }
134         }
135       }
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),
144                               radius, paint);
145           }
146         }
147       }
148
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);
156     }
157   }
158
159   public void drawViewfinder() {
160     Bitmap resultBitmap = this.resultBitmap;
161     this.resultBitmap = null;
162     if (resultBitmap != null) {
163       resultBitmap.recycle();
164     }
165     invalidate();
166   }
167
168   /**
169    * Draw a bitmap with the result points highlighted instead of the live scanning display.
170    *
171    * @param barcode An image of the decoded barcode.
172    */
173   public void drawResultBitmap(Bitmap barcode) {
174     resultBitmap = barcode;
175     invalidate();
176   }
177
178   public void addPossibleResultPoint(ResultPoint point) {
179     List<ResultPoint> points = possibleResultPoints;
180     synchronized (points) {
181       points.add(point);
182       int size = points.size();
183       if (size > MAX_RESULT_POINTS) {
184         // trim it
185         points.subList(0, size - MAX_RESULT_POINTS / 2).clear();
186       }
187     }
188   }
189
190 }