首页 > 解决方案 > 扫码时如何修饰摄像头预览?

问题描述

我想创建一个条形码扫描仪活动,我发现了这个不错的教程: http: //www.devexchanges.info/2016/10/reading-barcodeqr-code-using-mobile.html

如何用检测到的区域(带点或矩形)装饰预览图像。

标签: androidbarcode-scannerandroid-vision

解决方案


在您的顶部放置另一个透明视图SurfaceView并在其上绘制。两种视图都将松散耦合,因此需要一些数学运算才能将装饰与相机输出正确匹配。

所以在你的activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp">

    <SurfaceView
        android:id="@+id/surface_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true" />

    <com.yourApp.YourDecoratorView
        android:id="@+id/decorator_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true" />
    ...
</RelativeLayout> 

然后,创建一个自定义视图:

class YourDecoratorView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {

    ... initializing code

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)

        // your code
        canvas?.drawLine(0f, 0f, 0f, paint)
    }
}

如果它只是您想要显示的静态视觉效果,请使用 anImageView而不是自定义视图并创建您想要的可绘制对象。


推荐阅读