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

How to capture image inside the overlay in Camera2 API Android?

我只需要捕获覆盖框内的图像,如图所示。我不需要 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;
    }

编辑

Rectangle.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 中设置旋转标志,甚至您必须在应用中处理。

我最初创建了一个位图,比如说,capturedBitmap。这是相机拍摄的原始位图。

capturedImage = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);

然后我必须找到叠加层的确切坐标。

// Set the left, top, width and height of the overlay to get that portion of the image.
// Also, rotate the image i.e., rotationMatrix

croppedImage = Bitmap.createBitmap(capturedImage, left, top, cropWidth, cropHeight, rotationMatrix, false);