android - 扫码时如何修饰摄像头预览?
问题描述
我想创建一个条形码扫描仪活动,我发现了这个不错的教程: http: //www.devexchanges.info/2016/10/reading-barcodeqr-code-using-mobile.html
如何用检测到的区域(带点或矩形)装饰预览图像。
解决方案
在您的顶部放置另一个透明视图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
而不是自定义视图并创建您想要的可绘制对象。
推荐阅读
- javascript - 使用动态路由部署 Next JS 应用程序时出错
- azure-aks - 我们可以在没有 ACR 的情况下将本地 asp.net 核心 docker 映像部署到 AKS 集群吗
- php - Sentry with PHP:清除异常消息中的敏感数据
- go - 在 GORM 中访问联结表中的列
- java - 发布实体时 Spring Data Rest 警告
- python - 为什么索引被相同索引而不是不同索引填充的次数更多?
- javascript - 谷歌放置 api 密钥在 24 小时内不起作用
- python - 使用以下代码使 discord.py 机器人在 discord 上向随机用户发送消息:
- python - 根据特定组的最小值和最大值为列分配值
- android - 在 Jetpack Compose 中,如何应用存储在 Assets 中的字体/字体?