[英]Draw rect over camera preview
我有一個相機預覽,我可以在其中獲取每一幀,對於每一幀,我都會分析里面的對象,並且我有一個包含識別出的每個對象和位置的列表,所以我已經有了每個對象的位置。 現在我想在相機預覽上圍繞該對象繪制一個矩形,但是做 canvas.drawRect 不起作用,有什么建議嗎?
for (final Detector.Recognition result : results) {
final RectF location = result.getLocation();
if (location != null && result.getConfidence() >= 0.1) {
result.setLocation(location);
mappedRecognitions.add(result);
Log.d("OBJECT: ", result.getTitle());
final Paint paint = new Paint();
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(2.0f);
canvas_crop.drawRect(location, paint);
cropToFrameTransform.mapRect(location);
}
}
這是布局 XML
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"
android:orientation="vertical">
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<com.otaliastudios.cameraview.CameraView
android:id="@+id/cameraView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
如果您不需要記錄矩形,因此只在實時預覽中顯示,那么通常的解決方案是覆蓋一個專門用於渲染圖紙的自定義視圖。
創建自定義視圖並將其放在頂部,確保它始終與CameraView 顯示的預覽的位置和大小(縱橫比)相匹配。 接下來是一個讓您入門的示例,盡管您需要添加邏輯以確保它與 CameraView “預覽”指標相匹配。 使用setTargets傳遞要繪制的矩形:
爪哇:
public final class OverlayView extends View {
private final Paint paint = new Paint();
private final List<Rect> targets = new ArrayList<>();
public OverlayView(@NonNull final Context context) {
this(context, null);
}
public OverlayView(@NonNull final Context context, @Nullable final AttributeSet attrs) {
this(context, attrs, 0);
}
public OverlayView(@NonNull final Context context, @Nullable final AttributeSet attrs, final int defStyleAttr) {
super(context, attrs, defStyleAttr);
final float density = context.getResources().getDisplayMetrics().density;
this.paint.setStrokeWidth(2.0f * density);
this.paint.setColor(Color.BLUE);
this.paint.setStyle(Paint.Style.STROKE);
}
@Override
protected void onDraw(final Canvas canvas) {
super.onDraw(canvas);
synchronized (this) {
for (final Rect entry : this.targets) {
canvas.drawRect(entry, this.paint);
}
}
}
public void setTargets(@NonNull final List<Rect> sources) {
synchronized (this) {
this.targets.clear();
this.targets.addAll(sources);
this.postInvalidate();
}
}
}
科特林:
class OverlayView(context: Context, attrs: AttributeSet) : View(context, attrs) {
private val paint = Paint()
private val targets: MutableList<Rect> = ArrayList()
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
synchronized(this) {
for (entry in targets) {
canvas.drawRect(entry, paint)
}
}
}
fun setTargets(sources: List<Rect>) {
synchronized(this) {
targets.clear()
targets.addAll(sources)
this.postInvalidate()
}
}
init {
val density = context.resources.displayMetrics.density
paint.strokeWidth = 2.0f * density
paint.color = Color.BLUE
paint.style = Paint.Style.STROKE
}
}
對於您的特定 xml,如下所示:
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"
android:orientation="vertical">
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<com.otaliastudios.cameraview.CameraView
android:id="@+id/cameraView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<com.otaliastudios.cameraview.OverlayView
android:id="@+id/overlayView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
請注意,您正在為 CameraView 使用match_parent ,這應該在代碼中更好地處理以調整到所有可能的相機縱橫比,因此當您解析預覽大小時,這也必須應用於新的OverlayView 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.