public void draw(Canvas canvas) {
Log.d(TAG, "on draw text graphic");
if (element == null) {
throw new IllegalStateException("Attempting to draw a null text.");
}
// Draws the bounding box around the TextBlock.
RectF rect = new RectF(p,q,r,s);
canvas.drawRect(rect, rectPaint);
// Renders the text at the bottom of the box.
canvas.drawText(text, rect.left, rect.bottom, textPaint);
我在图像中有坐标(p,q,r,s),我只想模糊这个区域,现在我在它周围画一个矩形。如果可以使用画布,那就太好了,但也欢迎其他方法
我查看了其他文章,每个人都参考了 RenderScript 并模糊了整个图像或模糊了 textview,我在这里没有使用 textview,而是在给定坐标的情况下动态创建矩形(目的是模糊这部分)的 GraphicOverlay。
这是布局视图:
<android.support.constraint.ConstraintLayout
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">
<ImageView
android:id="@+id/image_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="Select image for text recognition"
android:scaleType="fitStart"
app:layout_constraintBottom_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="parent" />
<com.example.sengu.test_1.GraphicOverlay
android:id="@+id/graphic_overlay"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" />
<Button
android:id="@+id/capture_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toTopOf="@id/button_text"
android:text="Capture" />
<Button
android:id="@+id/button_text"
android:text="Find text"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@id/button_translate_text"
app:layout_constraintBottom_toBottomOf="parent"/>
</android.support.constraint.ConstraintLayout>