1 /*
2  * Copyright 2022 The Android Open Source Project
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 androidx.camera.integration.diagnose
18 
19 import android.content.Context
20 import android.graphics.Canvas
21 import android.graphics.Color
22 import android.graphics.Paint
23 import android.graphics.PointF
24 import android.util.AttributeSet
25 import android.util.Log
26 import android.widget.FrameLayout
27 
28 /**
29  * Overlay View for drawing alignment result and target grid line to {@link Canvas} in diagnosis
30  * mode.
31  */
32 class OverlayView(context: Context, attrs: AttributeSet) : FrameLayout(context, attrs) {
33 
34     private val unalignedTargetGridPaint = Paint()
35     private var alignedTargetGridPaint = Paint()
36     private var thresholdPaint = Paint()
37     // TODO: should it be nullable? only drawing when there are 4 barcodes
38     private var result: Calibration? = null
39 
40     init {
41         // TODO: scale density of the width with the screen's pixel density
42         unalignedTargetGridPaint.color = Color.RED
43         unalignedTargetGridPaint.strokeWidth = 8F
44         alignedTargetGridPaint.color = Color.GREEN
45         alignedTargetGridPaint.strokeWidth = 8F
46         thresholdPaint.color = Color.WHITE
47     }
48 
setCalibrationResultnull49     fun setCalibrationResult(result: Calibration) {
50         this.result = result
51     }
52 
drawGridLinesnull53     private fun drawGridLines(canvas: Canvas, line: Pair<PointF, PointF>, paint: Paint) {
54         canvas.drawLine(line.first.x, line.first.y, line.second.x, line.second.y, paint)
55     }
56 
getGridLinePaintnull57     private fun getGridLinePaint(isAligned: Boolean?): Paint {
58         return if (isAligned == true) {
59             alignedTargetGridPaint
60         } else {
61             unalignedTargetGridPaint
62         }
63     }
64 
onDrawnull65     override fun onDraw(canvas: Canvas) {
66         super.onDraw(canvas)
67         Log.d(TAG, "calling on draw")
68 
69         result?.let {
70             val paintColor = getGridLinePaint(result!!.isAligned)
71             result!!.topGrid?.let { drawGridLines(canvas, it, paintColor) }
72             result!!.bottomGrid?.let { drawGridLines(canvas, it, paintColor) }
73             result!!.rightGrid?.let { drawGridLines(canvas, it, paintColor) }
74             result!!.leftGrid?.let { drawGridLines(canvas, it, paintColor) }
75 
76             // drawing threshold box
77             result!!.thresholdTopLeft?.let { canvas.drawRect(it, thresholdPaint) }
78             result!!.thresholdTopRight?.let { canvas.drawRect(it, thresholdPaint) }
79             result!!.thresholdBottomLeft?.let { canvas.drawRect(it, thresholdPaint) }
80             result!!.thresholdBottomRight?.let { canvas.drawRect(it, thresholdPaint) }
81         }
82         Log.d(TAG, "finished drawing")
83     }
84 
85     companion object {
86         private const val TAG = "OverlayView"
87     }
88 }
89