android - 如何在 Camera2 API Android 中捕获叠加层内的图像?

标签 android overlay crop android-camera2

Image inside overlay

我只需要捕获覆盖框内的图像,如图所示。我不需要 TextureView 中的整个图像。我是 Android 的新手,我想不出几个提到的只捕获矩形区域内图像的解决方案。 叠加层必须位于中心。我试过了,但我无法实现。

我遵循了这段代码 - Google Sample-Camera2Basic


为了添加叠加层,我使用了以下代码:

activity_camera2_basic_fragment.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">

    <com.example.googlecamera2.AutoFitTextureView
        android:id="@+id/texture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true" />

    <LinearLayout
        android:id="@+id/surface"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_centerInParent="true"/>

    <FrameLayout
        android:id="@+id/control"
        android:layout_width="match_parent"
        android:layout_height="112dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:background="@color/control_background">

        <Button
            android:id="@+id/picture"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/picture" />

        <ImageButton
            android:id="@+id/info"
            android:contentDescription="@string/description_info"
            style="@android:style/Widget.Material.Light.Button.Borderless"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical|right"
            android:padding="20dp"
            android:src="@drawable/ic_action_info" />

    </FrameLayout>

</RelativeLayout>

我这样实现了 onCreateView:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_camera2_basic, container, false);

        linearLayout = view.findViewById(R.id.surface);
        linearLayout.addView(new Rectangle(getActivity()));
        return view;
    }

编辑

矩形.java

package com.example.googlecamera2;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.View;

public class Rectangle extends View {
    Paint paint = new Paint();

    public Rectangle(Context context) {
        super(context);
    }


    @Override
    public void onDraw(Canvas canvas) {
        paint.setColor(Color.GREEN);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(6);
        Rect rect = new Rect(120, 100, 620, 900);
        canvas.drawRect(rect, paint );
    }
}

请建议我如何只从覆盖区域获取裁剪图像并在预览中将覆盖居中。

最佳答案

假设您知道矩形相对于相机预览区域的x,y,w,h,即0,0,textureWidth,textureHeight .

您还知道,当您收到 Jpeg 时,图像的尺寸:0,0,jpegWidth,jpegHeight

现在您需要将第一个缩放为第二个:裁剪区域将是

x*jpegHeight/textureWidth 
y*jpegWidth/textureHight 
w*jpegHeight/textureWidth 
h*jpegWidth/textureheight

宽度和高度翻转了,因为你使用纵向配置:很少有 Android 相机可以自己生成纵向 Jpeg。大多数情况下,他们只在 EXIF header 中设置旋转标志,甚至是您必须在应用中处理的标志。

关于android - 如何在 Camera2 API Android 中捕获叠加层内的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55038077/

相关文章:

android - 设置backgroundTint兼容所有android版本

android - 多次调用分页库中的回调

android - 填充微调器

Android 评分栏 : getProgressDrawable doesn't cast to right Class

iOS - 如何像默认相册一样实现裁剪图像?

带有叠加图像和文本的 html 横幅

android - 相机预览内存不足错误

qt - QGLWidget : Strange results 上的半透明 QWidget

c++ - 在 Qt 中使用 QRubberBand 裁剪图像

python - OpenCV 校准相机裁剪错误