package com.barcodescanner import android.content.Context import android.graphics.Canvas import android.graphics.Color import android.graphics.Paint import android.graphics.Rect import android.graphics.Typeface import android.util.AttributeSet import androidx.core.content.ContextCompat import me.dm7.barcodescanner.core.ViewFinderView class BarcodeViewFinder : ViewFinderView { private var frameWidth: Int? = null private var frameHeight: Int? = null private var mFramingRect: Rect = Rect(0, 0, 0, 0) var viewFinderPositioned: ((x: Float, y: Float) -> Unit)? = null private val bounds = Rect() private val tutorialTextPaint: Paint by lazy { Paint().apply { color = Color.WHITE textSize = resources.getDimensionPixelSize(R.dimen.tutorial_text_size).toFloat() typeface = Typeface.DEFAULT_BOLD } } private val rectBackgroundColor by lazy { ContextCompat.getColor(context, R.color.rect_background_color) } private val rectStrokeColor by lazy { ContextCompat.getColor(context, R.color.rect_border_color) } private val rectTextPaint: Paint by lazy { Paint().apply { style = Paint.Style.FILL } } private val paddingVertical by lazy { resources.getDimensionPixelSize(R.dimen.padding_vertical) } private val paddingHorizontal by lazy { resources.getDimensionPixelSize(R.dimen.padding_horizontal) } private val radius by lazy { resources.getDimensionPixelSize(R.dimen.radius) } private val marginBottom by lazy { resources.getDimensionPixelSize(R.dimen.margin_bottom) } fun setFrameWidth(width: Int) { frameWidth = width setupViewFinder() } fun setFrameHeight(height: Int) { frameHeight = height setupViewFinder() } override fun getFramingRect(): Rect { return mFramingRect } constructor(context: Context) : super(context) { initView() } constructor(context: Context?, attributeSet: AttributeSet?) : super(context, attributeSet) { initView() } private fun initView() { tutorialTextPaint.getTextBounds( context.getString(R.string.center_barcode_in_scanner), 0, context.getString(R.string.center_barcode_in_scanner).length, bounds ) } private fun getFrameWidth() = frameWidth ?: (width * 0.75f).toInt() private fun getFrameHeight() = frameHeight ?: (width * 0.75f * 0.75f).toInt() @Synchronized override fun updateFramingRect() { val left = (width - getFrameWidth()) / 2 val top = (height - getFrameHeight()) / 2 mFramingRect = Rect( left, top, left + getFrameWidth(), top + getFrameHeight() ) viewFinderPositioned?.invoke(left.toFloat(), top.toFloat()) } override fun onDraw(canvas: Canvas) { super.onDraw(canvas) rectTextPaint.style = Paint.Style.FILL rectTextPaint.color = rectBackgroundColor canvas.drawRoundRect( (width - bounds.width()) * 1f/2 - paddingHorizontal, mFramingRect.top.toFloat() - marginBottom - bounds.height() - 2* paddingVertical, (width - bounds.width()) * 1f/2 - paddingHorizontal + bounds.width() + 2*paddingHorizontal, mFramingRect.top.toFloat() - marginBottom, radius.toFloat(), radius.toFloat(), rectTextPaint ) rectTextPaint.style = Paint.Style.STROKE rectTextPaint.color = rectStrokeColor canvas.drawRoundRect( (width - bounds.width()) * 1f/2 - paddingHorizontal, mFramingRect.top.toFloat() - marginBottom - bounds.height() - 2* paddingVertical, (width - bounds.width()) * 1f/2 - paddingHorizontal + bounds.width() + 2*paddingHorizontal, mFramingRect.top.toFloat() - marginBottom, radius.toFloat(), radius.toFloat(), rectTextPaint ) canvas.drawText( context.getString(R.string.center_barcode_in_scanner), (width - bounds.width()) * 1f/2 , mFramingRect.top.toFloat() - marginBottom - paddingVertical, tutorialTextPaint) } }