【问题标题】:How to capture image inside the overlay in Camera2 API Android?如何在 Camera2 API Android 中捕获叠加层内的图像?
【发布时间】:2019-03-07 07:19:16
【问题描述】:

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

请建议我如何从叠加区域中获取裁剪后的图像,并在预览中居中叠加。

【问题讨论】:

  • 似乎有两个独立的问题:一个是如何裁剪Jpeg,第二个是如何裁剪实时预览。两者都有一个简单的解决方法:为您的相机选择适当的放大级别。
  • 您能详细说明一下吗?我是 Android 新手,我不知道如何实现您的建议。我的主要问题是从矩形中获取图像。你能分享一个sn-p吗?不胜感激!
  • 我不明白为什么要裁剪实时预览?
  • 我可能误解了在预览中将叠加层居中的含义。如果您对实时预览感到满意,那么您只需要知道如何裁剪 Jpeg。
  • 是的,你能帮我裁剪一下吗?我实际上已经硬编码了叠加层的坐标。设备变化时不居中。

标签: android overlay crop android-camera2


【解决方案1】:

假设您知道矩形相对于相机预览区域的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 标头中设置轮换标志,甚至是您必须在应用中处理的标志。

【讨论】:

  • 感谢您的重要建议。
  • 您能详细说明一下吗?我取了所有的值,但是如何从上述公式生成新的缩放位图?
  • @RiddhiShah 在这里看到other answer
【解决方案2】:

我最初创建了一个位图,例如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);

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多